Segment line at chainage points using a table

I have a table containing a description field and an integer field which has values along the line. I need to split each line according to the values in the table.

#
# @date 04/08/2015
# @author Cindy Williams
#
# Creates segments along a selected line feature
# by splitting it according to a table of measures
# and saving it in a new feature class.
#
# For use as a standalone script.
#
import arcpy
arcpy.env.workspace = r"C:\Some\Arb\Folder\work.gdb"
selected_feature = "Line1"
selected_field = "Name"
qry = """ "{0}" = '{1}' """.format(selected_field, selected_feature)
# Get the selected geometry
line_geom = arcpy.da.SearchCursor("ftr_lines", "SHAPE@", qry).next()[0]
fields_fc = ["SHAPE@", "Description"]
cursor_ins = arcpy.da.InsertCursor("ftr_segments", fields_fc)
fields_tbl = ["Measure", "Description"]
start = 1 # Placeholder for previous row value
with arcpy.da.SearchCursor("tbl_measures", fields_tbl, qry) as cursor_sea:
for row in cursor_sea:
if start == 1:
start = 0
print("Handled first row")
else:
end = row[0]
segment_geom = line_geom.segmentAlongLine(start, end)
segment = [segment_geom, row[1]]
cursor_ins.insertRow(segment)
start = row[0]
del cursor_ins

segmentAlongLine requires a start and end value for where the line needs to be cut. The end value is the current row’s measure value, while the start value is the previous row’s end value. After trying to move the cursor back and forth, I decided the best way to achieve this would be to simply store the row’s value in the start variable.

The script inserts the segments into an existing line feature class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.