I'm trying to retrieve polygons out of a postgresql database and I'm running into several problems. First, I've been able to get ST_AsText(geom) to work in conjunction with arcpy.FromWKT(), but the precision is lost as described in the PostGIS docs:

WKT format does not maintain precision so to prevent floating truncation, use ST_AsBinary or ST_AsEWKB format for transport.

But when I try to use ST_AsBinary or ST_AsEWKB with arcpy.FromWKB I get these errors:

cur.execute("SELECT ST_AsBinary(geom) FROM master.modeled_yield_hotspots WHERE model_id = 4;")
lsTemp = cur.fetchall()
lsGeom = []
for j in range(0, len(lsTemp)):
arcpy.CopyFeatures_management(lsGeom, pathScratchGDB + "fcHotspots")

Traceback (most recent call last):
  File "C:\SmartFarm_Tools\python_scripts\testing.py", line 24, in <module>
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\__init__.py", line 1640, in FromWKB
    return gp.createObject("geometry", byte_array, None, spatial_reference)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\geoprocessing\_base.py", line 379, in createObject
    self._gp.CreateObject(*gp_fixargs(args, True)))
RuntimeError: Object: CreateObject cannot create geometry from inputs

If I print lsTemp[j] I get the following:

(<read-only buffer for 0x0000000012808190, size 82146, offset 0 at 0x0000000003092F10>,)

What is a read-only buffer? How do I get WKB out of postgis and into a feature class or shapefile?

  • See here for information about what buffers are. – jpmc26 Sep 29 '17 at 23:44

It looks like from the documentation that arcpy.FromWKB expects a bytearray data type, which you can get using the bytearray() function:

    SELECT ST_AsBinary(geom)
    FROM master.modeled_yield_hotspots
    WHERE model_id = %s;
""", (4,))
lsGeom = []
for r in cur:
  • Did this. Still got an error. =/ And I'm certain the conversion worked correctly. (I can do a direct hexadecimal comparison between the source and the bytearray.) – jpmc26 Sep 29 '17 at 23:53

If you're already interfacing with your data in PostGIS using ArcPy, you could simply use a SearchCursor, manipulate the data as needed, and then insert it into a shapefile/fGDB feature class using an InsertCursor.

You don't need to use Psycopg2 to pull the data out, the SearchCursor can handle that.

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.