HOW TO

Add line vertices at fixed intervals using ArcPy in ArcGIS Pro

Last Published: November 18, 2024

Summary

In ArcGIS Pro, adding line vertices at fixed intervals using ArcPy significantly improves spatial data precision and time management. This method automates the process of creating line vertices at the intersection of point features, which are generated using the Generate Points Along Line tool. The automation produces more detailed line geometry while maintaining consistent vertex distances, which are useful for spatial queries and analysis.

Browse this article for the workflow to add line vertices at fixed intervals using ArcPy in ArcGIS Pro.

Procedure

  1. Open the project in ArcGIS Pro.
  2. Create point features along the line feature at intervals using the Generate Points Along Lines tool. In this example, point features are created at 100-meter intervals.
  3. Open the Python window to add line vertices to the point features using ArcPy and run the following script.
Note:
The indentations must be maintained in the script. They specify which code block the script resides in.
    1. Import the necessary module.
import arcpy
    1. Specify the required feature layers.
line_layer = r"<location_path>\<name>.gdb"
point_layer = r"<location_path>\<name>.gdb"
    1. Start a loop to search for all available features.
with arcpy.da.UpdateCursor(line_layer, ['SHAPE@']) as lineCursor:
    for row in lineCursor:
        geo = row[0]
    1. Within the loop, identify the selected points along the line feature. The numbers can be zero or more.
        arcpy.management.SelectLayerByLocation(point_layer, "INTERSECT", geo, None, "NEW_SELECTION")
        res = arcpy.management.GetCount(point_layer)
        n = int(res.getOutput(0))
    1. Within the same loop, start a logic statement to specify the condition of at least one intersecting point.
        if n > 0:
            point_List = []
            with arcpy.da.SearchCursor(point_layer, ['SHAPE@']) as pointCursor:
                for pointRow in pointCursor:
                    point_List.append(pointRow[0])
    1. Within the logic code block, convert the line to point features.
            lineArr = geo.getPart(0)
            for p in lineArr:
                point_List.append(arcpy.PointGeometry(p))
    1. Specify the position of the point features along the line feature.
            pointDict = {}
            for p in point_List:
                poin = geo.measureOnLine(p, True)
                pointDict[poin] = p
    1. Create a new polyline directly from sorted centroids.
            centroid_points = [pointDict[key].centroid for key in sorted(pointDict.keys())]
            newLine = arcpy.Polyline(arcpy.Array(centroid_points))
    1. Update the line geometry with the new polyline.
            row[0] = newLine
            lineCursor.updateRow(row)
    1. Place the cursor at the end of the script and press Enter twice to run the script.

The code below demonstrates the full working script.

import arcpy

line_layer = r"C:\User\Desktop\Testfolder\test.gdb\testline"
point_layer = r"C:\User\Desktop\Testfolder\test.gdb\testpoint"

with arcpy.da.UpdateCursor(line_layer, ['SHAPE@']) as lineCursor:
    for row in lineCursor:
        geo = row[0]

        arcpy.management.SelectLayerByLocation(point_layer, "INTERSECT", geo, None, "NEW_SELECTION")
        res = arcpy.management.GetCount(point_layer)
        n = int(res.getOutput(0))
        if n > 0:
            point_List = []
            with arcpy.da.SearchCursor(point_layer, ['SHAPE@']) as pointCursor:
                for pointRow in pointCursor:
                    point_List.append(pointRow[0])
            
            lineArr = geo.getPart(0)
            for p in lineArr:
                point_List.append(arcpy.PointGeometry(p))

            pointDict = {}
            for p in point_List:
                poin = geo.measureOnLine(p, True)
                pointDict[poin] = p

            centroid_points = [pointDict[key].centroid for key in sorted(pointDict.keys())]
            newLine = arcpy.Polyline(arcpy.Array(centroid_points))

            row[0] = newLine
            lineCursor.updateRow(row)

The map shows the line feature with vertices added at fixed intervals.

Image of the line vertices at fixed intervals

Article ID: 000033851

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 3
  • ArcGIS Pro 3 2

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options