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

被折叠的 条评论
为什么被折叠?



