HOW TO
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 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.
import arcpy <VariableName1> = "<PointName1>" <VariableName2> = "<PointName2>" <VariableName3> = "<LineName>"
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>"
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)))
if not arcpy.Exists(VariableName3): arcpy.management.CreateFeatureclass(arcpy.env.workspace, "<LineName>", "POLYLINE")
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)))
line_array.append(start_point.centroid) line_array.append(end_point.centroid)
line = arcpy.Polyline(line_array) line_array.removeAll()
with arcpy.da.InsertCursor(<VariableName3>, ["SHAPE@"]) as icursor: icursor.insertRow([line])
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.
Get help from ArcGIS experts
Download the Esri Support App