Get the distance of an existing point on a line

This is similar to generating chainage along a line, in the sense that it is the exact opposite. Instead of creating point coordinates along a line feature using a known distance, I want to determine the distance an existing point coordinate is from the start point of a line feature, along that line feature.

To Python!

# @date 06/03/2015
# @author Cindy Williams
# Returns the distance a point is along a line. Assumes that the
# existing point and line feature classes have the same spatial reference,
# and the points are snapped to the line.
# For use as a standalone script.
import arcpy
arcpy.env.workspace = r"C:\Some\Arb\Folder\work.gdb"
# Input variables
fc_line ="ftr_line")
fc_pnt ="ftr_point")
# Get line geometry – assumes only one feature in feature class
polyline = arcpy.da.SearchCursor(fc_line, "SHAPE@").next()[0]
# Loop over the point feature class
with arcpy.da.SearchCursor(fc_pnt, "SHAPE@") as cursor:
for row in cursor:
# Get the percentage the current point is along the line
perc = polyline.queryPointAndDistance(row[0], True)[1]
# Calculate the percentage distance in metres
dist = polyline.getLength()*perc
print("% along line: {0}%\tDistance along line: {1}m".format(round(perc*100, 2), round(dist, 2)))
print("Script complete.")

This marks the first time I’m embedding a Gist in WordPress since I’m busy moving all my stuff there. The idea for this script has been percolating in my head for a few months but I could never get it quite right. Now I’m glad it has come together, based off an idea sparked by this GeoNet thread.

Leave a Reply

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

You are commenting using your 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.