其他
ArcGIS Python | 矢量影像批量裁剪、合并
矢量批量裁剪的代码
在:chp10\python\矢量批量裁剪.tbx\矢量批量裁剪,可以直接运行,右键编辑查看代码
代码如下:
# ---------------------------------------------------------------------------
# 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 arcpy
from 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.CatalogPath
p=filepath.find(".mdb")
ftype="String"
for field in desc.fields:
if field.Name ==fieldname:
ftype=field.Type
break
arcpy.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\影像切割,可以直接运行,右键编辑可以查看程序源代码
源代码如下:
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
参数:
影像批量裁剪参数设置界面
运行界面如下:
影像批量裁剪运行界面
文章授权转载:gisoracle
- END -
Python | ArcPy批量更新图形面积
ArcGIS 基准面介绍和坐标系分类
如何做一张漂亮的高程、坡度、坡向分析图(附练习数据下载)
羊了个GIS