3

I am trying to convert polygons to polylines in ArcGIS Pro using ArcPy. I do not have the license so I cannot use the tools that do the job. Due to the way things are set-up for now I cannot use QGIS or GRASS GIS modules. I am basically restricted to ArcPy and would prefer not to use other modules that have to be installed / imported to ensure functionality across different working stations.

The manual workflow as provided by Esri: https://support.esri.com/en/technical-article/000018461

I cannot find a way to populate my newly created Polyline feature with the data from the Polygons.

My function so far (I work with shapefiles):

def PolygonToLine(inFC, outPath, outFC_Name):

# Set local variables
   geometry_type = "POLYLINE"
   template = inFC
   has_m = "DISABLED"
   has_z = "DISABLED"
# Use Describe to get a SpatialReference object of input
   spatial_reference = arcpy.Describe(inFC).spatialReference

#Execute CreateFeatureclass
   arcpy.CreateFeatureclass_management(outPath, outFC_Name, geometry_type, template, has_m, has_z, spatial_reference)

#initiate output (note; shapefile)
   outFC = outPath+"/"+outFC_Name

#populate Polyline feature class
   arcpy.Copy_management(inFC, outFC)

The result of this is just a copy of my polygon features and an empty Polyline feature dataset. Any ideas?

2
  • 2
    Your Python indent scheme is invalid. I'd use a nested pair of DA cursors, converting the polygon into polyline via the Polygon.boundary() request.
    – Vince
    Commented Aug 11, 2020 at 15:13
  • 1
    I'd try something similar to what @Vince suggested but I'd first use arcpy.da.SearchCursor() and list comprehension to create a list of your polygon geometries, and then use arcpy.da.InsertCursor() to read that list and Polygon.boundary() to write the polygon boundaries as polylines.
    – PolyGeo
    Commented Aug 12, 2020 at 10:05

1 Answer 1

5

As suggested in the comments, try using cursors and the boundary method to extract polygon outlines as polylines:

arcpy.env.workspace = r'C:\GIS\data\testdata' #I have my polygon shapefile in this folder

polygon_fc = "ak_riks"
polygon_fields = [f for f in arcpy.ListFields(polygon_fc) if f.type not in ['Geometry','OID']]

#Create a polyline feature class and add all fields from polygon fc
polyline_fc = "ak_riks_lines"
arcpy.CreateFeatureclass_management(out_path=arcpy.env.workspace, out_name=polyline_fc, geometry_type='POLYLINE', 
                                    spatial_reference=polygon_fc)
for f in polygon_fields:
    arcpy.AddField_management(in_table=polyline_fc, field_name=f.name, field_type=f.type, 
                   field_precision=f.precision, field_scale=f.scale, field_length=f.length)

fieldlist = [f.name for f in polygon_fields]+['SHAPE@']
icur = arcpy.da.InsertCursor(polyline_fc, fieldlist) #To insert attributes and geometries

with arcpy.da.SearchCursor(polygon_fc, fieldlist) as cursor:
    for row in cursor:
        geom = row[-1].boundary()
        newrow = list(row[:-1])+[geom]
        icur.insertRow(newrow)
del(icur)

enter image description here

enter image description here

0

Your Answer

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

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