操作方法

操作方法:在 ArcGIS Pro 中使用 ArcPy 将现有点重新定位到具有共同属性的面中

Last Published: March 28, 2024

描述

在 ArcGIS Pro 中,在包含相似属性的面内放置点对于各种空间分析(例如空间聚合和数据集成)非常重要。 公共属性通常是指数据集内要素共享的字段或属性。

点要素将放置在面要素之外

本文提供了在 ArcGIS Pro 中使用 ArcPy 将点放置在具有公共属性的面中的工作流。

解决方案或解决方法

Note:
This workflow requires a full script to run in the ArcGIS Pro Python window. All the indents are to be kept as portrayed in the code block.
  1. 在 ArcGIS Pro 中,打开包含点和面要素图层的地图。
  2. 打开 Python 窗口。 有关详细信息,请参阅 ArcGIS Pro:Python 窗口
  3. 运行以下脚本:
Note:
This workflow creates a new point feature in the geodatabase that is not added into the map.
  1. 导入必要的模块并创建用户定义的函数,以创建用于重新定位的新参考点要素。
import arcpy
import os

def place_points_inside_polygons(input_polygon_fc, output_point_fc, common_attribute_field):
    arcpy.management.CreateFeatureclass(
        out_path=os.path.dirname(output_point_fc),
        out_name=os.path.basename(output_point_fc),
        geometry_type="POINT",
        spatial_reference=input_polygon_fc
    )
  1. 将公共属性字段添加至参考点要素类。
    arcpy.management.AddField(output_point_fc, common_attribute_field, "TEXT")
  1. 遍历输入要素类中的每个面,计算每个面内的质心点,然后将点要素插入到面要素中,该点要素与相应面要素具有相同的公共属性值。
    with arcpy.da.InsertCursor(output_point_fc, ["SHAPE@", common_attribute_field]) as cursor:
        with arcpy.da.SearchCursor(input_polygon_fc, ["SHAPE@", common_attribute_field]) as search_cursor:
            for row in search_cursor:
                polygon_geom = row[0]
                common_attribute_value = row[1]
                point_inside_polygon = polygon_geom.centroid
                cursor.insertRow([point_inside_polygon, common_attribute_value])
  1. 设置输入参数。 将 <pathToPolygonFc> 和 <pathToPointFc> 分别替换为面要素类和点要素类的路径。 将 <fieldName> 替换为公共属性字段名称。
input_polygon_fc = r"<pathToPolygonFc>"
output_point_fc = r"<pathToOriginalPointFc>"
common_attribute_field = "<fieldName>"
  1. 调用该函数以将点放置在具有通用属性的面内。
place_points_inside_polygons(input_polygon_fc, output_point_fc, common_attribute_field)

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

import arcpy
import os

def place_points_inside_polygons(input_polygon_fc, output_point_fc, common_attribute_field):
    arcpy.management.CreateFeatureclass(
        out_path=os.path.dirname(output_point_fc),
        out_name=os.path.basename(output_point_fc),
        geometry_type="POINT",
        spatial_reference=input_polygon_fc
    )

    arcpy.management.AddField(output_point_fc, common_attribute_field, "TEXT")

    with arcpy.da.InsertCursor(output_point_fc, ["SHAPE@", common_attribute_field]) as cursor:
        with arcpy.da.SearchCursor(input_polygon_fc, ["SHAPE@", common_attribute_field]) as search_cursor:
            for row in search_cursor:
                polygon_geom = row[0]
                common_attribute_value = row[1]
                point_inside_polygon = polygon_geom.centroid
                cursor.insertRow([point_inside_polygon, common_attribute_value])

input_polygon_fc = r"C:\Users\Documents\ArcGIS\Projects\MyProject40\MyProject40.gdb\Polygon"
output_point_fc = r"C:\Users\Documents\ArcGIS\Projects\MyProject40\MyProject40.gdb\Pointsss"
common_attribute_field = "Distance"

place_points_inside_polygons(input_polygon_fc, output_point_fc, common_attribute_field)

下图显示了使用 ArcPy 放置在具有公共属性的面中的点。

这些点将放置在面要素中

文章 ID: 000032318

获得人工智能支持

使用 Esri Support AI Chatbot 快速解决您的问题。

立即开始聊天

相关信息

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

获取来自 ArcGIS 专家的帮助

联系技术支持部门

立即开始聊天

转至下载选项