日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 批量裁剪GIS数据(包含GDB,MDB,Shp) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
# -*- coding: utf-8 -*- # made by 汪林_質(zhì)檢處 import os.path import arcpy import sys from arcpy import envFCDBDir = "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 = fieldbreak if os.path.exists(FCDBDir):for dirpath,dirnames,filenames in os.walk(FCDBDir):# 遍歷GDB文件夾 獲取GDBfor dirname in dirnames:if ".gdb" in dirname:gdbfilepath = os.path.join(dirpath,dirname)if not gdbfilepath in GDBAllPath:GDBAllPath.append(gdbfilepath)# 遍歷MDB文件夾 獲取MDBfor 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文件夾 獲取Shapefor 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 = everyfilepathsinglefclist = arcpy.ListFeatureClasses("","All")if singlefclist and len(singlefclist)>0:for singlefc in singlefclist:# 如果singlefc是unicode則不做改變,否則將utf-8的singlefc編碼解碼成unicodeif not isinstance(singlefc,unicode):singlefc = singlefc.decode('utf-8')# 對(duì)于Shape FC會(huì)帶擴(kuò)展名.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編碼解碼成unicodeif not isinstance(dataset,unicode):dataset = dataset.decode('utf-8')if isinstance(everyfilepath,unicode):env.workspace = everyfilepath+"\\"+datasetdspath = everyfilepath+"\\"+datasetelse: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"

上面裁剪結(jié)果為shape格式,下面裁剪結(jié)果為GDB格式。

# -*- coding: utf-8 -*- # made by 汪林_質(zhì)檢處 import os.path import arcpy import sys from arcpy import envFCDBDir = "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 = fieldbreak if os.path.exists(FCDBDir):for dirpath,dirnames,filenames in os.walk(FCDBDir):# 遍歷GDB文件夾 獲取GDBfor dirname in dirnames:if ".gdb" in dirname:gdbfilepath = os.path.join(dirpath,dirname)if not gdbfilepath in GDBAllPath:GDBAllPath.append(gdbfilepath)# 遍歷MDB文件夾 獲取MDBfor 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文件夾 獲取Shapefor 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 = everyfilepathsinglefclist = arcpy.ListFeatureClasses("","All")if singlefclist and len(singlefclist)>0:for singlefc in singlefclist:# 如果singlefc是unicode則不做改變,否則將utf-8的singlefc編碼解碼成unicodeif not isinstance(singlefc,unicode):singlefc = singlefc.decode('utf-8')# 對(duì)于Shape FC會(huì)帶擴(kuò)展名.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 singlefcelse:# 只有GB2312編碼才能做替換操作newoutputsinglefc = str(singlefc.encode('gb2312')).replace('-','_')if not isinstance(newoutputsinglefc,unicode):newoutputsinglefc = newoutputsinglefc.decode('gb2312')print singlefcarcpy.Clip_analysis(singlefc,"lyr",outputgdb+"\\"+newoutputsinglefc,""); datasetlist = arcpy.ListDatasets("","Feature")for dataset in datasetlist:# 如果dataset是unicode則不做改變,否則將utf-8的dataset編碼解碼成unicodeif not isinstance(dataset,unicode):dataset = dataset.decode('utf-8')if isinstance(everyfilepath,unicode):env.workspace = everyfilepath+"\\"+datasetdspath = everyfilepath+"\\"+datasetelse: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"

?

轉(zhuǎn)載于:https://www.cnblogs.com/514687800/p/5109785.html

總結(jié)

以上是生活随笔為你收集整理的批量裁剪GIS数据(包含GDB,MDB,Shp)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。