Skip to content

Instantly share code, notes, and snippets.

@cindygis
Created March 6, 2015 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cindygis/6498f7f4be235084d9fd to your computer and use it in GitHub Desktop.
Save cindygis/6498f7f4be235084d9fd to your computer and use it in GitHub Desktop.
Returns the distance a point is along a line.
#
# @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 = arcpy.management.MakeFeatureLayer("ftr_line")
fc_pnt = arcpy.management.MakeFeatureLayer("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.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment