操作方法

操作方法:在 ArcGIS Pro 中通过 ArcPy 按百分比分割面

Last Published: August 26, 2024

描述

ArcGIS Pro 允许使用诸如分割(分析)工具和修改要素窗格中的“分割”工具等工具将面要素分割为多条线段。 但是,提供的工具无法按百分比分割面。 请按照本文中提供的步骤,在 Python 窗口中按百分比分割面。

解决方案或解决方法

Note: 
Ensure the data added to the map and the map share the same projected coordinate system to avoid errors.
  1. 导入 ArcPy 模块并配置该脚本的环境。
Import arcpy
p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps("<Map_Name>")[0]
lyr = m.listLayers(0)
  1. 使用阵列参数指定面分割百分比。 在本示例中,将按 20%、30% 和 50% 来分割面。
splits = [20, 30, 50]
  1. 使用 SearchCursor 函数检索面和面范围属性。
with arcpy.da.SearchCursor("<Layer_Name>", ["SHAPE@"]) as pcursor:
   for prow in pcursor:
      polygon = prow[0]    # polygon to cut
      e = polygon.extent   # bounding extent of polygon
      print(e.XMin,e.YMin,e.XMax,e.YMax)
del pcursor
  1. 创建一个变量用于存储几何迭代精度值。
stepsize = 0.001
  1. 指定参数以设置面分割方向。
leftXstart = e.XMin
leftX = e.XMin + stepsize
ymax = e.YMax
ymin = e.YMin
  1. 根据在步骤 2 中设置的分割百分比,使用 cut() 函数遍历面剪切过程。
cutpoly = polygon
icursor = arcpy.da.InsertCursor("<Layer_Name>", ["SHAPE@"])
for i in splits[:2]:
   print(i)
   tol = 0
   while tol < i:
      # construct NS line
      pntarray = arcpy.Array()
      pntarray.add(arcpy.Point(leftX, ymax))
      pntarray.add(arcpy.Point(leftX, ymin))
      pline = arcpy.Polyline(pntarray,arcpy.SpatialReference(<WKID>))
      # cut polygon and get split-parts
      cutlist = cutpoly.cut(pline)
      tol = 100 * cutlist[1].area / polygon.area
      leftX += stepsize
      #print str(leftX) + ":" + str(tol)
   cutpoly = cutlist[0]
   icursor.insertRow([cutlist[1]])
# part 0 is on the right and part 1 is on the left of the split
  1. 插入最后一个分割余数。
icursor.insertRow([cutlist[0]])
del icursor

以下代码显示了完整的工作脚本:

Import arcpy
p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps("Map")[0]
lyr = m.listLayers(0)
splits = [20, 30, 50]
with arcpy.da.SearchCursor("testpolygon", ["SHAPE@"]) as pcursor:
   for prow in pcursor:
      polygon = prow[0]
      e = polygon.extent
      print(e.XMin,e.YMin,e.XMax,e.YMax)
del pcursor
stepsize = 0.001
leftXstart = e.XMin
leftX = e.XMin + stepsize
ymax = e.YMax
ymin = e.YMin
cutpoly = polygon
icursor = arcpy.da.InsertCursor("testpolygon", ["SHAPE@"])
for i in splits[:2]:
   print(i)
   tol = 0
   while tol < i:
      pntarray = arcpy.Array()
      pntarray.add(arcpy.Point(leftX, ymax))
      pntarray.add(arcpy.Point(leftX, ymin))
      pline = arcpy.Polyline(pntarray,arcpy.SpatialReference(3857))
      cutlist = cutpoly.cut(pline)
      tol = 100 * cutlist[1].area / polygon.area
      leftX += stepsize
   cutpoly = cutlist[0]
   icursor.insertRow([cutlist[1]])
icursor.insertRow([cutlist[0]])
del icursor

下图演示了在 ArcGIS Pro 中运行 Python 脚本后,按 20%、30% 和 50% 来分割面。

使用 ArcPy 按百分比分割面。

文章 ID: 000027999

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

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

下载 Esri 支持应用程序

获取来自 ArcGIS 专家的帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项