I have a polyline shapefile and a polygon shapefile. I need to add vertices to the polylines where they cross polygon boundaries. The polyline length must stay the same, so the Intersect tool doesn't work.
What tool should I use? I just want to add vertices and I will be performing this for dozens of datasets.
EDIT: Below are two attempts that don't work. Code & expected behavior identified.
Base-Case to Check Expected Behavior:
outpath = r'C:\myfolder'
shp_input_lines = r'C:\myfolder\original_lines.shp'
shp_input_polygons = r'C:\myfolder\original_polygons.shp'
# make sure there are no multipart gremlins hidden in here
clean_lines = outpath + '\\' + '_clean_lines.shp'
arcpy.MultipartToSinglepart_management(shp_input_lines, clean_lines)
# add "my_id" field
arcpy.AddField_management(clean_lines, "my_id", "SHORT")
counter = 1
with arcpy.da.UpdateCursor(clean_lines, ["my_id"]) as cursor:
for row in cursor:
row[0] = counter
cursor.updateRow(row)
counter += 1
output1 = outpath + '\\' + '_clean_lines_intersected.shp'
arcpy.Intersect_analysis([clean_lines, shp_input_polygons], output1)
output2 = outpath + '\\' + '_clean_lines_intersected_vertices.shp'
arcpy.FeatureVerticesToPoints_management(output1, output2, point_location="ALL")
CHECKS, this is basis for expected behavior:
- number of lines in "clean_lines" = 3123
- number of points in "output2" = 14173
Try 1, Failed: Intersect then Dissolve
# using output1 from above Base-Case
output3 = outpath + '\\' + '_clean_lines_intersected_dissolved.shp'
arcpy.Dissolve_management(output1, output3, dissolve_field="my_id", multi_part="SINGLE_PART", unsplit_lines="DISSOLVE_LINES")
# also tried with unsplit_lines="UNSPLIT_LINES" made no difference
output4 = outpath + '\\' + '_clean_lines_intersected_dissolved_vertices.shp'
arcpy.FeatureVerticesToPoints_management(output3, output4, point_location="ALL")
CHECKS:
- number of lines in "output3" = 3123 (good)
- number of points in "output4" = 13846 (bad; expected 14173)
Try 2, Failed: Integrate polyline and polygon files
# using clean_lines from above Base-Case
cleancopy = outpath + '\\' + '_clean_lines2.shp'
arcpy.CopyFeatures_management(clean_lines, cleancopy)
arcpy.Integrate_management([cleancopy, shp_input_polygons], "0.1 feet")
CHECKS:
- number of lines in "cleancopy" = 3120 (bad; expected 3123)