2

I am trying to create a polygon from polylines. I've created this script but it gives errors of Arrays.

import arcpy
polyline=r'C:\temp\temp_lines.shp'
Output_polygon=r'C:\temp\temp_polygon.shp'
polygonArray = arcpy.Array()
polygonArray.add(polyline)
cursor = arcpy.InsertCursor(Output_polygon)
row = cursor.newRow()
polygon = arcpy.Polygon(polygonArray, "")
row.SHAPE = polygon
cursor.insertRow(row)
del row
del cursor
7

You need to break it down to points if they're good points and reconstruct. Polylines are made from paths, polygons are made from rings. Although they are created in a similar way they are not compatable, hence your error.

Go through each point on the line adding a point to your output array and then insert.

here's a post that might help Get all the points of a polyline

This should work, though it's only a fragment... you have to set your own FeatureClass and make an insert cursor to accept the polygon:

rows = arcpy.da.SearchCursor(FeatureClass)
desc = arcpy.Describe(FeatureClass)
shapefieldname = desc.ShapeFieldName

for row in rows:
    feat = row.getValue(shapefieldname) # input line
    PArray = arcpy.Array() # new polygon
    partnum = 0
    for part in feat:
        for pnt in feat.getPart(partnum):
            PArray.add(arcpy.Point(pnt.X,pnt.Y))
        # you will need to check that the first and last point are the same
        partnum += 1
    OutPoly = arcpy.Polygon(PArray) # now it's a polygon

Have a read of these:

Reading Geometries http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001t000000

Writing Geometries http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Writing_geometries/002z0000001v000000/

  • Thanks Michael. I tested the code and it gave this error: feat = row.getValue(InFc) # input line NameError: name 'row' is not defined – Matt May 15 '14 at 6:54
  • row is a cursor object (arcpy.da.searchcursor) see edits. – Michael Stimson May 15 '14 at 21:33
  • Thanks a lot, Michael. I've got it working with some tweaks. – Matt May 19 '14 at 9:09
  • I just tested this and it worked well for me. I populated a list with OutPoly and used that in an insert cursor to create my polygons feature class. Define a list before the loop, then at the end say OutPolyList.append(OutPoly). – jbalk Oct 20 '16 at 4:50

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.