HOW TO

Place lines from points in a feature class to a single point in another feature class using ArcPy.

Last Published: March 8, 2023

Summary

In ArcGIS Pro, point features can be connected with line features using the Points To Line tool. In some instances, there is a need to place connection lines from a group of point features to a selected point in a different point feature class. However, by using the Points To Line tool, the generated lines only connect to the points from the same point feature class as shown in the image below.

The output after using the Points To Line tool

The following procedure describes how to programmatically connect points in a feature class to a single point feature in another feature class with lines using ArcPy.

Procedure

  1. In ArcGIS Pro, open the map containing the point and line feature classes.
  2. Select a point feature on the map. Refer to ArcGIS Pro: Select features interactively for more information.
  3. Open the Python window. Refer to ArcGIS Pro: Python window for more information.
  4. Run the following script:
    1. Import the necessary modules and set names for the necessary points and line feature layers.
import arcpy

<VariableName1> = "<PointName1>"
<VariableName2> = "<PointName2>"
<VariableName3> = "<LineName>"
  1. Select one point from <VariableName1> and one or more points from <VariableName2> in the map.
assert arcpy.management.GetCount(points_layer1)[0] == "1", "select exactly 1 point from <VariableName1>"
assert int(arcpy.management.GetCount(points_layer1)[0]) > 0, "select > 0 point(s) from <VariableName2>"
  1. Get the coordinates of the one selected point in <VariableName1>.
with arcpy.da.SearchCursor(<VariableName1>, ["SHAPE@XY"]) as cursor:
  for row in cursor:
    end_x, end_y = row[0]
    end_point = arcpy.PointGeometry(arcpy.Point(float(end_x), float(end_y)))
  1. Create a new feature class to store the lines if needed.
if not arcpy.Exists(VariableName3):
  arcpy.management.CreateFeatureclass(arcpy.env.workspace, "<LineName>", "POLYLINE")
  1. Create an array to store the line geometry and run the arcpy.da.SearchCursor() function.
line_array = arcpy.Array()

with arcpy.da.SearchCursor(<VariableName2>, ["SHAPE@XY"]) as cursor:
  for row in cursor:
    start_x, start_y = row[0]
    start_point = arcpy.PointGeometry(arcpy.Point(float(start_x), float(start_y)))
  1. Append the start and end points to the line array.
line_array.append(start_point.centroid)
line_array.append(end_point.centroid)
  1. Create a polyline object from the line array.
line = arcpy.Polyline(line_array)
line_array.removeAll()
  1. Use the insert cursor to insert the line into the lines feature class.
with arcpy.da.InsertCursor(<VariableName3>, ["SHAPE@"]) as icursor:
  icursor.insertRow([line])

  1. Add the lines layer to the map to refresh the display. Press the Enter key twice.
arcpy.management.MakeFeatureLayer(<VariableName3>,"<LineName>")

The code block below demonstrates the full script.

import arcpy

points_layer1 = "facilities"
points_layer2 = "pts"
lines_layer = "lines"

assert arcpy.management.GetCount(points_layer1)[0] == "1", "select exactly 1 point from points_layer1"
assert int(arcpy.management.GetCount(points_layer1)[0]) > 0, "select > 0 point(s) from points_layer2"

with arcpy.da.SearchCursor(points_layer1, ["SHAPE@XY"]) as cursor:
  for row in cursor:
    end_x, end_y = row[0]
    end_point = arcpy.PointGeometry(arcpy.Point(float(end_x), float(end_y)))

if not arcpy.Exists(lines_layer):
  arcpy.management.CreateFeatureclass(arcpy.env.workspace, "lines", "POLYLINE")

line_array = arcpy.Array()

with arcpy.da.SearchCursor(points_layer2, ["SHAPE@XY"]) as cursor:
  for row in cursor:
    start_x, start_y = row[0]
    start_point = arcpy.PointGeometry(arcpy.Point(float(start_x), float(start_y)))
   
    line_array.append(start_point.centroid)
    line_array.append(end_point.centroid)

    line = arcpy.Polyline(line_array)
    line_array.removeAll()
   
    with arcpy.da.InsertCursor(lines_layer, ["SHAPE@"]) as icursor:
      icursor.insertRow([line])
     
arcpy.management.MakeFeatureLayer(lines_layer,"lines")

The image below shows line features connecting a group of point features to a selected point feature in another feature class.

Connected lines from a group of point features to a different selected point feature class

Article ID: 000029537

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 0
  • ArcGIS Pro 2 8 x
  • ArcGIS Pro 2 7 x
  • ArcGIS Pro 2 x

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