ArcGIS Pro脚本工具(13)——合并多个文件夹或数据库

之前做了合并多个数据库的脚本,原文如下

使用Arcpy合并多个GDB数据库_学学GIS的博客-CSDN博客_arcpy gdb现有多个县的GDB数据库,每个数据库包含多个要素数据集,每个要素数据集下又包含多个要素类。现在需要创建一个新的GDB数据库,其包含之前所有数据库的要素数据集和数据集下的要素类,且每个要素类保留字段而不包含要素。人工处理工作量过大,使用Arcpy处理更有效率。代码如下import osimport arcpygdblist=[]file_dir="D:\\Desktop"dirlist=os.listdir(file_dir) #获取 D:\\Desktop 下一层级目录的路径for dihttps://blog.csdn.net/baidu_28157641/article/details/106602766但是有几点不足:

  1. 没有考虑数据库里面的独立要素类,也就是不在数据集里面的要素类
  2. 只能合并gdb数据库,没有考虑包含shp要素类的文件夹的合并
  3. 只有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]+" 的文件组织")

参数设置

f5f5efaaee674674bdc6ff093e18b97e.png

验证代码(根据输入自动填充参数)

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

效果演示

287dffac01c646c59cd3fd35af4ee8de.gif

合并湖南、湖北、江西三省的交通网络数据库

 

 


版权声明:本文为baidu_28157641原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。