之前做了合并多个数据库的脚本,原文如下
- 没有考虑数据库里面的独立要素类,也就是不在数据集里面的要素类
- 只能合并gdb数据库,没有考虑包含shp要素类的文件夹的合并
- 只有Python脚本,没有做成更易用的脚本工具
为此,改进了原来的脚本代码,并制作了脚本工具
Python脚本
import os
import arcpy
folderlist = arcpy.GetParameterAsText(0)
gdblist = folderlist.split(";")
print(gdblist)
arcpy.AddMessage(gdblist)
new_dir = arcpy.GetParameterAsText(1)
#复制第一个文件夹作为初始文件夹
arcpy.management.Copy(gdblist[0], new_dir)
workspaces = []
#获取初始文件夹内所有的文件夹路径
for filepath, dirnames, filenames in os.walk(new_dir):
workspaces.append(filepath)
for ws in workspaces:
arcpy.env.workspace = ws
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
#循环所有的待合并的文件夹
for i in range(1, len(gdblist)):
#获取待合并文件夹内同一位置的要素类的路径
name = ws.replace(new_dir, gdblist[i])+"\\"+fc
arcpy.AddMessage(name)
try:
arcpy.management.Append(name, fc, "NO_TEST")
except:
arcpy.AddMessage("请检查 "+gdblist[i]+" 的文件组织")
datasets = arcpy.ListDatasets()
for ds in datasets:
fcs = arcpy.ListFeatureClasses(feature_dataset=ds)
arcpy.env.workspace = ds
for fc in fcs:
for i in range(1, len(gdblist)):
name = ws.replace(new_dir, gdblist[i])+"\\"+fc
try:
arcpy.management.Append(name, fc, "NO_TEST")
except:
arcpy.AddMessage("请检查 "+gdblist[i]+" 的文件组织")
参数设置
验证代码(根据输入自动填充参数)
class ToolValidator(object):
"""Class for validating a tool's parameter values and controlling
the behavior of the tool's dialog."""
def __init__(self):
"""Setup arcpy and the list of tool parameters."""
self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
"""Refine the properties of a tool's parameters. This method is
called when the tool is opened."""
return
# 根据输入自动填充参数
def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
if self.params[0].altered:
if not self.params[1].value:
raw_dir = self.params[0].valueAsText.split(";")[0]
raw_path = os.path.dirname(raw_dir)
raw_name = os.path.splitext(os.path.basename(raw_dir))[0]
raw_ext = os.path.splitext(os.path.basename(raw_dir))[1]
new_dir=raw_path+"\\"+raw_name+"_Merge"+raw_ext
self.params[1].value=new_dir
return
def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
效果演示
合并湖南、湖北、江西三省的交通网络数据库
版权声明:本文为baidu_28157641原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。