操作方法

操作方法:使用 ArcPy 将线从一个要素类中的点放置到另一个要素类中的单个点。

Last Published: March 8, 2023

摘要

在 ArcGIS Pro 中,可以使用点集转线工具将点要素与线要素连接起来。 在某些情况下,需要将一组点要素的连接线放置在不同的点要素类中的选定点。 但是,通过使用点集转线工具,生成的线仅连接到来自同一点要素类的点,如下图所示。

使用点集转线工具后的输出

以下过程介绍如何使用 ArcPy 以编程方式将要素类中的点连接到另一个包含线的要素类中的单个点要素。

过程

  1. 在 ArcGIS Pro 中,打开包含点和线要素类的地图。
  2. 在地图上选择一个点要素。 有关详细信息,请参阅 ArcGIS Pro:以交互方式选择要素
  3. 打开 Python 窗口。 有关详细信息,请参阅 ArcGIS Pro:Python 窗口
  4. 运行以下脚本:
    1. 导入必要的模块并为必要的点和线要素图层设置名称。
import arcpy

<VariableName1> = "<PointName1>"
<VariableName2> = "<PointName2>"
<VariableName3> = "<LineName>"
  1. 在地图中,从 <VariableName1> 中选择一个点,并从 <VariableName2> 中选择一个或多个点。
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. 获取 <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. 如果需要,创建一个新要素类来存储线。
if not arcpy.Exists(VariableName3):
  arcpy.management.CreateFeatureclass(arcpy.env.workspace, "<LineName>", "POLYLINE")
  1. 创建一个数组以存储线几何并运行 arcpy.da.SearchCursor() 函数。
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. 将起点和终点附加到线数组。
line_array.append(start_point.centroid)
line_array.append(end_point.centroid)
  1. 从线数组创建折线对象。
line = arcpy.Polyline(line_array)
line_array.removeAll()
  1. 使用插入光标将线插入到 lines 要素类中。
with arcpy.da.InsertCursor(<VariableName3>, ["SHAPE@"]) as icursor:
  icursor.insertRow([line])

  1. 将线图层添加到地图以刷新显示。 按两次 Enter 键。
arcpy.management.MakeFeatureLayer(<VariableName3>,"<LineName>")

以下代码块演示了完整脚本。

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")

下图显示了将一组点要素连接到另一个要素类中的所选点要素的线要素。

从一组点要素到其他所选点要素类的连接线

文章 ID: 000029537

接收通知并查找新问题或常见问题的解决方案

从我们全新的 AI 聊天机器人中获得简明答案和视频解决方案。

下载 Esri 支持应用程序

相关信息

发现关于本主题的更多内容

获取来自 ArcGIS 专家的帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项