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....?


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.

  • yeah...but you wouldn't be able to write pixel values to the raster (without explicitly using postgis functions), right? – James Jul 31 '15 at 10:38
  • Hah, you're right. I didn't test that one crucial step. Made a small edit to my answer to reflect that. – Rob Skelly Jul 31 '15 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.