AnsweredAssumed Answered

ArcGIS Python API's WKT doesn't match EWKT from PostGIS

Question asked by dca.uw on Oct 24, 2017
Latest reply on Nov 14, 2017 by dca.uw

Hi all - I am trying to retrieve features from an ArcGIS Server 10.31 map service using the ArcGIS Python API and insert them into PostgreSQL/PostGIS. For the geometry, both use WKT, but they don't have the same format. Here is a snippet of what I run to get the data:

import arcgis
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
from arcgis.geometry import Geometry

perimetersLAYER = FeatureLayer('https://<URL>/arcgis/rest/services/<service>/MapServer/1')
perimeters_feature_set = perimetersLAYER.query(where="objectid = 8605")

for feature in perimeters_feature_set.features:
    print('\n\n')
    for field in feature.fields:
        print(field + ': ' + str(feature.get_value(field)))
   
    geom = Geometry(feature.geometry)
    print('geom.WKT: ' + str(geom.WKT))
   

 

The ArcGIS Python API Geometry response looks like this:

MULTIPOLYGON (((-13645793.9305409509688616,  4645922.6947647286579013), 
(-13645734.9949775058776140,  4645907.8913778141140938),
(-13645738.5948890540748835,  4645918.5636747097596526),
(-13645706.8243041206151247,  4645943.8052548961713910), [SNIP]

I'd like to pass this directly to a SQL insert with the PostGIS MULTIPOLYGON function, which looks like this:

MULTIPOLYGON(((0 0 0,4 0 0,4 4 0,0 4 0,0 0 0),(1 1 0,2 1 0,2 2 0,1 2 0,1 1 0)),((-1 -1 0,-1 -2 0,-2 -2 0,-2 -1 0,-1 -1 0)))

 

Note that the geometry formats are slightly different. In PostGIS, the x/y/z coords are space delimited and each vertex is comma delimited. The Esri geometry's x/y coords are comma delimited, and each vertex is wrapped in parentheses and also comma delimited.

 

I can parse all the strings and munge the formats from one to another, but is there a way to automagically convert from one to another? It looks like Esri's WKT does not match the input PostGIS needs for its WKT functions. When I try the Geometry's WKB and JSON properties, it returns None, so I'm not sure where to go with that.

 

Of course the geometry format is the minutia of my problem. The bigger picture issue is to find the shortest path from an ArcGIS Enterprise map service to PostGIS. If there's a better way, I'm all ears, though I'd like to keep it in python and run it on Linux.

 

thanks!

Outcomes