批量裁剪GIS数据(包含GDB,MDB,Shp)

本段Python代码支持ArcGIS常见格式比如GDB、MDB和Shape格式的裁剪。

# -*- coding: utf-8 -*-
# made by 汪林_质检处
import os.path
import arcpy
import sys
from arcpy import env


FCDBDir = "E:\\cliptest\\data"
output = "E:\\Result"
clipshp = "E:\\cliptest\\clip.shp"

GDBAllPath=[]
# OID字段名称
ShapeOID = ""
print 'processing...'


if not isinstance(clipshp,unicode):
    clipshp = clipshp.decode('utf-8')
if not isinstance(FCDBDir,unicode):
    FCDBDir = FCDBDir.decode('utf-8')
if not isinstance(output,unicode):
    output = output.decode('utf-8')


if not os.path.isfile(clipshp):
    print clipshp +" is not a File"
    sys.exit(0)
fields = arcpy.ListFields(clipshp)
if fields is None:
    print "Read "+clipshp+"Failed"
    sys.exit(0)


for field in fields:
    if field.type == "OID":
        ShapeOID = field
        break
if os.path.exists(FCDBDir):
    for dirpath,dirnames,filenames in os.walk(FCDBDir):
        # 遍历GDB文件夹 获取GDB
        for dirname in dirnames:
            if ".gdb" in dirname:
                gdbfilepath = os.path.join(dirpath,dirname)
                if not gdbfilepath in  GDBAllPath:
                    GDBAllPath.append(gdbfilepath)
        # 遍历MDB文件夹 获取MDB
        for filename in filenames:
            if os.path.splitext(filename)[1]=='.mdb':
                mdbfilepath = os.path.join(dirpath,filename)
                if not mdbfilepath in GDBAllPath:
                    GDBAllPath.append(mdbfilepath)
        # 遍历Shp文件夹  获取Shape
        for filename in filenames:
            if os.path.splitext(filename)[1]=='.shp':
                shpfilepath = os.path.join(dirpath,filename)
                if not dirpath in GDBAllPath:
                    GDBAllPath.append(dirpath)
else:
    print "Directory "+FCDBDir+" Not Exist"
    sys.exit(0)
arcpy.MakeFeatureLayer_management(clipshp, "lyr")
values = [row[0] for row in arcpy.da.SearchCursor(clipshp, ShapeOID.name)]
uniqueValues = set(values)
for unique in uniqueValues:
    arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION",ShapeOID.name + " = " + str(unique))
    rows = arcpy.SearchCursor("lyr")
    for row in rows:       
        everyResultPath = os.path.join(output,str(row.getValue(ShapeOID.name)))
        if not os.path.exists(everyResultPath):
            print "Directory "+everyResultPath+" Created Succeed"
            os.makedirs(everyResultPath)
        for everyfilepath in GDBAllPath:
            env.workspace = everyfilepath
            singlefclist = arcpy.ListFeatureClasses("","All")
            if singlefclist and len(singlefclist)>0:
                for singlefc in singlefclist:
                    # 如果singlefc是unicode则不做改变,否则将utf-8的singlefc编码解码成unicode
                    if not isinstance(singlefc,unicode):
                        singlefc = singlefc.decode('utf-8')
                    # 对于Shape FC会带扩展名.shp,如果是gdb或mdb 则不带
                    if '.shp' in singlefc:
                        # 只有GB2312编码才能做替换操作
                        newoutputsinglefc = str(singlefc.encode('gb2312')).replace('-','_')
                        if not isinstance(newoutputsinglefc,unicode):
                            newoutputsinglefc = newoutputsinglefc.decode('gb2312')
                        singlefc = singlefc[0:singlefc.find('.shp')]
                        arcpy.env.outputMFlag= "disabled"
                        arcpy.Clip_analysis(singlefc+".shp","lyr",everyResultPath+"\\"+newoutputsinglefc+".shp","");
                    else:
                        # 只有GB2312编码才能做替换操作
                        newoutputsinglefc = str(singlefc.encode('gb2312')).replace('-','_')
                        if not isinstance(newoutputsinglefc,unicode):
                            newoutputsinglefc = newoutputsinglefc.decode('gb2312')
                        arcpy.Clip_analysis(singlefc,"lyr",everyResultPath+"\\"+newoutputsinglefc+".shp","");                  
            datasetlist = arcpy.ListDatasets("","Feature")
            for dataset in datasetlist:
                # 如果dataset是unicode则不做改变,否则将utf-8的dataset编码解码成unicode
                if not isinstance(dataset,unicode):
                    dataset = dataset.decode('utf-8')
                if isinstance(everyfilepath,unicode):
                    env.workspace = everyfilepath+"\\"+dataset
                    dspath = everyfilepath+"\\"+dataset
                else:
                    env.workspace = everyfilepath+"\\"+dataset.encode('gb2312')
                    dspath = everyfilepath+"\\"+dataset.encode('gb2312')
                fclist = arcpy.ListFeatureClasses("")
                if fclist and len(fclist)>0:
                    for fc in fclist:
                       arcpy.Clip_analysis(fc,"lyr",everyResultPath+"\\"+fc+".shp","");
