前言 又到每日分享Python小技巧的時候了,今天給大家分享啥呢?大家要不要猜一猜,今天給大家分享矢量影像批量裁剪以及合併。 聽起來有點不懂,看完你就懂了,擴展知識。 矢量批量裁剪的代碼 在:chp10\python\矢量批量裁剪.tbx\矢量批量裁剪,可以直接運行,右鍵編輯查看代碼 代碼如下: P ...
前言
又到每日分享Python小技巧的時候了,今天給大家分享啥呢?大家要不要猜一猜,今天給大家分享矢量影像批量裁剪以及合併。
聽起來有點不懂,看完你就懂了,擴展知識。
矢量批量裁剪的代碼
在:chp10\python\矢量批量裁剪.tbx\矢量批量裁剪,可以直接運行,右鍵編輯查看代碼
代碼如下:
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)
作用:使用一個矢量圖層批量裁剪多個矢量數據,欄位值是裁剪後資料庫名稱
批量裁剪參數設置界面
界面如下:
批量裁剪運行界面
是否mdb,勾上是mdb,不勾是gdb
◾矢量批量合併
在:chp10\python\批量合併.tbx\數據批量合併下,可以直接運行,右鍵編輯可以查看代碼
代碼如下:
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())
參數如下,工作空間可以是地理資料庫(文件地理數據或者個人地理數據),也是可以一個文件夾,這裡建議是資料庫
批量合併參數設置界面
運行界面如下:
批量合併運行界面
◾影像批量裁剪
在:chp10\python\影像切割.tbx\影像切割,可以直接運行,右鍵編輯可以查看程式源代碼
源代碼如下:
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)
作用:使用一個矢量數據,批量裁剪一個影像,矢量欄位值是裁剪後影像的數據名,格式為tif
參數:
影像批量裁剪參數設置界面
運行界面如下:
最後
這篇文章到這裡就結束了,再往下麵也沒有了,有沒有學會的小伙伴,記得點一個贊,沒有學會的也沒關係,可以評論或者私信
我。