Select to view content in your preferred language

Deleting a portion (part) of a multi-part line featue class

697
2
05-09-2014 08:39 AM
GerryGabrisch
Frequent Contributor
I have a line feature class with multi-part geometries.  I would like to cursor through the geometries and delete any parts that are not the first part.  Does anyone have any code to share?
Tags (2)
0 Kudos
2 Replies
JoshuaChisholm
Frequent Contributor
Hello Gerry,

This should work:
import arcpy
arcpy.env.overwriteOutput = True

inFC=r'C:\Path\To\MultiPartFeatureClass.shp'

rows = arcpy.da.UpdateCursor(inFC,("SHAPE@"))

for row in rows:
    geom=row[0].type.capitalize() #returns Point, Polygon, Polyline, or Multipoint.
    exec('row[0]=arcpy.'+geom+'(row[0].getPart(0))') #executes line for appropriate geometery
    rows.updateRow(row)
    del row
del rows


Sorry about the exec line, but I wanted the script to work for any geometry type.
0 Kudos
GerryGabrisch
Frequent Contributor
Thanks, hua17!
That is a more elegant solution than my hack as shown below. 

infc =r'C:\MultiPartFeatureClass.shp'
outfc = r'C:\FisrtPartFeatureClass.shp'
newarray = []
    for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@"]):
        partnum = 0
        for part in row[1]:
            if partnum == 0:
                thispart = []
                for pnt in part:
                    if pnt:
                        thispart.append([pnt.X, pnt.Y])
            partnum += 1
        newarray.append(thispart)
    features = []
    for feature in newarray:
        features.append(arcpy.Polyline(arcpy.Array([arcpy.Point(*coords) for coords in feature])))
    arcpy.CopyFeatures_management(outfc, SingleParts)
0 Kudos