print "Done"
    

上述代码裁剪结果为shape格式。下面的代码裁剪结果为Gdb文件格式。

# -*- coding: utf-8 -*-
# made by 汪林_质检处
import os.path
import arcpy
import sys
from arcpy import env


FCDBDir = "E:\\cliptest\\data"
output = "E:\\Result\\caituhou"
clipshp = "E:\\cliptest\\clip.shp"

GDBAllPath=[]
# OID字段名称
ShapeOID = ""
print 'processing...'


if not isinstance(clipshp,unicode):
    clipshp = clipshp.decode('utf-8')
if not isinstance(FCDBDir,unicode):
    FCDBDir = FCDBDir.decode('utf-8')
if not isinstance(output,unicode):
    output = output.decode('utf-8')


if not os.path.isfile(clipshp):
    print clipshp +" is not a File"
    sys.exit(0)
fields = arcpy.ListFields(clipshp)
if fields is None:
    print "Read "+clipshp+"Failed"
    sys.exit(0)


for field in fields:
    if field.type == "OID":
        ShapeOID = field
        break
if os.path.exists(FCDBDir):
    for dirpath,dirnames,filenames in os.walk(FCDBDir):
        # 遍历GDB文件夹 获取GDB
        for dirname in dirnames:
            if ".gdb" in dirname:
                gdbfilepath = os.path.join(dirpath,dirname)
                if not gdbfilepath in  GDBAllPath:
                    GDBAllPath.append(gdbfilepath)
        # 遍历MDB文件夹 获取MDB
        for filename in filenames:
            if os.path.splitext(filename)[1]=='.mdb':
                mdbfilepath = os.path.join(dirpath,filename)
                if not mdbfilepath in GDBAllPath:
                    GDBAllPath.append(mdbfilepath)
        # 遍历Shp文件夹  获取Shape
        for filename in filenames:
            if os.path.splitext(filename)[1]=='.shp':
                shpfilepath = os.path.join(dirpath,filename)
                if not dirpath in GDBAllPath:
                    GDBAllPath.append(dirpath)
else:
    print "Directory "+FCDBDir+" Not Exist"
    sys.exit(0)
