Wednesday, 27 September 2017

line - Remove polyline vertex outside polygon in Arcpy


I am trying to remove polyline vertex which are outside the buffer polygon and not creates new vertex on intersection.


Wrong


I found a script which remove vertex but inside polygon. What is more it creates new vertex on intersection. It may help with this task.



Wrong output


import itertools, arcpy
arcpy.env.overwriteOutput = True
line_FC = arcpy.GetParameterAsText(0)
poly_FC = arcpy.GetParameterAsText(1)
output = arcpy.GetParameterAsText(2)

#clone input lines to memory for fast processing
temp = arcpy.CopyFeatures_management(line_FC, "in_memory/temp")


polygons = arcpy.CopyFeatures_management(poly_FC, arcpy.Geometry())

for poly in polygons:
with arcpy.da.UpdateCursor(temp, ["SHAPE@"]) as uCursor:
for line in uCursor:
line = line[0]
diff = line.difference(poly)
#if the two lines are not equal, that means it intersected the polygon
if not line.equals(diff):
#the result of geometry.difference() is a multipart line of only those

#parts that lie outside the polyon
parts = diff.getPart()

#if parts is empty that means the line is completely within the polygon
#i.e., no difference
if parts:
#We'll need to "join" the end of part1 to the beginning of part2
#so we'll just flatten the list of lists
joined = list(itertools.chain.from_iterable(parts))


#and create a new polyline object to update the shape
poly_trimmed = arcpy.Polyline(arcpy.Array(joined))
uCursor.updateRow([poly_trimmed])

arcpy.CopyFeatures_management(temp, output)

Is there a way to do it in arcpy? I am using ArcGIS Pro 1.3.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...