ArcGIS Python 矢量数据批量裁剪
矢量数据批量裁剪
输入裁剪的要素图层,可以是一个或多个,裁剪依据的图层一般是面要素,字段是裁剪后数据库的名字(如果是shp格式,是文件夹的名称),如果选择字段不是唯一值,有相同的,则相同的数据融合在一起裁剪输入要素,输出结果可以是gdb(文件地理数据库)或mdb(个人地理数据库),也可以是SHP,裁剪后要素名称和原来的一样,界面下图所示。
批量裁剪界面
代码如下:
#coding=utf8
import sys, os, string
import arcpy
from arcpy import env
def getuniqueValue(inTable,inField):#获得字段唯一值
rows = arcpy.SearchCursor(inTable)
# Create an empty list
uniqueList = []
try:
for row in rows:
# If the value is not already in the list, append it
if row.getValue(inField) not in uniqueList:
uniqueList.append(row.getValue(inField))
return uniqueList
finally:
if row:
del row
if rows:
del rows
def getlength(mystr):#获得长度,汉字长度为2,英文为1
lenTxt = len(mystr)
lenTxt_utf8 = len(mystr.decode('utf-8'))
size = int((lenTxt_utf8 - lenTxt)/2 + lenTxt)
#arcpy.AddMessage("'{0}'最后长度{1},lenTxt={2},lenTxt_utf8={3}".format(mystr,size,lenTxt,lenTxt_utf8))
return size
def midFill(sumn,mystr,Fill):#填充长度一样
n=getlength(mystr)
if n>=sumn:
return mystr
leftn=int((sumn-n)/2)
s=""
lefts=s.ljust(leftn,Fill)
s=""
rightn=sumn-n-leftn
rights=s.ljust(rightn,Fill)
return lefts+mystr+rights
def printauthor(toolname):#打印作者信息
titlestr=""
sumn=60
Fill='*'
titlestr=titlestr.ljust(sumn,Fill)
arcpy.AddMessage(titlestr)
arcpy.AddMessage(midFill(sumn,u"欢迎使用:"+toolname,Fill))
mystr=u"本工具闫磊编制,QQ:276529800"
arcpy.AddMessage(midFill(sumn,mystr,Fill))
mystr=u"使用前请做好数据备份,工具产生的不良后果请自行承担!"
arcpy.AddMessage(midFill(sumn,mystr,Fill))
arcpy.AddMessage(titlestr)
arcpy.env.overwriteOutput = True
inworkspace = arcpy.GetParameterAsText(0)
clipshp = arcpy.GetParameterAsText(1)
fieldname= arcpy.GetParameterAsText(2)
outworkspace = arcpy.GetParameterAsText(3)
gdbbool = arcpy.GetParameterAsText(4).upper()
desc = arcpy.Describe(clipshp)
shapeName = desc.shapeFieldName #shape字段
result = arcpy.GetCount_management(clipshp)
count= int(result.getOutput(0))
if count <= 0:
arcpy.AddMessage(clipshp+u"没有数据")
pass
printauthor(u"矢量数据批量裁剪")
uniqueList=getuniqueValue(clipshp,fieldname)
Dissolveb=False;#是否融合
num1=len(uniqueList)
if not count==num1:
arcpy.AddMessage(u"由于"+fieldname+u"字段不是唯一值,软件做了融合处理,你看到几个和最终结果个数不一致,原始有"+str(count)+u"个,最后输出只有"+str(num1)+u"个数据库")
outnewshp = arcpy.CreateUniqueName("yl_temp") #临时
arcpy.Dissolve_management(clipshp, outnewshp, [fieldname], "", "MULTI_PART","DISSOLVE_LINES")
count=len(uniqueList)
clipshp= outnewshp
Dissolveb=True
mydatasets= inworkspace.split(";")
num=len(mydatasets)*count
arcpy.SetProgressor("step", u"更新"+fieldname,0,num,1)
rows = arcpy.SearchCursor(clipshp)
row = rows.next()
i=0;
while row:
#arcpy.AddMessage(u"7=执行到这里")
fieldvalue =""+ str(row.getValue(fieldname))
out_mdb=""
#arcpy.AddMessage("======================================================out_mdb"+out_mdb)
if gdbbool=="MDB":
out_mdb=outworkspace + "\\"+fieldvalue+".mdb" #os.path.basename(dataset)
elif gdbbool=="GDB":
out_mdb=outworkspace + "\\"+fieldvalue+".gdb"
else:#shp
out_mdb=outworkspace +os.sep+fieldvalue
arcpy.AddMessage(u"out_mdb"+out_mdb)
if not arcpy.Exists(out_mdb): #可以文件夹
if gdbbool=="MDB":
arcpy.CreatePersonalGDB_management(os.path.dirname(out_mdb),os.path.basename(out_mdb))
elif gdbbool=="GDB":
arcpy.CreateFileGDB_management(os.path.dirname(out_mdb),os.path.basename(out_mdb))
else:#shp
os.makedirs(out_mdb)
#arcpy.AddMessage("88888888888888888888888888888888888888")
geometry=row.getValue(shapeName)
for dataset in mydatasets:
i+=1
arcpy.SetProgressorPosition()
arcpy.SetProgressorLabel(u"正在裁剪....,完成:"+str(i*100/num)+"%" )
try:
#mylayer=os.path.basename(dataset) #原来有别名的裁剪有问题
desc=arcpy.Describe(dataset)
mylayer=desc.baseName
arcpy.AddMessage(u"clip:"+dataset+" to "+out_mdb+"\\"+ mylayer)
mylayer=mylayer.replace("(","")
mylayer=mylayer.replace(")","")
#oldoutFC=out_mdb+"\\"+ mylayer
outFC = arcpy.ValidateTableName(mylayer,out_mdb)
#arcpy.AddMessage("outFC:"+outFC)
#arcpy.Clip_analysis(dataset, jfb_Select,out_mdb+"\\"+ mylayer, "")
if gdbbool=="SHP":
arcpy.Clip_analysis(dataset, geometry,out_mdb+"\\"+outFC+".shp", "")
else:
arcpy.Clip_analysis(dataset, geometry,out_mdb+"\\"+outFC, "")
except Exception, ErrorDesc:
#If an error set output boolean parameter "Error" to True.
arcpy.AddError(str(ErrorDesc))
row = rows.next()
if Dissolveb:
if arcpy.Exists(clipshp):
arcpy.Delete_management(clipshp)
if row:
del row
arcpy.ResetProgressor()
文章授权转载:gisoracle
- END -
李德仁院士:关于推进实景三维中国建设的若干思考
全球矢量任意下载!路网、建筑、水系...
自动化和半自动矢量化提取地物矢量轮廓
420平方公里实景三维模型!苏州正在打造一座数字孪生城