在好例子网,分享、交流、成长!
您当前所在位置:首页Python 开发实例Python语言基础 → arcgis批量裁剪gdb(批处理)

arcgis批量裁剪gdb(批处理)

Python语言基础

下载此实例
  • 开发语言:Python
  • 实例大小:5.33KB
  • 下载次数:13
  • 浏览次数:383
  • 发布时间:2021-02-04
  • 实例类别:Python语言基础
  • 发 布 人:zzsdexcfgh
  • 文件格式:
  • 所需积分:2
 相关标签: ArcGIS GIS AR 批量 GD

实例介绍

【实例简介】
【实例截图】
【核心代码】
# -*- coding: utf-8 -*-
# made by 汪林_质检处
import os.path
import arcpy
import sys
from arcpy import env
 
 
FCDBDir = "D:\\di\\houhe.gdb"
output = "D:\\di\\caituhou"
clipshp = "D:\\di\\tufu.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"


标签: ArcGIS GIS AR 批量 GD

实例下载地址

arcgis批量裁剪gdb(批处理)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警