HOW TO

Detect line feature misalignments using ArcPy in ArcGIS Pro

Last Published: June 11, 2025

Summary

Line feature misalignments can be detected programmatically to ensure features such as roads, pipelines, or power lines follow intended paths or reference alignments. This article describes the workflow to detect line feature misalignments using ArcPy in ArcGIS Pro.

Procedure

Note:
This workflow requires a full script to run in the ArcGIS Pro Python window. The indents must be retained as portrayed in the code block.
  1. In ArcGIS Pro, open the map containing the line features.
  2. Open the Python window. Refer to ArcGIS Pro: Python window for more information.
  3. Run the following script:
    1. Define the feature class containing the line features.
line_feature_class = r"<pathToFeatureClass>"
    1. Set the reference alignment.
alignment_feature_class = r"<pathToFeatureClass>"
    1. Set the buffer distance to flag misalignments.
misalignment_threshold = <bufferDistance>
    1. Create a spatial reference for the analysis.
sr = arcpy.Describe(line_feature_class).spatialReference
    1. Use the Near tool to calculate the distance from each line to the nearest alignment.
arcpy.analysis.Near(line_feature_class, alignment_feature_class)
    1. Perform a spatial join to identify line features that are within the misalignment threshold.
join_result = arcpy.analysis.SpatialJoin(line_feature_class, alignment_feature_class, r"C:<outputShapeFile>", join_type="KEEP_COMMON")
    1. Identify misaligned lines by checking the near distance.
with arcpy.da.UpdateCursor(line_feature_class, ["SHAPE@", "NEAR_DIST", "OBJECTID"]) as cursor:
    for row in cursor:
        # NEAR_DIST field contains the distance to the alignment
        if row[1] > misalignment_threshold:
            print(f"Line {row[2]} is misaligned by {row[1]} meters.")

The code below demonstrates the full working script.

line_feature_class = r"C:\Users\Documents\ArcGIS\Projects\MyProject44\MyProject44.gdb\Linefeatureclass"

alignment_feature_class = r"C:\Users\Documents\ArcGIS\Projects\MyProject44\MyProject44.gdb\Line1"

misalignment_threshold = 5

sr = arcpy.Describe(line_feature_class).spatialReference

arcpy.analysis.Near(line_feature_class, alignment_feature_class)

join_result = arcpy.analysis.SpatialJoin(line_feature_class, alignment_feature_class, r"C:\Users\Documents\ArcGIS\Projects\MyProject44\Output\reference_line.shp", join_type="KEEP_COMMON")

with arcpy.da.UpdateCursor(line_feature_class, ["SHAPE@", "NEAR_DIST", "OBJECTID"]) as cursor:
  for row in cursor:
        if row[1] > misalignment_threshold:
            print(f"Line {row[2]} is misaligned by {row[1]} meters.")

The image below shows the list of the misaligned lines printed in the Python window.

The misaligned lines printed in the Python window

Article ID: 000035974

Software:
  • ArcGIS Pro

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

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options