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

摘要:
#-*-coding:utf-8-*-#madeby汪林_质检处importos.pathimportarcpyimportsysfromarcpyimportenvFCDBDir="E:\cliptest\data"output="E:\Result"clipshp="E:\cliptest\clip.shp"GDBAllPath=[]#OID字段名称ShapeOID=""print'proce
#-*- coding: utf-8 -*-#made by 汪林_质检处
importos.path
importarcpy
importsys
from arcpy importenv


FCDBDir = "E:\cliptest\data"output = "E:\Result"clipshp = "E:\cliptest\clip.shp"
GDBAllPath=[]
#OID字段名称
ShapeOID = ""
print 'processing...'


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


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


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

上面裁剪结果为shape格式,下面裁剪结果为GDB格式。

#-*- coding: utf-8 -*-#made by 汪林_质检处
importos.path
importarcpy
importsys
from arcpy importenv


FCDBDir = "E:\cliptest\data"output = "E:\Result\caituhou"clipshp = "E:\cliptest\clip.shp"
GDBAllPath=[]
#OID字段名称
ShapeOID = ""
print 'processing...'


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


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


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

免责声明:文章转载自《批量裁剪GIS数据(包含GDB,MDB,Shp)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇关于C# winform唤起本地已安装应用程序(测试win10,win7可用)GIT : IDEA切换到某个tag下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Ansi,UTF8,Unicode编码(续)

1.三种编码的回顾 Ansi字符串我们最熟悉,英文占一个字节,汉字2个字节,以一个\0结尾,常用于txt文本文件。 Unicode字符串,每个字符(汉字、英文字母)都占2个字节;在VC++的世界里,Microsoft比较鼓励使用Unicode,如wchar_t。 UTF8是Unicode一种压缩形式,英文A在unicode中表示为0x0041,英语中这种存...

gdb命令调试技巧

gdb命令调试技巧 一、信息显示1、显示gdb版本 (gdb) show version2、显示gdb版权 (gdb) show version or show warranty3、启动时不显示提示信息gdb -q exe 或者.bashrc 添加alias gdb="gdb -q",重启shell4、退出时不显示提示信息(gdb) set confirm...

gdb 调试

昨天下班之间请教师父一个panic的解法,见他用gdb调试,真的很厉害,看到堆栈的东西,跟踪堆栈,定位报错的panic出错的具体汇编语句,很惊吓,感叹自己也是用gdb很久的人了,他的很多用法还不知道,特补充下vmlinux和gdb的相关知识在此作为笔记。 (gdb)list *0xc33310a0 (查看0xc33310a0地址所在的源代码段,常用来查看报...

Asp.Net 之 抓取网页内容

一、获取网页内容——html ASP.NET 中抓取网页内容是非常方便的,而其中更是解决了 ASP 中困扰我们的编码问题。 需要三个类:WebRequest、WebResponse、StreamReader。 WebRequest、WebResponse 的名称空间是:System.Net StreamReader 的名称空间是:System.IO 核心代...

C++用iconv进行页面字符转换

在对HTML页面进行爬取时,总会遇到一些不同的编码,而我们通常都不会一一对这些编码进行处理,而是集体转换成相同的编码,也易于装入数据库。此时,iconv便成为一个很方便的工具。 iconv 头文件"iconv.h"。iconv命令可以将一种已知的字符集文件转换成另一种已知的字符集文件。 它的作用是在多种国际编码格式之间进行文本内码的转换。 li...

vbs实现unicode和ascii的转化

一 Copy a Unicode File to an ANSI FileWiToAnsi.vbs文件: ' Utility to rewrite a Unicode text file as an ANSI text file' For use with Windows Scripting Host, CScript.exe or WScript.ex...