[GIS] Creating a geoTIFF from PostGIS raster column

gdalgeoalchemypostgispythonraster

I am able to retrieve raw raster data from a PostGIS raster in a PostgreSQL database using SQL Alchmey. I can get the raw data directly from the database. Now I would like to create a geoTIFF file from the raw data. Here is the raw raster data from the database.

0100000100000000000000D03F000000000000D0BF0000000000804140000000000000F43F00000000000000000000000000000000E6100000010001004A003C1CC600000000

enter image description here

The above image is a screenshot of the database table that I am accessing through SQL Alchemy. Is there a way that I can create a geotiff from this raw data using gdal in python? I have looked for a similar workflow, but couldn't find any examples on creating a tiff directly from raw raster data.

Best Answer

You can query for TIFF data directly:

The query:

SELECT
    ST_AsTIFF(ST_Union(rast), 'LZW') as tiff,
FROM {dataset_name}
WHERE ST_Intersects(ST_SetSRID(ST_GeomFromText('{region_string}'), '4326'), rast)

Then you can use GDAL or RasterIO to write it to a file.

(You may even be able to just dump the binary direct to a file.)

Related Question