Skip to content

Instantly share code, notes, and snippets.

@pyRobShrk
Last active October 12, 2018 20:13
Show Gist options
  • Save pyRobShrk/3ba95e95ba8da502c9d060bc0e2e2a73 to your computer and use it in GitHub Desktop.
Save pyRobShrk/3ba95e95ba8da502c9d060bc0e2e2a73 to your computer and use it in GitHub Desktop.
Quick function (arcpy) to remove redundant vertices from polyline layer
# Remove duplicate vertices from lines
# A point is considered duplicate if it has the same XY coordinates within a tolerance (precision)
# The precision is an integer number of decimal places
# for X,Y in lat, long, 5 decimals is 1.1 meters and 6 is 11 cm
def removeDupeVertices(feature, precision):
fields = ['SHAPE@'] + [f.name for f in arcpy.ListFields(feature)]
sr = arcpy.Describe(feature).SpatialReference
upd = arcpy.da.UpdateCursor(feature,fields)
orphanList = []
for row in upd:
changed = False
lines = []
try:
for ln in row[0]:
i = 0
for pt in ln[1:]:
if ((round(pt.X,precision) == round(ln[i].X,precision)) and
(round(pt.Y,precision) == round(ln[i].Y,precision))):
ln.remove(i+1)
changed = True
else:
i += 1
lines.append(ln)
if len(lines) > 1:
lines = arcpy.Polyline(arcpy.Array(lines), sr)
else:
lines = arcpy.Polyline(lines[0], sr)
row[0] = lines
if changed:
upd.updateRow(row)
if row[1] % 1000 == 0:
print ('updated row %s' % row[1])
except:
orphanList.append(row[1])
print ('row %s does not have geometry' % row[1])
return orphanList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment