arcpy批量提取面状水系中间线

由于面状水系可能存在多条中间线,因此批量提取时需要使用 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
 

上述代码假设输入为包含多个面状水系区

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

认真学GIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值