Comparing extents of Rasters for selection

641
2
08-14-2011 11:08 PM
George_ChandeepCorea
New Contributor III
I am writing a script that takes an input of an Area of Interest (AOI) and then walks through the base directories to find any file that has an extent that intersects with the AOI, clip it and generate a text output file. I've got most of it working except comparing the raster extent to the AOI extent. It is not giving errors but just doesn't work...

Any advise?

I am also having issues with trying to clip the extent to a polygon, it clips but maintains the MAX/MIN and doesn't clip to the mask. I have tried three methods as in the comments in the script.

Extract by Mask says I don't have Spatial Analyst but it is on.

Thanks guys,


import arcpy, glob, os, sys, string
from arcpy import env
from arcpy.sa import *

RootDirectories = [r'P:\2011\Job_154_PythonScript_for_AOI\Working\Orthophotomosaic', r'P:\2011\Job_154_PythonScript_for_AOI\Working\Contours']
RasterTypes = ['tif','jpg'] #boolean, valuelist

path = os.getcwd()
os.chdir(path)
#AOI = arcpy.mapping.Layer('AOI.shp')
#AOIextent=AOI.getExtent()
AOI = 'AOI.tif'
AOIobj=arcpy.Raster(AOI)
AOIextent=AOIobj.extent

#print AOIextent
SR = 0
x=0
for RootDirectory in RootDirectories:
    for root, dirs, files in os.walk(RootDirectory):
        for RasterType in RasterTypes:
            FileList = [os.path.join(root, fi) for fi in files if fi.endswith(RasterType)]
            for File in FileList:
                FileDesc=arcpy.Describe(File)
                SR = FileDesc.spatialReference
                if SR.name != "Unknown":
                    FileObj = arcpy.Raster(File)
                    FileExtent = FileObj.extent
                    print File, FileExtent
                    if FileExtent.within(AOIobj.extent):
                        print "WITHIN extent"

                    else:
                        print "outside extent"
                        x=x+1
# NOTE: The following should be executed in the within loop above but is here as the extent comparison isn't working

                        f = open(path+'\\AOI_Clip\\AOI_SubsetGenerator.txt', 'a')
                        f.write(str(x)+","+str(FileDesc.baseName)+","+str(FileDesc.extension)+","+str(FileDesc.dataType)+","+str(FileDesc.path)+","+str(SR.name)+"\n")
                        f.close()
                        outRaster=path+'\\AOI_Clip\\'+str(FileDesc.baseName)+"^AOI_Clip"+str(x)+"."+str(FileDesc.extension)
#Method 1
                        #arcpy.Clip_management(File, "#", outRaster, AOI, "0", "ClippingGeometry")
#Method 2
                        desc = arcpy.Describe("AOI.shp")
                        frame = str(desc.extent)
                        arcpy.Clip_management (File, frame, outRaster)
#Method 3
                        #outExtractByMask = ExtractByMask(File, 'AOI.shp')
                        #outExtractByMask.save(OutRaster)
        
                        print "Created: " + str(outRaster)
            print "Changing Directory to: " + str(dirs)
            
print "Exiting"
Tags (2)
0 Kudos
2 Replies
PatrickJurgens
New Contributor III
You may need to use the function arcpy.CheckOutExtension("Spatial") in order for arcpy to realize that you have the spatial analyst extension.
If this works, then you will want to use the CheckInExtension() function when you are done in order to let go of the license.
Details and an example are provided at:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/CheckOutExtension/000v0000003q000000/
0 Kudos
George_ChandeepCorea
New Contributor III
Thanks. It works fine now once the extension is on. I didn't know that arcpy had to turn these on.

best,
0 Kudos