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
nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked May 14, 2014 at 1:33

1 Answer 1

8

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/

answered May 14, 2014 at 1:38
4
  • Thanks Michael. I tested the code and it gave this error: feat = row.getValue(InFc) # input line NameError: name 'row' is not defined Commented May 15, 2014 at 6:54
  • row is a cursor object (arcpy.da.searchcursor) see edits. Commented May 15, 2014 at 21:33
  • Thanks a lot, Michael. I've got it working with some tweaks. Commented May 19, 2014 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). Commented Oct 20, 2016 at 4:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.