Hello, I have a script that my intentions are for it to segment polylines into equal intervals with the last polyline as a remainder. Initially, I attempted to conduct this through GeneratePointsAlongLines at equal distance and then SplitLinesAtPoint functions but noticed that this produced segmented lines that had lengths that were inconsistent with the required length. Furthermore, I'm the only person at my company that has an Advanced Pro license and I would like to make this tool work for others.
My script is able to successfully iterate through the source polylines layer and add information to an output layer using cursors and segmentAlongLine method. Though the attributes table has the correct number of rows, the feature layer still only shows the original polyline when selected. I believe what is happening is that the segmented polyline doesn't actually exist. Any ideas on what I'm doing wrong? Thank You!
Below is my script:
import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\Users\roder\OneDrive\Documents\ArcGIS\Projects\PythonScripting\PythonScripting.gdb" # Change to getParameter
polylines = "CranePath_OnRoad"#Change to getParameter
spatial_ref = arcpy.Describe(polylines).spatialReference
output = "CranePath_OnRoad_Segmented" #Change to getParameter
interval_dist = 20 # feet, Change to get Parameter
#distance_interval_unit = getParameter
arcpy.AddField_management(polylines,"Line_Length","DOUBLE")
arcpy.management.CalculateGeometryAttributes(polylines, "Line_Length LENGTH_GEODESIC", "FEET_US")
mem_polyline = arcpy.CreateFeatureclass_management("in_memory", "mem_polyline", "POLYLINE", polylines, "DISABLED", "DISABLED", spatial_ref)
arcpy.AddField_management(mem_polyline,"LineID","LONG")
in_flds = ["OID@", "SHAPE@", "Line_Length"]
out_flds = ["SHAPE@", "Line_Length", "LineID"]
cnt_in = 0
cnt_out = 0
with arcpy.da.InsertCursor(mem_polyline,out_flds) as curs_out:
with arcpy.da.SearchCursor(polylines,in_flds) as curs_in:
for row_in in curs_in:
cnt_in += 1
polyline_in = row_in[1]
from_dist = 0
lineID = row_in[0]
max_dist = float(row_in[2])
if cnt_in % 100 == 0:
print("Processing line: {}, generated {} segments".format(cnt_in, cnt_out))
while from_dist <= max_dist:
cnt_out += 1
to_dist = from_dist + interval_dist
remainder_dist = max_dist - from_dist
segment = polyline_in.segmentAlongLine(from_dist,to_dist,False)
lst_out = list((segment,interval_dist,lineID))
curs_out.insertRow(lst_out)
from_dist += interval_dist
else:
cnt_out += 1
segment = polyline_in.segmentAlongLine(from_dist-interval_dist,max_dist,False)
lst_out = list((segment,remainder_dist,lineID))
curs_out.insertRow(lst_out)
mem_polyline_fl = arcpy.MakeFeatureLayer_management(mem_polyline, "Polylines_memory")
arcpy.CopyFeatures_management(mem_polyline_fl, output)
arcpy.Delete_management(mem_polyline)
arcpy.Delete_management(mem_polyline_fl)