1

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

Martin Hügi
3,6822 gold badges26 silver badges51 bronze badges
asked May 6, 2015 at 16:00

2 Answers 2

1

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.

answered Jul 31, 2015 at 0:03
2
  • yeah...but you wouldn't be able to write pixel values to the raster (without explicitly using postgis functions), right? Commented Jul 31, 2015 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. Commented Jul 31, 2015 at 15:57
0

Recently came across this and it ended up helping me, so I thought I would share an update.

Adding additional arguments help as well as adding raster constraints to the table you are trying to use.

ds = gdal.Open('PG:host='' dbname='' schema='' table='' user='' port='' password='' column='' mode=2')
SELECT AddRasterConstraints('raster_table'::name, 'column_name'::name);

Hope this helps someone else too with the added details.

Bera
81.7k14 gold badges84 silver badges198 bronze badges
answered May 4, 2021 at 6:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.