Somebody on the team was having trouble tracing streams due to breaks in the streams introduced when converting raster to feature.
So here is slick little solution which takes n paths in a line features and makes a 1 path (aka 1 part) out of it.
None of the vertices are moved. Where there were two paths separated by a gap you will have one path with no gap.
To work property the paths have to be pointed in the same direction and not be converging. To deal with that would require quite a bit more logic.
As is script modified data in place, so back up your data before using.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def connect_parts(fc): | |
""" | |
Takes each part of a line feature. If made of more than one path | |
(aka segment, aka part) connects them into a single path. | |
Vertices are unchanged. | |
Code authored on ArcGIS ver 10.2.2 | |
""" | |
import json | |
with arcpy.da.UpdateCursor(fc, "Shape@JSON") as uc: | |
for row in uc: | |
j = json.loads(row[0]) | |
paths = j['paths'] | |
if len(paths) > 1: | |
j['paths'] = [list(itertools.chain(*paths))] | |
uc.updateRow([json.dumps(j),]) |
I was constrained by having to use Arc 10.0 for a similar task, but I believe this script will do the same thing in version 10.0:
def connect_parts(fc):
desc = arcpy.Describe(fc)
shapefieldname = desc.ShapeFieldName
rows = arcpy.UpdateCursor(fc)
for row in rows:
feat = row.getValue(shapefieldname)
if feat.isMultipart == True:
pntArray = arcpy.Array()
for part in feat:
for pnt in part:
pntArray.add(pnt)
Shape = arcpy.Polyline(pntArray)
del pntArray
row.Shape = Shape
rows.updateRow(row)
del Shape
del rows