1

I have a polyline feature class and a point feature class, they are both in the same projection. The points have been snapped to the line and I am attempting to measure the distance along the line from the beginning of the line to each point on the line, and output that distance in a field on the point. This is the code I am using:

import arcpy
# Get input parameters
polyline = arcpy.GetParameterAsText(0)
point = arcpy.GetParameterAsText(1)
output_field = arcpy.GetParameterAsText(2)
# Function to measure distance along the polyline and update the point feature class
def measure_distance_along_polyline(polyline_fc, point_fc, distance_field):
    # Create a dictionary to store the polyline features
    polyline_dict = {}
    with arcpy.da.SearchCursor(polyline_fc, ["OID@", "SHAPE@"]) as cursor:
        for row in cursor:
            polyline_dict[row[0]] = row[1]
    # Update the point feature class with measured distances
    with arcpy.da.UpdateCursor(point_fc, ["SHAPE@", distance_field]) as cursor:
        for row in cursor:
            point_geom = row[0]
            # Get the associated polyline feature
            polyline_oid = point_geom.firstPoint.ID
            polyline = polyline_dict.get(polyline_oid)
            if polyline:
                # Measure distance from the beginning of the polyline to the point
                distance = polyline.measureOnLine(point_geom, False)
                row[1] = distance
                cursor.updateRow(row)
# Call the function
measure_distance_along_polyline(polyline, point, output_field)

This is what the feature classes look like: enter image description here

I run the script as a tool and set the parameters to the polyline, point, and field. The first time I ran the script, it worked fine, but every time I have tried to run the script since, it appears to run successfully with no errors, but it does not change the output field. The output field remains Null.

How do I get it to work?

1
  • I would speculate the source of the problem is in this line polyline_oid = point_geom.firstPoint.ID. It's unusual that the ID property of the geometry is set and there is nothing in your question that indicates that it has been done. So when you query your dictionary for the polyline you are probably returning null. Normally one would spatially join the polyline ID to the point and that becomes an attribute of the point feature, not the ID property of the geometry.
    – Hornbydd
    Commented May 24, 2023 at 15:54

3 Answers 3

0

firstPoint.ID is always 0, unless you did something very fancy creating these points. Your script might work only for first polyline IF it is stored in shapefile.

So, run Near tool between lines and points and change

["SHAPE@", distance_field]

to:

["SHAPE@", distance_field, "NEAR_FID"]

Change:

polyline_oid = point_geom.firstPoint.ID

to:

polyline_oid = row[2]
0

I have performed similar in the past and have updated your code with snippets and commented. There are probably several ways to complete this task.

import arcpy
# Get input parameters
polyline = arcpy.GetParameterAsText(0)
point = arcpy.GetParameterAsText(1)
output_field = arcpy.GetParameterAsText(2)

# Function to measure distance along the polyline and update the point feature class
def measure_distance_along_polyline(polyline_fc, point_fc, distance_field):
    ## this will hold the OID of the point (key) and the OID of the line (value)
    points_dict = {}

    ## generate a near table to help populate the points_dict
    near_tbl = arcpy.GenerateNearTable_analysis(polyline_fc, point_fc, "in_memory\\near_tbl",
    search_radius=0, location="LOCATION", closest="ALL")

    ## for each near table entry
    with arcpy.da.SearchCursor(near_tbl, ["NEAR_FID", "IN_FID"]) as cursor:
        for row in cursor:
            ## if the NEAR_FID (point) not in the dictionary add it with IN_FID (linear)
            ## as the value
            if row[0] not in points_dict:
                points_dict[row[0]] = row[1]


    # Create a dictionary to store the polyline features
    polyline_dict = {}

    with arcpy.da.SearchCursor(polyline_fc, ["OID@", "SHAPE@"]) as cursor:
        for row in cursor:
            polyline_dict[row[0]] = row[1]


    # Update the point feature class with measured distances
    with arcpy.da.UpdateCursor(point_fc, ["OID@", "SHAPE@", distance_field]) as cursor:
        for row in cursor:
            ## the point geometry
            point_geom = row[1]

            ## Get the associated polyline feature for the point from the points_dict
            try:
                polyline_oid = points_dict[row[0]]
            except KeyError:
                ## the point did not intersect the line or was out of the search radius distance
                ## move on to the next point
                continue

            polyline = polyline_dict.get(polyline_oid)
            if polyline:
                # Measure distance from the beginning of the polyline to the point
                distance = polyline.queryPointAndDistance(point_geom)[1]
                row[2] = distance
                cursor.updateRow(row)

# Call the function
measure_distance_along_polyline(polyline, point, output_field)
0

I could not get Clubdebambos's script to work either. In the end, I included in my model the Create Routes tool, then Locate Features Along Routes, then Join Field to connect the output table to the point feature class. It's not as clean as I like it, there is also several Calculate Field tools, so the model can be run multiple times on the same feature class, but overall it works pretty well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.