# -*- coding: utf-8 -*-
# made by 汪林_质检处
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')# 对于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编码解码成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"

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

# -*- coding: utf-8 -*-
# made by 汪林_质检处
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')# 对于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 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"

转载于:https://www.cnblogs.com/514687800/p/5109785.html

批量裁剪GIS数据(包含GDB,MDB,Shp)相关推荐

  1. 使用SHP数据批量裁剪TIF图像并转为JPG格式

    问题描述 已有数据:1张大范围的遥感影像和1张裁剪为同一大小的shp数据 使用工具:ArcPy 实现目标: 1.shp数据按照属性分块输出 2.使用shp数据批量裁剪遥感影像,将影像裁剪为小块的tif ...

  2. 使用shp数据批量裁剪栅格数据并统计均值

    批量实现利用shp文件,裁剪相应的多个栅格数据,并统计裁剪后栅格的均值至TXT文件,亲测可用. #使用shp数据批量裁剪栅格数据并统计均值 # -*- coding: cp936 -*- #使用shp ...

  3. GIS数据与CAD数据间带属性字段互相转换还原工具,shp转换成dwg保留属性表字段,解决ArcGIS等软件进行GIS数据转CAD数据无法保留属性字段问题

    目录 一.实现效果 二.shp转dwg工具 1.读取数据 2.数据设置 3.输出数据 4.工具使用 三.dwg转shp工具 1.暴露字段属性,并按字段分隔 2.提取字段信息 3.提取暴露字段 4.输出 ...

  4. gis 数据框裁剪_BIM+GIS的八大挑战!大挑战,见未来

    BIM与GIS的跨界融合,使微观领域的BIM信息与宏观领域的GIS信息实现交换和互操作,提升了BIM应用深度,将BIM的应用从单体延伸到建筑群甚至城市级,为GIS行业发展带来了新的契机,同时也带来了一 ...

  5. Mapinfo的TAB格式批量转GIS的SHP格式

    Mapinfo的TAB格式批量转GIS的SHP格式 现在用Mapinfo软件的越来越少了,但是日常工作学习中还是会经常接触到TAB格式,现在我们来用Mapinfo软件来批量将TAB格式批量转GIS的S ...

  6. GIS技巧100例05-ArcGIS分割栅格(批量裁剪)

    GIS思维于这一次要介绍的是<GIS技巧100例05-ArcGIS分割栅格(批量裁剪)>,我们之前介绍ArcGIS批量批量裁剪影像建议大家用的是模型构建器.今天我会介绍ArcGIS分割工具 ...

  7. gis 数据框裁剪_ArcGis入门学习教程带练习数据

    百度网盘 链接:https://pan.baidu.com/s/18iTEYMygozKZT2vuzLY8Dg 提取码:pwp8 目    录 实验一.使用 ARCMAP浏览地理数据 1 一.实验目的 ...

  8. 【ArcGIS遇上Python】窗体版Python批量处理地理数据--栅格裁剪

    一.说明 之前写过用Python代码块处理地理数据的例子,如裁剪.投影.格式转换等,曾想过用C#制作个窗体,嵌入Python代码,今天无意发现Python也能用tKinter构建简单的窗体,眼前一亮, ...

  9. gis 数据框裁剪_【GIS实操】如何用GIS提取“三调”数据?

    做国土空间规划时,最重要的资料之一就是三调GIS数据.当资料到手,会发现一个问题:我只需要做一个镇/村的规划,但是整个市/镇数据在手,怎样才能将我需要的内容提取出来呢?这就需要用到GIS的剪裁工具!剪 ...

最新文章

  1. 硬投票分类器(VotingClassifier)构建实战
  2. python项目2019_2019 年 11 月精选 GitHub 上 几个牛逼 Python 的项目
  3. 我心目中的编程高手(转)
  4. [zz]正则表达式使用详解
  5. Android自定义控件:动画类---逐帧动画AnimationDrawable
  6. Linux系统中df与du命令查看分区大小
  7. window命令查看cpu核数_win7如何查内存条型号机CPU 信息【通过命令来查看】
  8. 前端面试题大集合:来自真实大厂的532道面试题(只有题,没有答案)
  9. htc a620d 刷android,续航以及最后的总结_HTC A620d - CNMO
  10. 阿里巴巴矢量图标的应用
  11. 光谱分析中的变量选择
  12. php集成c sdk,GitHub - cuncle/spider-php-sdk
  13. 微信H5域名被屏蔽被拦截的最新解决方案(微信域名防投诉)
  14. 中国这10家慕课网站,您需要知道
  15. 软件著作权转让的收费标准怎样
  16. 快速实现Thread Mesh组网详解
  17. Colmap论文——《Structure-from-Motion Revisited》论文阅读笔记
  18. 树莓派不插HDMI不能开机的解决方法
  19. 如何选择适合你的兴趣爱好(七十),表演
  20. python中 什么意思_Python里面的这几个梗,你能回答出来吗

热门文章

  1. Imagination
  2. Hibernate读书笔记
  3. 不要错过路边的的风景
  4. DataGrid 完全攻略之四 (实现统计)
  5. Single Image Haze Removal(图像去雾)-CVPR’09 Best Paper
  6. RDIFramework.NET(.NET快速开发框架) 答客户问(2014-02-23)
  7. 机器学习预测+akshare
  8. 软件定义,软件开发,软件维护
  9. wps html编辑表格,WPS 2017个人版演示word使用技巧(wps2017表格使用技巧)
  10. python中什么是关键字参数_如何使用python语言中函数的关键字参数的用法