0

I have a polygon feature class that i am looping through row by row. I want to take each row/record and create it into its own Polygon Geometry or Shapefile/FeatureLayer using WKT

PolygonGeometry Attempt:

summary = 'fc'
for row in arcpy.da.SearchCursor(summary, ["SHAPE@"]):
    wellknown = row[0].WKT
    arcpy.Polygon(wellknown)

I have tried

arcpy.Polygon(*[wellknown])

also

arcpy.Polygon(arcpy.Array([arcpy.Point(*wellknown)]))

Traceback (most recent call last):
  File "C:\Users\rzagha\Desktop\RTK\python\FreighMunicipality.py", line 121, in <module>
    hm = arcpy.Polygon(arcpy.Array([arcpy.Point(*wkt)]))
TypeError: __init__() takes at most 6 arguments (36467 given)

whats the correct syntax to create a polygon geometry from WKT and is there an easier way to create a featurelayer or shapefile from current row/record within the for loop like

arcpy.MakeFeatureLayer_management(row[0], "zone")  

or

arcpy.MakeFeatureLayer_management(row[0].wkt, "zone")
2

You can pass the current row's OID to the where clause of arcpy.MakeFeatureLayer_management.

with arcpy.da.SearchCursor(summary, ['OID@', 'name']) as cursor:
    for row in cursor:
        layerName = 'layer_{}'.format(name)
        print(layerName)
        where_clause = '"OBJECTID" = {}'.format(row[0])
        arcpy.MakeFeatureLayer_management (summary, layerName, where_clause)
  • I am still curious though how one would create a Polygon from WKT using arcpy – ziggy Oct 5 '16 at 18:27
  • 2
    You could construct the geometry object with the .fromWKT method and pass that to the SHAPE@ field with an InsertCursor as illustrated in Brian's response. – Evan Oct 5 '16 at 18:39
2

If you wanted to make shapefiles you could do something like this:

import arcpy, os
arcpy.env.overwriteOutput = True
fc = r'C:\temp\someShapefile.shp'

dataCursor = arcpy.da.SearchCursor(fc, ['SHAPE@', 'someField'])

for row in dataCursor:
    sr = row[0].spatialReference
    newFC = r'C:\temp\{0}.shp'.format(row[1])
    arcpy.CreateFeatureclass_management(os.path.dirname(newFC), os.path.basename(newFC), 'POLYGON', spatial_reference=sr)
    insertCursor = arcpy.da.InsertCursor(newFC, ['SHAPE@'])
    insertCursor.insertRow([row[0]])
    del insertCursor

del dataCursor

This loops through your file and creates a new shapefile for every record using a field value as the new shapefile's name using insertCursor. Using WKT isn't necessary unless you needed to for some reason.

  • I knew I could do this, but i was wondering if there was a quicker easier way. Like put my row into a make feature layer. using WKT is something I was just curious about using to make a Polygon. – ziggy Oct 5 '16 at 18:09

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.