arcpy.MakeFeatureLayer_management(clipshp, "lyr")
values = [row[0] for row in arcpy.da.SearchCursor(clipshp, ShapeOID.name)]
uniqueValues = set(values)
for unique in uniqueValues:
    arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION",ShapeOID.name + " = " + str(unique))
    rows = arcpy.SearchCursor("lyr")
    for row in rows:       
        everyResultPath = os.path.join(output,str(row.getValue(ShapeOID.name)))
        if not os.path.exists(everyResultPath):
            os.makedirs(everyResultPath)
            print "Directory "+everyResultPath+" Created Succeed"
        outputgdb =  os.path.join(everyResultPath,"ClipResult"+str(row.getValue(ShapeOID.name))+".gdb")
        if os.path.isdir(outputgdb):
            if arcpy.Exists(outputgdb):
                arcpy.Delete_management(outputgdb)
        arcpy.CreateFileGDB_management(everyResultPath,"ClipResult"+str(row.getValue(ShapeOID.name))+".gdb")
        print outputgdb+" Create Succeeded"
        for everyfilepath in GDBAllPath:
            env.workspace = everyfilepath
            singlefclist = arcpy.ListFeatureClasses("","All")
            if singlefclist and len(singlefclist)>0:
                for singlefc in singlefclist:
                    # 如果singlefc是unicode则不做改变,否则将utf-8的singlefc编码解码成unicode
                    if not isinstance(singlefc,unicode):
                        singlefc = singlefc.decode('utf-8')
                    # 对于Shape FC会带扩展名.shp,如果是gdb或mdb 则不带
                    if '.shp' in singlefc:
                        # 只有GB2312编码才能做替换操作
                        newoutputsinglefc = str(singlefc.encode('gb2312')).replace('-','_')
                        if not isinstance(newoutputsinglefc,unicode):
                            newoutputsinglefc = newoutputsinglefc.decode('gb2312')
                        singlefc = singlefc[0:singlefc.find('.shp')]
                        arcpy.env.outputMFlag= "disabled"
                        arcpy.Clip_analysis(singlefc+".shp","lyr",outputgdb+"\\"+newoutputsinglefc,"");
                        print singlefc
                    else:
                        # 只有GB2312编码才能做替换操作
                        newoutputsinglefc = str(singlefc.encode('gb2312')).replace('-','_')
                        if not isinstance(newoutputsinglefc,unicode):
                            newoutputsinglefc = newoutputsinglefc.decode('gb2312')
                        print singlefc
                        arcpy.Clip_analysis(singlefc,"lyr",outputgdb+"\\"+newoutputsinglefc,"");                  
            datasetlist = arcpy.ListDatasets("","Feature")
            for dataset in datasetlist:
                # 如果dataset是unicode则不做改变,否则将utf-8的dataset编码解码成unicode
                if not isinstance(dataset,unicode):
                    dataset = dataset.decode('utf-8')
                if isinstance(everyfilepath,unicode):
                    env.workspace = everyfilepath+"\\"+dataset
                    dspath = everyfilepath+"\\"+dataset
                else:
                    env.workspace = everyfilepath+"\\"+dataset.encode('gb2312')
                    dspath = everyfilepath+"\\"+dataset.encode('gb2312')
                fclist = arcpy.ListFeatureClasses("")
                if fclist and len(fclist)>0:
                    for fc in fclist:
                       arcpy.Clip_analysis(fc,"lyr",outputgdb+"\\"+fc,"");
                       print fc;
print "Done"
    


