Select to view content in your preferred language

How do I create a raster layer from a postgres/postGIS database?

2681
3
12-15-2020 08:54 PM
Labels (2)
JesseTemplin2
Regular Contributor

Hello,

I'm new to ArcGIS, and would like to create a file geodatabase or self contained project by making queries to a postgres database that includes postGIS data.

So far, I have been able to create and load a template with some layer groups, populate the groups with query layers that each contain one geometry, and then save it all to a new project.

The part I'm stuck on is creating a raster layer. The raster data are stored as well known binary in our database, and I can get them into ArcGIS Pro as Blobs in a standalone table by making a query something like this:

 

SELECT ST_AsTIFF(ST_RastFromWKB(wkb_image), 'JPEG90') tiff_image FROM my_table

 

Then I tried passing the Blobs to MakeRasterLayer inside of a SearchCursor loop:

 

with arcpy.da.SearchCursor(table, ["tiff_image"]) as cursor:
    print(f"\nrows:")
    for row in cursor:
        print(row[0])
        arcpy.MakeRasterLayer_management(row[0], "RasterLayer")

 

But that didn't work.

The next thing I'm thinking is to save the images before any of the ArcPy function calls, then load them with MakeRasterLayer. The documentation makes it sound like that should work.

Is there a better way to do this with ArcPy?

0 Kudos
3 Replies
DavidPike
MVP Notable Contributor

possibly Raster—ArcGIS Pro | Documentation , run ras_object = arcpy.Raster(row[0]) then ras_object.save(out_path) but I might be way-off.

0 Kudos
Robert_LeClair
Esri Esteemed Contributor

If you have the Esri Data Interoperability Extension, you can import PostGIS, PostGIS Raster and PostgreSQL data into your GDB's.

0 Kudos
JesseTemplin2
Regular Contributor

Thanks for the responses. I think the arcpy.Raster function is expecting the same type of input as MakeRasterLayer. The Data Interop extension would probably work, but we don't have that license.

I don't have a complete solution yet, but I was able to install that psycopg2 package in ArcGIS Pro, then query the database with ST_RastFromWKB, and save the byte array as a tif. Now I just need to figure out how to embed the geographic data in the tif (preferable) or save it in a separate file as described here:

https://community.esri.com/t5/arcgis-runtime-sdk-for-net/raster-without-spatial-reference/td-p/28047...

0 Kudos