前言
又到每日分享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
参数:
影像批量裁剪参数设置界面
运行界面如下:
最后
这篇文章到这里就结束了,再往下面也没有了,有没有学会的小伙伴,记得点一个赞,没有学会的也没关系,可以评论或者私信
我。
来源:https://www.cnblogs.com/123456feng/p/16171974.html
本站部分图文来源于网络,如有侵权请联系删除。