Splitting a line with a point

Recently I got an email where a colleague was trying to determine the distance a point on a line was from the beginning.  They were spinning their wheels looking at geometry operators and not seeing a solution.

First and foremost arcpy offers a broad broad collection of geoprocessing tools. What is perhaps less obvious is how those tools can be used directly with geometry objects. In the above scenario, using geometry as both input and output offers the solution to the problem. At a functional level, the Split Line at Points tool offers the solution. Being able to use it directly with geometry saves the hassle (and mess) of creating temporary datasets to work with.

import arcpy

def split_line_with_point(in_line, in_point, search_radius=0):
    """Splits a Polyline object with a PointGeometry object. Returns
    a list of two Polyline geometries. First line will include the
    starting point of the in_line. Curves are supported.

    Parameters:
    in_line: arcpy.Polyline
    in_point: arcpy.PointGeometry
    search_radius: string | float | int
        Use to split the lines based on its proximity to the point.
        Line will be split at the nearest location to the point.
        If search_radius is 0, the point must be on the line.
        Can be expressed as a number or linear unit.
    """

    split_lines = arcpy.management.SplitLineAtPoint(
        in_line,
        in_point,
        arcpy.Geometry(),
        search_radius)

    if len(split_lines) == 1:
        raise Exception('Line could not be split')
    else:
        return split_lines

So to answer the original question, to get the distance a point on a line is from the beginning, you could use the above function and then ask for the length property of the first Polyline returned in the list.

distance = split_line_with_point(line, point)[0].length
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s