Describe Extent of Raster Outside of ArcMap

3731
4
05-12-2014 07:41 AM
ColinStief1
New Contributor II
Greetings,

I'm trying to understand the help files, and I am seeing that using arcpy.Describe(object) applies to layers.

What I'm trying to do is loop through a large folder of raster files outside of ArcMap and grab their extent. My first thought was to use the Describe method, but if it applies only to layer files, that means I'd have to create a layer file (temp or not) for 900+ rasters, which is not ideal.

Is there any other method I could use that doesn't involve geoprocessing to grab the extent of raster data sitting in a folder outside of ArcMap using arcpy?

Thanks,

Colin
Tags (2)
0 Kudos
4 Replies
JamesCrandall
MVP Frequent Contributor
Derscribe works on rasters too but just use arcpy.GetRasterProperties_management method to get extents.



ws = r'C:\MyDir\MyFGDB.gdb'
arcpy.env.workspace = ws
rasters = arcpy.ListRasters()
for raster in rasters:
    desc = arcpy.Describe(raster)
    nam = desc.name
    print "Results for " + str(nam)
   
    result_top = arcpy.GetRasterProperties_management(raster, "TOP")
    result_bot = arcpy.GetRasterProperties_management(raster, "BOTTOM")
    result_left = arcpy.GetRasterProperties_management(raster, "LEFT")
    result_right = arcpy.GetRasterProperties_management(raster, "RIGHT")

    print "extent top: " + str(result_top)
    print "extet bottom: " + str(result_bot)
    print "extent left" + str(result_left)
    print "extent right" + str(result_right)

0 Kudos
ChrisSnyder
Regular Contributor III
There's 3 ways to do it. I think #1 will be the fastest though...

#1
rasterPath = r"C:\temp\test.img"
rstObj = arcpy.Raster(rasterPath)
xmin, ymin, xmax, ymax = rstObj.extent.XMin, rstObj.extent.YMin, rstObj.extent.XMax, rstObj.extent.YMax

#2
rasterPath = r"C:\temp\test.img"
dscObj = arcpy.Describe(rasterPath)
xmin, ymin, xmax, ymax = dsc.extent.XMin, dscObj.extent.YMin, dscObj.extent.XMax, dscObj.extent.YMax

#3
rasterPath = r"C:\temp\test.img"
xmin = float(arcpy.GetRasterProperties_management(raster, "LEFT").getOutput(0))
ymin = float(arcpy.GetRasterProperties_management(raster, "BOTTOM").getOutput(0))
xmax = float(arcpy.GetRasterProperties_management(raster, "RIGHT").getOutput(0))
ymax = float(arcpy.GetRasterProperties_management(raster, "TOP").getOutput(0))
0 Kudos
ColinStief1
New Contributor II
Thanks to both of you,

Very helpful.

Colin
0 Kudos
CurtisBelyea1
New Contributor

I'm sure this is long since resolved, but here goes anyway...

There is another way to skin this cat, and maybe one or two more cats.  It seems to me that with 9.3, accessing some info got harder, or at least requires a little more code.  Since I'm a terrible typist, and therefore begrudge every line of code I have to type, I searched for path of lesser resistance.  I reverted to calling the 9.2 geoprocessor. 

For example, calling the Clip_management tool:

import arcpy, arcgisscripting

gp2 = arcgisscripting.create()  ## the 9.2 geoprocessor

temperatureRaster = "Y:\\Connectivity\\temperature\\fut_mean_temp.tif"

tempDir = "Y:\\Connectivity\\scratch"

fc = 'cora_expert_CAT_buffer_2015Aug19.shp'  ## in my current workspace

gp.Clip_management(temperatureRaster,gp2.describe(fc).extent,tempDir + "\\tmpTemperature.tif","layer","#","ClippingGeometry")

There's a bunch of stuff from my specific situation that I've left out that's irrelevant, but the point is you can still call the older geoprocessors and use them to pass info.

So to get all the extents, even if using ArcGIS 10.3:

import arcpy (if you want)

import arcgisscripting

arcpy.env.overwriteOutput = True ## and any other environments you want

gp2 = arcgisscripting.create()

arcpy.env.workspace = "C://rasterDirectory"

for raster in arcpy.ListDatasets():

     raster, gp2.describe(raster).extent

That should print them all out.  And in string format, so no casting.

Another place I've found this useful is with getCount_management.  With 9.3 and higher this returns an object.  If you revert to 9.2 with the above approach, it'll still return an integer.

Sorry if it's too little, too late, rambling or if anything is missing.  Also, I don't know how to post code, so it might also look like crap.

0 Kudos