Create Line from Point to Nearest Point on a Line in ArcGIS Pro with ArcPy

Table of Contents

Introduction

A common workflow is to create a line from each point in a point dataset to the nearest corresponding point on a linear dataset.  

Leverage ArcPy for geospatial data management workflows within ArcGIS Pro. Learn the fundamentals of utilising ArcGIS Pro geoprocessing tools with ArcPy for data management, conversions, and analysis. This course is packed with amazing content that will help you discover the power of automating tasks within the ArcGIS Pro environment. Take your ArcPy skills from beginner to snake charmer. A little code goes a long way, a little ArcPy provides you with an in-demand skill. Sign up now for our highly rated course.

Custom Tool Syntax

The syntax for the Create Random Points tool is as follows…

				
					createLineFromPointToNearestLine(
    pt_features,
    ln_features,
    {max_distance},
    {create_new_fc},
    {out_feature_class}
)
				
			

We’re going to take a different direction from the Create Random Points syntax and use three required parameters; in_features, the features you wish to generate random points for, these can be Point, Polyline, or Polygon; out_feature_class, the output Point feature class to create containing the random points; and number_of_points, the number of points to generate for each feature from the in_features.

Import the ArcPy module

ArcPy is a Python site package that enables automation, analysis, and management of geographic data within the ArcGIS software environment. 

				
					import arcpy
				
			

Define the parameters

We define the parameter based on the syntax with to required paramaters; pt_features, ln_features, and three optional parameters; max_distance, create_new_fc, and out_feature_class.

				
					## input point feature class
pt_features = arcpy.GetParameterAsText(0)
 
## input linear feature class
ln_features = arcpy.GetParameterAsText(1)
 
## maximum distance between point and line to consider 
max_distance = arcpy.GetParameterAsText(2)
 
## Create a new linear feature class or not
create_new_fc = arcpy.GetParameterAsText(3)
 
## the output feature class if creating a new one
out_feature_class = arcpy.GetParameterAsText(4)
				
			

Required Python object for the tool

We only require one object to help us out with our custom workflow and that is the Spatial Reference System from the input ln_features.

				
					srs_id = arcpy.Describe(ln_features).spatialReference.factoryCode
				
			

Generate Near Table

Next, we use the Generate Near Table tool and store the output table in the memory workspace.

				
					near_tbl = arcpy.analysis.GenerateNearTable(
                in_features = pt_features,
                near_features = ln_features,
                out_table = "memory\\near_tbl",
                search_radius = max_distance,
                location = "LOCATION",
                angle = "NO_ANGLE",
                closest = "CLOSEST",
                closest_count = 0,
                method = "PLANAR"
)
				
			

Our Near Table has four fields of interest; FROM_X, FROM_Y, NEAR_X, NEAR_Y, that allow us to construct our lines.

The Main Event: Create the Lines from each Point

We can now use the XY to Line tool to create our new linear features. We will create these in the memory workspace which will give us flexibility to either create a new feature class or append the new lines into our existing linear feature class.

				
					near_lines = arcpy.management.XYToLine(
    in_table = near_tbl,
    out_featureclass = "memory\\near_lines",
    startx_field = "FROM_X",
    starty_field = "FROM_Y",
    endx_field = "NEAR_X",
    endy_field = "NEAR_Y",
    line_type = "GEODESIC",
    id_field = "NEAR_FID",
    spatial_reference = srs_id,
    attributes="NO_ATTRIBUTES"
)
				
			

Its good to clean up as we go, we no longer require the near table so let’s delete that from the memory workspace.

				
					arcpy.management.Delete(near_tbl)
				
			

Create a New Feature Class - Optional Parameter

If the user choose to create a new feature class we use the Export Features tool to commit the lines in memory to disk. Otherwise, we use the Append tool to add the new linear features into our existing linear feature class.

				
					if create_new_fc == "true":
    arcpy.conversion.ExportFeatures(
        in_features = near_lines,
        out_features = out_feature_class
    )
 
else:
        arcpy.management.Append(
        inputs=near_lines,
        target=ln_features,
        schema_type="NO_TEST"
    )
				
			

And of course, we clean up the memory workspace.

				
					arcpy.management.Delete(near_lines)
				
			

ArcPy for Data Management and Geoprocessing with ArcGIS Pro

Leverage ArcPy for geospatial data management workflows within ArcGIS Pro. Learn the fundamentals of utilising ArcGIS Pro geoprocessing tools with ArcPy for data management, conversions, and analysis. This course is packed with amazing content that will help you discover the power of automating tasks within the ArcGIS Pro environment. Take your ArcPy skills from beginner to snake charmer. A little code goes a long way, a little ArcPy provides you with an in-demand skill. Sign up now for our highly rated course.

Create the tool in ArcGIS Pro

