ArcPy脚本——批量Shp合并脚本

本文介绍了一种使用Python和ArcPy库批量合并同一工作空间下所有子目录中的shp文件的方法。通过创建一个字典来存储唯一shapefile的名称及其对应的完整路径,然后将这些文件合并到指定的输出文件夹中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下代码是使用ArcToolBoxs工具运行的,如果需要直接在脚本中运行,修改接受参数方式即可。

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Created on: 2018-08-18 
#   (generated by GL:qq,695396984)
# Description: 
# ---------------------------------------------------------------------------
#shp文件夹
workspace = arcpy.GetParameterAsText(0)
#输出文件夹
output_folder = arcpy.GetParameterAsText(1)
arcpy.env.workspace = workspace
Dict = {}
 
#"walk" through subfolders and below, and drop all contents into the empty dictionary
#unique shapefile names as keys, and with their file path as values.
for root, dirs, files in os.walk(workspace):
    for dir in dirs:
        arcpy.env.workspace = os.path.join(root,dir)
        for fc in arcpy.ListFeatureClasses():
            if not fc in Dict:
                Dict[fc] = []
                Dict[fc].append(os.path.join(root,dir,fc))
            else:
                Dict[fc].append(os.path.join(root,dir,fc))
 
#loop through each key and merge them together
for key in Dict:
    output = os.path.join(output_folder,key[:-4]) + ''
    arcpy.Merge_management(Dict[key], output)
    print output + " created"
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值