Monday, December 17, 2012

Finding Distance Along A Line

The arcpy polyline geometry object has a very helpful method called positionAlongLine (value, {use_percentage}).  This function finds either a distance (in the feature's coordinate system) or the percentage from the line's starting location.  It then returns a PointGeometry object that can be used for further analysis, or that can be saved out as a feature class using the CopyFeatures tool.  If you use the optional use_percentages, false means it's a distance, true means it is a percentage with values between 1-100.  If a value is great than the length or over 100 % then the last point is used.  If values are less than 0 or less than 0% then the first point is used as well.

Assuming you have a line feature class, just create a search cursor and begin accessing your locations.  

fc = r"c:\temp\lines.shp" 
with da.SearchCursor(fc, ["SHAPE@"] as cursor:
    for row in cursor:
       print row[0].positionAlongLine(10, True)    
       print row[0].positionAlongLine(10, False)

No comments: