3

I have multiple polylines that have three vertices. I need to delete the middle vertices on all of them. Besides doing this one at a time, is there a way to do this with either a tool or python? I tried the Edit Vertices editing tools by dragging a box over vertices but you can only do that for multiple vertices of the same feature, not multiple features at a time.

5

Here are the manual tool steps for deleting the middle vertex one feature at a time:

  1. Start edit session on layer and select a feature
  2. On edit toolbar click the Edit Vertices button
  3. Hover over middle vertex until mouse cursor changes and right click and select Delete Vertex option

Here is the arcpy code snippet for deleting middle vertex of a polyline feature that has only three vertices:

fc = arcpy.mapping.Layer('C:/Temp/Temp.gdb/myFc')
with arcpy.da.UpdateCursor(fc,["shape@"]) as cursor:
  for row in cursor:
    # loop through parts
    for part in row[0]:
      count = 0
        # loop through vertices
        for pnt in part:
          count = count + 1
          if count == 3:
            arr = row[0].getPart(0)
            arr.remove(1)
            newLine = arcpy.Polyline(arr)
            row[0] = newLine
            cursor.updateRow(row)
  • This returned an error but it is because after the "count = 0" there are unnecessary indentions in all subsequent lines. After deleting two spaces per line, it worked perfectly! Thank you. – Chris Feb 3 '17 at 22:01
1

Thanks for the solution @artwork21! For those wondering about situations where you may have line segments containing more than 3 vertices, the simplest solution I found was to just change the operator from "count == 3" to "count >= 3". Since vertex #1 will always be between the start (0) and end points (2 or greater) you can leave the argument "arr = remove(1)" as is. The For loop will continue to cycle through the rows in the feature class until the condition is false (aka. all rows have line segments containing 2 vertices).

import arcpy

fc = arcpy.mapping.Layer('C:/Temp/Temp.gdb/myFc')

with arcpy.da.UpdateCursor(fc,["SHAPE@"]) as cursor:
    for row in cursor:
        #loop through parts
        for part in row[0]:
            count = 0
            # loop through verticies
            for pnt in part:
                count = count + 1
                if count >= 3:
                    arr = row[0].getPart(0)
                    arr.remove(1)
                    newLine = arcpy.Polyline(arr)
                    row[0] = newLine
                    cursor.updateRow(row)

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.