查看原文
其他

ArcGIS Python ​影像批量裁剪

GIS前沿 2022-12-30

该工具在:“14综合\工具箱.tbx\影像裁剪\按记录批量裁剪影像”,影像数据按矢量面要素批量裁剪,界面如图14-5所示。

图14-5 影像批量裁剪

按一个矢量面数据,按字段值相同的融合在一起裁剪影像,字段值是裁剪后的影像名字,如果是数字开头,自动加T,如果其他特殊字符,会自动替换;输出工作空间是数据库,就是数据库格式,如果文件夹,可以是.tif文件或.img文件,影像裁剪可以使用空间分析中arcpy.sa.ExtractByMask,按掩膜提取;可以使用数据管理下Clip_management,一定把使用输入要素裁剪要素勾上,如图14-6所示。

图14-6 影像的裁剪

#coding=utf8
import sys, os, string,types
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

arcpy.env.overwriteOutput =
True
oldraster  = arcpy.GetParameterAsText(0)
clipshp  = arcpy.GetParameterAsText(
1)
fieldname= arcpy.GetParameterAsText(
2)
outworkspace= arcpy.GetParameterAsText(
3)
outdesc = arcpy.Describe(outworkspace)

ext= arcpy.GetParameterAsText(
4)

if outdesc.dataType== "Workspace":
    ext=
""
elif not outdesc.dataType=="Folder":#FeatureDataset FeatureLayer
   
arcpy.AddError(u"格式错误无法裁剪")
   
pass
arcpy.CheckOutExtension("spatial")
desc = arcpy.Describe(clipshp)
shapeName = desc.shapeFieldName
#shape字段

result = arcpy.GetCount_management(clipshp)
num=
int(result.getOutput(0))
if num <= 0:
    arcpy.AddMessage(clipshp+
u"没有数据")
   
pass
uniqueList=getuniqueValue(clipshp,fieldname)
Dissolveb=
False;#是否融合
num1=len(uniqueList)
if not num==num1:
    arcpy.AddMessage(
u"由于"+fieldname+u"字段不是唯一值,软件做了融合处理,"
   
+"你看到几个和最终结果个数不一致,原始有"
   
+str(num)+u",最后输出只有"+str(num1)+u"")
    outnewshp = arcpy.CreateUniqueName(
"yl_temp") #临时
   
arcpy.Dissolve_management(clipshp, outnewshp, [fieldname], "", "MULTI_PART","DISSOLVE_LINES")
    num=
len(uniqueList)
    clipshp= outnewshp
    Dissolveb=
True
arcpy.SetProgressor("step", u"正在裁剪",0,num,1)
rows = arcpy.SearchCursor(clipshp)

i=
0

for row in rows:

   
try:
        i+=
1
       
arcpy.SetProgressorPosition()
        arcpy.SetProgressorLabel(
u"正在裁剪....,完成:"+str(i*100/num)+"%" )
        fieldvalue=
str(row.getValue(fieldname))
       
if fieldvalue==None:
            fieldvalue=
"None"


       
geometry=row.getValue(shapeName)
       
if (outdesc.dataType== "Workspace"):
            outFC = arcpy.ValidateTableName(fieldvalue+ext,outworkspace)
            out_raster =outworkspace+
"/"+outFC
       
else:
            out_raster=outworkspace+
"/"+fieldvalue+ext
        arcpy.AddMessage(
u"正在裁剪:"+out_raster);
       
#out_raster =outworkspace+"/"+fieldvalue+ext

        #arcpy.sa.ExtractByMask(oldraster, geometry, out_raster)
       
arcpy.Clip_management(oldraster,"#",out_raster,geometry,  "#", "ClippingGeometry")
        arcpy.env.pyramid =
"PYRAMIDS 3 BILINEAR JPEG"
       
arcpy.BuildPyramids_management(out_raster)
   
except Exception, ErrorDesc:
       
#If an error set output boolean parameter "Error" to True.
       
arcpy.AddError(str(ErrorDesc))
arcpy.ResetProgressor()
if Dissolveb:
   
if arcpy.Exists(clipshp):
        arcpy.Delete_management(clipshp)

if row:
   
del row
if rows:
   
del rows


文章授权转载:gisoracle


- END -


ArcGIS中加载各种在线地图
李德仁院士:关于推进实景三维中国建设的若干思考
全球矢量任意下载!路网、建筑、水系...
自动化和半自动矢量化提取地物矢量轮廓
420平方公里实景三维模型!苏州正在打造一座数字孪生城

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存