1

As part of a larger script I am extracting postgis geometries from WKT(got that part down) then I am trying to create an in_memory polygon geometry to be used in arcpy tools.

so far:

    postgis_layer="select ST_Astext(st_force2d(shape)) from {}".format(fc2)
    print postgis_layer
    cur.execute(postgis_layer)
    #good until here!
    in_mry=arcpy.Polygon(arcpy.FromWKT((x for x in cur)))
    print in_mry
    #arcpy.Union_analysis(fc1,in_mry,output)

how do I go from arcpy.FromWKT to the arcpy.Polygon? then can I use that in the union tool? I do not want to save the geometries from postgis as feature classes

    postgis_layer="select ST_Astext(st_force2d(shape)) from {}".format(fc2)
    print postgis_layer
    cur.execute(postgis_layer)
    geoms=arcpy.Array()
    for geom in cur:
        print geom[0]
        geoms.add(arcpy.FromWKT(geom[0]))
    in_mry=arcpy.Polygon(geoms)
    print in_mry
    #arcpy.Union_analysis(fc1,in_mry,output)



select ST_Astext(st_force2d(shape)) from rmp_indicators.total_forest
POLYGON((535836.248384263 740131.124948196,535934.437164262 740343.250836283,535909.998236761 740114.438629866,535836.248384263 740131.124948196))
Traceback (most recent call last):
  File "R:\Data\LUCZ_2017\Python_Scripts\Unclassified_Areas.py", line 53, in <module>
    p.union_fcs('blah','rmp_indicators.total_forest','b')
  File "R:\Data\LUCZ_2017\Python_Scripts\Unclassified_Areas.py", line 43, in union_fcs
    geoms.add(arcpy.FromWKT(geom[0]))
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\arcobjects\arcobjects.py", line 48, in add
    return convertArcObjectToPythonObject(self._arc_object.Add(*gp_fixargs(args)))
ValueError: Array: Add input not point nor array object

so I got until here:

    polys=[arcpy.FromWKT(geom[0]) for geom in cur]
    for x in polys:
        print x
    poly=arcpy.Polygon(polys)

polys works, but I do not know how to pass polys into the arcpy.Polygon

x prints out

<geoprocessing describe geometry object object at 0x24FAC2E0>
<geoprocessing describe geometry object object at 0x24FAC320>
<geoprocessing describe geometry object object at 0x24FAC360>
<geoprocessing describe geometry object object at 0x24FAC3A0>
<geoprocessing describe geometry object object at 0x24FAC3E0>
<geoprocessing describe geometry object object at 0x24FAC420>
<geoprocessing describe geometry object object at 0x24FAC460>
<geoprocessing describe geometry object object at 0x24FAC4A0>
<geoprocessing describe geometry object object at 0x24FAC4E0>
<geoprocessing describe geometry object object at 0x24FAC520>

so clearly the geometries are there, I just do not know how to pass them into the Polygon geometry. I am going to just try and use polys as the input to the union tool, maybe the arcpy.FromWKT automatically turns it into a functioning Polygon because it is one

  • This is an extremely convoluted route for what would be a very simple Query Layer utilization exercise. This may be an XY Problem, better solved by asking for a solution to the root problem than to the problematic methodology. – Vince Feb 15 '18 at 22:29
  • good point Vince, I have not thought of that option and that might work well. However I must add I have had very shaky experience with postgres to arc and back and forth. I understand its read and write with 10.4.1 or greater but I have found if I make edits in postgresql, Arc does not like those changes – ziggy Feb 16 '18 at 0:04
  • 1
    ArcGIS expects valid topology, which is not entirely unreasonable. – Vince Feb 16 '18 at 0:48
  • @Vince for sure, I would say the lack of valid topology is something that is lacking on the PostGIS end. Then again, they use different geometry models – ziggy Feb 16 '18 at 13:35
0

So I ended up figuring this out:

Essenitally the arcpy.FromWKT() will create the actual geometries you pass through it. I was under the assumption based on other ESRI geometry constructors that this would have to be passed at least through the arcpy.Polygon constructor if not the arcpy.Array(). The documentation is pretty sparse for this arcpy.FromWKT() but whatever you pass through will be ready to use geometries

postgis_layer="select ST_Astext(st_force2d(shape)) from {}".format(fc2)
cur.execute(postgis_layer)
polys=[arcpy.FromWKT(geom[0]) for geom in cur]
mem = r"in_memory\polygons"
memory_layer=arcpy.CopyFeatures_management(polys,mem)
arcpy.Union_analysis([fc1,memory_layer],output)

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.