1

I would like to import the compressed ZSTD GeoTiff to PostGIS with the following commands (below) but I get this error: RASTER_fromGDALRaster: Could not open bytea with GDAL. Check that the bytea is of a GDAL supported format. However, I do not have any problems with the import of uncompressed GeoTiff rasters. Does GDAL support the import of compressed GeoTiff? Thank you for your hints.

import psycopg2
from rasterio.io import MemoryFile

db_params = {
        "dbname": "test_db",
        "user": "postgres",
        "password": "demo",
        "host": "localhost",
        "port": "5432"}

table_name = "species_UZ"
raster_column_name = 'species'

try:
    conn = psycopg2.connect(**db_params)
    cursor = conn.cursor()

    file_path='my_raster.tif'
    with rasterio.open(file_path) as src:
            # You might need to adjust based on your raster structure
        raster_data = src.read()
        profile = src.profile

            # Convert to PostGIS-compatible format (e.g., GeoTIFF in memory)
        with MemoryFile() as memfile:
            with memfile.open(**profile) as dst:
                dst.write(raster_data)
                gdal_raster_bytes = memfile.read()
        
        create_table_sql = f"""
           SET postgis.gdal_enabled_drivers = 'ENABLE_ALL';
           CREATE TABLE IF NOT EXISTS {table_name} (id SERIAL PRIMARY KEY, {raster_column_name} RASTER);"""
        cursor.execute(create_table_sql)

        insert_sql = f"INSERT INTO {table_name} ({raster_column_name}) VALUES (ST_FromGDALRaster(%s));"
        cursor.execute(insert_sql, (gdal_raster_bytes,))

        conn.commit()
        print(f"Raster successfully imported into {table_name}.")

except Exception as e:
    print(f"Error: {e}")
finally:
    if cursor:
        cursor.close()
    if conn:
        conn.close()
1
  • always put full error message because there are other useful information. Commented Jul 29 at 17:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.