由于面状水系可能存在多条中间线,因此批量提取时需要使用 ArcGIS 中的 Feature To Line 工具结合 Python 循环和游标来完成。
以下是代码:
import arcpy
import os
# 设置输入输出路径和文件名
input_folder = r"C:\data\river_polygons"
output_folder = r"C:\data\river_midlines"
# 创建输出文件夹
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 遍历输入文件夹中所有 Shapefile 文件
for _, _, files in os.walk(input_folder):
for file in files:
if file.lower().endswith(".shp"):
# 获取当前文件路径和名称
input_shapefile = os.path.join(input_folder, file)
# 在提取线之前,删除现有的缓存文件 (如果已存在)
arcpy.Delete_management("in_memory")
# 根据面状水系区域生成几何网络
arcpy.Create geometric network(流体模拟)
network_path = "in_memory/network"
arcpy.FeatureClassToFeatureClass_conversion(input_shapefile, "in_memory", "polygons_for_network")
arcpy.CreateGeometricNetwork_management(network_path, "River", "SIMPLE_JUNCTION", "#", "#", "#", "#")
arcpy.AddFeatureClassToGeometricNetwork_management(network_path, "River", "polygons_for_network", "#", "#")
arcpy.BuildGeometricNetwork_management(network_path)
# 取得面状水系中心线
centerlines = []
with arcpy.da.SearchCursor(input_shapefile, ["OID@", "SHAPE@"]) as cursor:
for row in cursor:
# 使用 Feature To Line 工具计算中心线
input_polyline_feature = "in_memory/polyline_feature"
arcpy.FeatureToLine_management(row[1], input_polyline_feature, "#", "NO_ATTRIBUTES")
arcpy.MergeDividedRoads_cartography(input_polyline_feature, "#", input_polyline_feature + "_merged")
# 向列表追加中心线
centerlines.append((row[0], input_polyline_feature + "_merged"))
del cursor
# 将所有中心线融合成一个图层并保存到输出文件夹中
output_shapefile = os.path.join(output_folder, "midlines_" + file)
arcpy.Merge_management([i[1] for i in centerlines], output_shapefile)
with arcpy.da.UpdateCursor(output_shapefile, ["PolyID"]) as cursor:
for row in cursor:
# 更新新的线性要素 ID 与原始面状水系多边形相关
row[0] = [i[0] for i in centerlines if i[1].endswith("_" + str(row[0]))][0]
cursor.updateRow(row)
del cursor
文章来源:https://www.toymoban.com/news/detail-495553.html
上述代码假设输入为包含多个面状水系区文章来源地址https://www.toymoban.com/news/detail-495553.html
到了这里,关于arcpy批量提取面状水系中间线的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!