Python脚本批量合并GDB

 在实际操作中,经常对数据库文件进行合并、裁切等。如果遇到gdb比较多,要素层比较多,而且还存在数据集。虽然ArcGIS中的批量处理的功能,但填写参数过程也比较麻烦,如果一次性处理过多,程序容易停止工作。

    所以,选用Python来处理这样的问题,优势就非常明显了。把路径设置好,读取数据库及要素类,然后进行合并操作。下面是Python示例代码:

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import arcpy
import string
  
try:
    workspace = arcpy.GetParameterAsText(0) #input workspace
    outdb = arcpy.GetParameterAsText(1) #out db
  
    #target fc,but don't support dataset
    arcpy.env.workspace=outdb
    fcs = arcpy.ListFeatureClasses()
  
    arcpy.env.workspace=workspace
    for File in arcpy.ListFiles("*.*db"):
        for fc in fcs:
            arcpy.Append_management(File+"\\"+fc, outdb+"\\"+fc)
except arcpy.ExecuteError:
    print arcpy.GetMessages()

 这里需要说明的是:

此代码不支持包含数据集(dataset)的数据库,如果存在这种情况,可根据实际情况来设置输入和输出的路径。如arcpy.Append_management(File+”\\dlg\\”+fc, outdb+”\\ dlg \\” +fc),这里的dlg即为数据库中的数据集,如果存在多个数据集,则需要读取数据集,这样的功能将在后续进行完善;
代码中11、12行是重载编码,但这样的结果是print无输出(环境:Win7 x64+Python2.7),这是因为忽略了UNICODE,把ASCII直接传给UTF-8,显示有问题是正常的。注释即可。

  

   

转载自:https://blog.csdn.net/kone0611/article/details/52083656

You may also like...