foreword
It's time to share Python tips every day again. What do I want to share with you today? Do you want to guess, today I will share with you the batch cropping and merging of vector images.
It sounds a bit incomprehensible, but after reading it, you will understand and expand your knowledge.
Vector batch cropping code
In: chp10\python\vector batch cropping.tbx\vector batch cropping, you can run it directly, right-click to edit and view the code
code show as below:
Python学习交流Q群:906715085###
#---------------------------------------------------------------------------
#1.py# Created on: 星期日 一月 10 2018 11:02:13 上午
#(generated by ArcGIS/ModelBuilder)
#Usage: 矢量图批量切割, by 闫磊 4个参数
#原始数据 是图层,可以多选
#切割工具是是接幅表 或者行政
#字段是输出mdb名称
#输出路径
#---------------------------------------------------------------------------
#Create Geoprocessing Object
import sys, os, string
import arcpyfrom arcpy import env
defaultencoding = 'utf-8'if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding)
arcpy.env.overwriteOutput = True
inworkspace = arcpy.GetParameterAsText(0)
arcpy.AddMessage("输入数据="+inworkspace)
clipshp = arcpy.GetParameterAsText(1)
arcpy.AddMessage("裁剪=clipshp"+clipshp)
fieldname= arcpy.GetParameterAsText(2)
arcpy.AddMessage("字段=fieldname"+fieldname)
outworkspace = arcpy.GetParameterAsText(3)
arcpy.AddMessage("输出="+outworkspace)
mdbbool = arcpy.GetParameterAsText(4)
arcpy.AddMessage("是否mdb="+mdbbool)
desc = arcpy.Describe(clipshp)
filepath=desc.CatalogPathp=filepath.find(".mdb")
ftype="String"for field in desc.fields:
if field.Name ==fieldname:
ftype=field.Type
breakarcpy.AddMessage(u"默认地理数据库:"+arcpy.env.scratchWorkspace)jfb_
Select=arcpy.env.scratchWorkspace+"\yl999"#不能c:\要c:\\或者 c:/
rows = arcpy.SearchCursor(clipshp)
#arcpy.AddMessage(u"5=执行到这里")row = rows.next()#arcpy.AddMessage(u"6=执行到这里")while row: #arcpy.AddMessage(u"7=执行到这里") fieldvalue =""+ str(row.getValue(fieldname)) #arcpy.AddMessage(u"值fieldvalue="+fieldvalue) if p>0: #mdb Expression="["+fieldname +"]=" else: Expression="\""+fieldname +"\"=" #arcpy.AddMessage(u"表达式Expression1="+Expression) if ftype=="String":
Expression=Expression+"'"+fieldvalue+"'"
else:
Expression=Expression+fieldvalue
#arcpy.AddMessage(u"Expression2="+Expression)
arcpy.Select_analysis(clipshp, jfb_Select,Expression)
#arcpy.AddMessage(u"6=clipshp"+clipshp) out_mdb="" #arcpy.AddMessage("======================================================out_mdb"+out_mdb) if mdbbool=="true":
out_mdb=outworkspace + "\\"+fieldvalue+".mdb" #os.path.basename(dataset)
else:
out_mdb=outworkspace + "\\"+fieldvalue+".gdb"
arcpy.AddMessage(u"out_mdb"+out_mdb)
if not arcpy.Exists(out_mdb):
if mdbbool=="true": arcpy.CreatePersonalGDB_management(os.path.dirname(out_mdb),os.path.basename(out_mdb))
else:
arcpy.CreateFileGDB_management(os.path.dirname(out_mdb),os.path.basename(out_mdb))
mydatasets= string.split(inworkspace,";")
for dataset in mydatasets:
try:
mylayer=os.path.basename(dataset)
arcpy.AddMessage(u"clip:"+dataset+" to "+out_mdb+"\\"+ mylayer)
mylayer=mylayer.replace("(","")
mylayer=mylayer.replace(")","")
arcpy.Clip_analysis(dataset, jfb_Select,out_mdb+"\\"+ mylayer, "")
except Exception, ErrorDesc:
#If an error set output boolean parameter "Error" to True.
arcpy.AddError(str(ErrorDesc))
row = rows.next()if arcpy.Exists(jfb_Select):
arcpy.Delete_management(jfb_Select)
Function: Use one vector layer to batch crop multiple vector data, the field value is the name of the database after cropping
Batch cutting parameter setting interface
The interface is as follows:
Batch cutting running interface
mdb or not, mdb if checked, gdb if not checked
◾Vector batch merge
In: chp10\python\batch merge.tbx\data batch merge, you can run it directly, right-click to edit to view the code
code show as below:
import sys
##############################################
import arcpy
import string
try:
workspace =arcpy.GetParameterAsText(0) #'C:\Users\Administrator\Desktop\\cc'
outdb =arcpy.GetParameterAsText(1) #'C:\Users\Administrator\Desktop\\lutian.mdb'
arcpy.env.workspace = workspace
arcpy.AddMessage("outdb:"+outdb)
files = arcpy.ListWorkspaces("","")
for File in files:
arcpy.AddMessage("File:"+File)
arcpy.env.workspace = outdb
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
arcpy.AddMessage("fc:"+fc)
if arcpy.Exists(File + "\\" + fc):
arcpy.Append_management([ File + "\\" + fc], outdb + "\\" + fc,"NO_TEST","","")
else:
arcpy.AddMessage("not exists:"+File + "\\" + fc)
fcs = arcpy.ListTables()
for fc in fcs:
arcpy.AddMessage("fc:"+fc)
if arcpy.Exists(File + "\\" + fc):
arcpy.Append_management([File + "\\" + fc], outdb + "\\" + fc,"NO_TEST","","")
else:
arcpy.AddMessage("not exists:"+File + "\\" + fc)
dss = arcpy.ListDatasets()
for ds in dss:
arcpy.AddMessage("ds:"+ds)
arcpy.env.workspace = outdb+"\\"+ds
fcs1 = arcpy.ListFeatureClasses()
for fc1 in fcs1:
arcpy.AddMessage("fc1:"+fc1)
if arcpy.Exists(File + "\\" + ds + "\\" + fc1):
arcpy.Append_management([File + "\\" + ds + "\\" + fc1], outdb + "\\" + ds + "\\" + fc1,"NO_TEST","","")
else:
arcpy.AddMessage("not exists:"+File + "\\" + ds + "\\" + fc1)
except arcpy.ExecuteError:
arcpy.AddWarning(arcpy.GetMessages())
The parameters are as follows. The workspace can be a geodatabase (file geodata or personal geodata) or a folder. It is recommended to be a database here.
Batch merge parameter setting interface
The running interface is as follows:
Batch merge operation interface
◾ Image batch cropping
In: chp10\python\image cutting.tbx\image cutting, you can run it directly, right-click to edit to view the program source code
The source code is as follows:
Python学习交流Q群:906715085###
import sys, os, string,types
import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True
oldraster = arcpy.GetParameterAsText(0)
arcpy.AddMessage("1oldraster="+oldraster)
clipshp = arcpy.GetParameterAsText(1)
arcpy.AddMessage("2clipshp="+clipshp)
fieldname= arcpy.GetParameterAsText(2)
arcpy.AddMessage("3fieldname="+fieldname)
outworkspace= arcpy.GetParameterAsText(3)
arcpy.AddMessage("4="+outworkspace)
arcpy.CheckOutExtension("spatial")
rows = arcpy.SearchCursor(clipshp)
jfb_Select=outworkspace+"/temp.shp" #不能c:\要c:\\或者c:/
for row in rows:
try:
b=1
value=row.getValue(fieldname)
#gp.AddMessage("value="+value)
if (type(value) is types.IntType):
fieldvalue = str(value)
b=2
elif (type(value) is types.StringType):
#是否string类型
fieldvalue = value
else:
fieldvalue = str(value)
arcpy.AddMessage("fieldvalue="+fieldvalue)
if b==2:
Expression="\""+fieldname +"\" ="+fieldvalue+""
else:
Expression="\""+fieldname +"\" ='"+fieldvalue+"'" arcpy.AddMessage("Expression="+Expression+",jfb_Select="+jfb_Select+",clipshp="+clipshp)
arcpy.Select_analysis(clipshp, jfb_Select, Expression)
out_raster =outworkspace+"/"+fieldvalue+".tif"
arcpy.gp.ExtractByMask_sa(oldraster, jfb_Select, out_raster)
except Exception, ErrorDesc:
#If an error set output boolean parameter "Error" to True.
arcpy.AddError(str(ErrorDesc))if arcpy.Exists(jfb_Select):arcpy.Delete_management(jfb_Select)
Function: Use a vector data to crop an image in batches, the vector field value is the data name of the cropped image, and the format is tif
parameter:
Image batch cropping parameter setting interface
The running interface is as follows:
At last
This article ends here, and there will be no further down. If you have any friends who have learned, remember to give a like. It doesn't matter if you haven't learned, you can comment or send a private message.
I.