Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

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
share|improve this question

1 Answer 1

up vote 4 down vote accepted

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/

share|improve this answer
    
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 at 6:54
    
row is a cursor object (arcpy.da.searchcursor) see edits. –  Michael Miles-Stimson May 15 at 21:33
    
Thanks a lot, Michael. I've got it working with some tweaks. –  Matt May 19 at 9:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.