### 回答1: 要批量合并mdbgdb文件,可以按照以下步骤进行操作: 1. 确定要合并的mdbgdb文件所在的文件夹。 2. 创建一个新的gdb文件,作为合并后的文件存放位置。可以使用ArcGIS软件的“新建地理数据库”工具或者使用Python的arcpy模块进行创建。 3. 使用ArcGIS软件或者Python中的arcpy模块的“导入XML工作空间文档”或“导入地理数据库工作空间文档”工具,将mdb文件逐个导入到新的gdb文件中。 4. 使用ArcGIS软件中的“合并(Merge)”工具或者Python中的arcpy模块的“合并(Merge)”函数,将gdb文件夹中的所有gdb文件合并到新的gdb文件中。如果需要合并mdb文件夹中的所有mdb文件,可以先将这些mdb文件导入到新的gdb文件中,再进行合并操作。 5. 在合并过程中,需要注意字段的映射关系和重名字段的处理。可以根据需要进行字段匹配和筛选,确保合并后的数据结构和内容符合预期。 6. 完成合并后,对合并后的gdb文件进行验证和审查,确保数据的完整性和正确性。 总之,要批量合并mdbgdb文件,需要创建新的gdb文件作为合并后的存放位置,然后逐个导入mdb文件或合并gdb文件到新的gdb文件中。在合并过程中,需要注意字段的映射关系和重名字段的处理,最后进行数据验证和审查。 ### 回答2: 要批量合并mdbgdb文件,可以按照以下步骤进行: 一、准备工作: 1. 首先,将所有需要合并的mdbgdb文件放置在同一个文件夹下,方便进行批量处理。 2. 确保已安装相应的数据库软件,如Microsoft Access用于处理mdb文件,以及ArcGIS用于处理gdb文件。 二、合并mdb文件: 1. 打开Microsoft Access软件。 2. 在工具栏上选择“外部数据”,然后选择“新建数据源”。 3. 在弹出的对话框中选择“导入表”选项,并点击“确定”。 4. 在“文件类型”下拉菜单中选择“Microsoft Access”,然后浏览到mdb文件所在文件夹,并选择需要合并的mdb文件。 5. 点击“确定”开始导入选中的mdb文件。 6. 重复上述步骤,依次选择需要合并的所有mdb文件进行导入。 7. 导入完成后,在Microsoft Access中会显示所有导入的数据表,这些表即为合并后的结果。 三、合并gdb文件: 1. 打开ArcGIS软件。 2. 在ArcGIS主界面中,选择“目录”,然后浏览到gdb文件所在文件夹。 3. 在ArcCatalog中,选择“工具”,然后选择“数据整理工具”,再选择“合并”工具。 4. 在“合并”工具对话框中,选择需要合并的gdb文件,并设置输出位置。 5. 点击“运行”开始合并gdb文件。 6. 合并完成后,在输出位置会生成一个新的gdb文件,其中包含了所有选中gdb文件的内容。 通过以上步骤,就可以批量合并mdbgdb文件了。在合并完成后,可以进行必要的数据校验和处理,以确保数据的一致性和正确性。 ### 回答3: 批量合并mdbgdb需要依赖专业的地理信息系统(GIS)软件和一些技术知识。以下是一种常见的方法,用于批量合并mdbgdb: 1. 准备工作:首先,确定要合并的mdbgdb文件的位置和名称。确保这些文件都是在同一文件夹中,并且已安装所需的GIS软件。 2. 打开GIS软件:启动所选的GIS软件,例如ArcGIS、QGIS等。在软件界面中,进入“文件”或“工程”菜单,选择“打开”。 3. 添加数据:在打开的对话框中,导航到存储mdbgdb文件的文件夹,选择要合并的第一个mdb文件,然后点击“打开”按钮。 4. 合并mdb数据:在软件界面的主菜单中,找到“数据管理”或“数据工具”选项,选择“合并”或“转换”操作。在弹出的对话框中,选择要合并的mdb文件和合并后的输出位置。设定其他合并选项,例如字段映射、数据类型转换等。完成设置后,点击“运行”或“合并”按钮,开始合并过程。 5. 合并gdb数据:同样的操作,选择要合并的gdb文件和合并后的输出位置,设定其他合并选项,然后点击“运行”或“合并”按钮,开始合并过程。 6. 批量合并:如果有多个mdbgdb文件需要合并,重复步骤4和步骤5,直到合并完所有文件。 7. 检查合并结果:合并完所有mdbgdb文件后,对结果进行验证。检查合并后的输出文件是否包含了所有合并的数据,并且数据完整、正确。 以上是一种基本的批量合并mdbgdb的方法。具体步骤可能会因所使用的GIS软件而有所不同,但整体的原理和操作类似。在实际操作中,可以根据具体需求和软件功能进行调整和优化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值