[GIS] Create a PostGIS raster using GDAL and Python

gdalpostgispythonraster

I'd like to create a PostGIS raster using GDAL and python.

I can get the driver easily:

drv = gdal.GetDriverByName("PostGISRaster")

But how can I create a new raster table? I can't find any information about drv.Create to explain what to put there….?

Best Answer

It looks like the ability to create rasters is not yet available in the driver. However, you can create a layer using your standard PostgreSQL Python driver (i.e. psycopg2) and then open it from GDAL.

So you'd create a layer using ST_MakeEmptyRaster and ST_AddBand:

CREATE TABLE rtest (gid serial primary key, rast raster);
INSERT INTO rtest (rast) VALUES (ST_AddBand(ST_MakeEmptyRaster(100, 100, 1, 1, 2), '8BUI'::text));

Then you'd open and manipulate the raster in GDAL/Python

ds = gdal.Open('PG:host=localhost dbname=mydb table=rtest user=myuser')

I haven't tested this extensively yet, but it seems to work. doesn't work at all. I get the error,

Writing through PostGIS Raster band not supported yet

when I try to write.

You could use Python and psycopg2 to write chunks of your image to a raster table using ST_SetValues.