Python 实现ARCGIS中的ExtractByMask


1.单个文件的extract

import arcpy
from arcpy import env
from arcpy.sa import *

arcpy.env.workspace = "E:\\Data_Ex\\FJJXtif"
arcpy.env.extent = "E:\\Data_Ex\\FJJXtif\\20006.tif"
arcpy.env.snapRaster = "E:\\Data_Ex\\FJJXtif\\20006.tif"
#inRaster = "E:\\Data_Ex\\FJJXtif\\20001.tif"
#inMaskData = "E:\\Data_Ex\\FJJXtif\\changting.shp"
inRaster = "20006.tif"
inMaskData = "changting.shp"
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Execute ExtractByMask
outExtractByMask = ExtractByMask(inRaster, inMaskData)
print "ok"
outExtractByMask.save("E:\\work\\EVI_Data\\20006")

2.多个文件的批量处理
E:\Data_Ex\FJJXtif文件夹内存放的源文件组织方式为:
2000年到2010年的多幅tif图像,以2000年为例,文件命名方式为:“20001.tif,20002.tif,……,200023.tif”每个年份23幅图像。还有一个mask,文件名为changting.shp

import arcpy
from arcpy import env
from arcpy.sa import *
import os
import os.path
import sys

arcpy.env.workspace = "E:\\Data_Ex\\FJJXtif"
rootdir = 'E:\\Data_Ex\\FJJXtif'
for dirpath,filename,filenames in os.walk(rootdir):
    for filename in filenames:
        if os.path.splitext(filename)[1] == '.tif':
            filepath = os.path.join(dirpath,filename)
            inRaster = filepath
            #arcpy.env.extent = filepath
            arcpy.env.snapRaster = filepath
            outname = filename.replace('.tif','')
           #只处理每个年份中的一部分图像
            if ((int (outname[4:])>5) & (int (outname[4:])<19)):
                inMaskData = "E:\\Data_Ex\\FJJXtif\\changting.shp"
                arcpy.CheckOutExtension("Spatial")
                outExtractByMask = ExtractByMask(inRaster, inMaskData)
                print "ok"
                outExtractByMask.save("E:\\work\\EVI_Data2\\"+outname+'.tif')
                print outname


转载自:https://blog.csdn.net/hnyzwtf/article/details/44852605

You may also like...