Save your script and open up ArcGIS Pro. Right-click on your toolbox/toolset of choice and select New > Script. The New Script window will appear. In the General tab set Name to createLineFromPointToNearestLineLabel to Create Line from Point to Nearest Line, and the Description as per below or any description you feel is apt.

Set the Point Features with a Data Type of Feature Layer and set the Filter to Point geometry only (we are not considering Multipoint). Set the Line Features parameter with a Data Type of Feature Layer and set the Filter to Polyline. Set the Maximum Distance with a Data Type of Linear Unit, Type as Optional, and Filter to Meters and Kilometers (or your own choice.) For the Create new feature class? set the Data Type to Boolean and the Type as Optional. And for the Output Feature Class set the Data Type to Feature Class, the Type as Optional, the Direction as Output, and the Filter to Polyline.

In the Execution tab, click the folder icon in the top-right corner and add your saved Python script.

In the Validation tab, update as per below, we do not want the Output Feature Class parameter visible unless the Create new feature class? is checked.

				
					def initializeParameters(self):
    # Customize parameter properties. This method gets called when the
    # tool is opened.
    self.params[4].enabled = False
    return
 
def updateParameters(self):
    # Modify the values and properties of parameters before internal
    # validation is performed.
    if self.params[3].value:
        self.params[4].enabled = True
    else:
        self.params[4].value = ""
        self.params[4].enabled = False
    return
				
			

Take the tool for a spin and let me know what you think? You can download the tool and other custom tools over on this page. This tool is in the Custom Tools on a Basic License with ArcPy section.

Mastering ArcGIS Pro ArcPy Search, Insert, & Update Cursors

Unlock the full potential of ArcPy Cursors with our intensive and in-depth course. Designed for GIS professionals, analysts, and enthusiasts, this course delves into the intricacies of Search, Insert, and Update Cursors in the ArcPy library, empowering you to manipulate and manage spatial data with precision and efficiency.

ArcPy Cursors enable you to streamline your GIS workflows, automate repetitive tasks, and witness the full potential of spatial data manipulation in ArcGIS Pro.

All the code in one place

You can find the entire code workflow below with links to important components in the documentation that were used.

				
					import arcpy
 
################################################################################
## Esri Documentation
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/getparameterastext.htm
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/describe.htm
##  https://pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/generate-near-table.htm
##  https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/xy-to-line.htm
##  https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/delete.htm
##  https://pro.arcgis.com/en/pro-app/latest/tool-reference/conversion/export-features.htm
##  https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/append.htm
##
## ArcGIS Pro Version 3.2.2
##
################################################################################
 
################################################################################
## USER INPUTS
 
## input point feature class
pt_features = arcpy.GetParameterAsText(0)
## input linear feature class
ln_features = arcpy.GetParameterAsText(1)
## maximum distance to consider
max_distance = arcpy.GetParameterAsText(2)
## Create a new linear feature class or not
create_new_fc = arcpy.GetParameterAsText(3)
## the output feature class if creating a new one
out_feature_class = arcpy.GetParameterAsText(4)
 
################################################################################
## REQUIRED OBJECTS ############################################################
 
## srs id of the in_features so we can assign the output the same
srs_id = arcpy.Describe(ln_features).spatialReference.factoryCode
 
################################################################################
## GENERATE NEAR TABLE #########################################################
 
near_tbl = arcpy.analysis.GenerateNearTable(
                in_features = pt_features,
                near_features = ln_features,
                out_table = "memory\\near_tbl",
                search_radius = max_distance,
                location = "LOCATION",
                angle = "NO_ANGLE",
                closest = "CLOSEST",
                closest_count = 0,
                method = "PLANAR"
)
 
################################################################################
## CREATE LINES ################################################################
 
near_lines = arcpy.management.XYToLine(
    in_table = near_tbl,
    out_featureclass = "memory\\near_lines",
    startx_field = "FROM_X",
    starty_field = "FROM_Y",
    endx_field = "NEAR_X",
    endy_field = "NEAR_Y",
    line_type = "GEODESIC",
    id_field = "NEAR_FID",
    spatial_reference = srs_id,
    attributes="NO_ATTRIBUTES"
)
 
################################################################################
## CLEANUP MEMORY ##############################################################
 
arcpy.management.Delete(near_tbl)
 
################################################################################
## CREATE OUPUT FEATURE CLASS ##################################################
 
if create_new_fc == "true":
    arcpy.conversion.ExportFeatures(
        in_features = near_lines,
        out_features = out_feature_class
    )
 
################################################################################
## OR APPEND TO ORIGINAL LINEAR FEATURE CLASS ##################################
 
else:
        arcpy.management.Append(
        inputs=near_lines,
        target=ln_features,
        schema_type="NO_TEST"
    )
 
################################################################################
## CLEANUP MEMORY ##############################################################
 
arcpy.management.Delete(near_lines)
 
################################################################################
				
			

Leave a Comment

Your email address will not be published. Required fields are marked *