利用arcpy实现多个GDB数据库批量合并为一个。
代码示例:
# -*- coding:utf-8 -*-
import arcpy
import os
import sys
import re
inputpath = arcpy.GetParameterAsText(0) # 获取输入文件夹
out = arcpy.GetParameterAsText(1) # 获取输出文件夹
tcmc = arcpy.GetParameterAsText(2)
savetcmc = arcpy.GetParameterAsText(3)
allPath = inputpath
pathList = []
outpath = out
dirs = os.listdir(allPath) # 获取输入文件夹下的所有文件名
for dir in dirs:
pathList.append(dir) # 输入文件夹下的所有文件名形成列表
#print(pathList)
pathMerge = [] # 定义合并路径
i = 0 # 定义计数变量
for gdbName in pathList: # 遍历gdb名称
if gdbName.endswith(".gdb" or ".mdb"):
arcpy.env.workspace = allPath + "\\" + gdbName # 设置工作环境
datasets = arcpy.ListDatasets() # 列出数据集
print (datasets)
print (arcpy.ListFeatureClasses(feature_dataset=datasets[0]))
if tcmc in arcpy.ListFeatureClasses(feature_dataset=datasets[0]): # 判断第一个数据集中是否存在地类名称的图层
i = i + 1
#print (i)
path = os.path.join(arcpy.env.workspace, datasets[0], tcmc) # 形成完整路径
pathMerge.append(path)
#print(pathMerge)
else:
arcpy.AddWarning(" 警告:{0} 中不存在名称为 {1} 的要素类,请留意!".format(gdbName, tcmc))
arcpy.AddMessage(" 共{0} 个图层正在合并……".format(i))
if i > 1:
arcpy.Merge_management(pathMerge, outpath + "\\" + savetcmc)
del pathMerge
else:
copyFc = pathMerge[0]
arcpy.CopyFeatures_management(copyFc, outpath + "\\" + savetcmc)
arcpy.AddMessage("合并成功,请关闭")