Get Cell Size from Raster Parameter?

5640
2
Jump to solution
01-14-2011 10:48 AM
DanielDillard
New Contributor II
Hi all,

I am trying to set up a script that takes a raster parameter and a polygon parameter, converts the polygon to raster, and uses the two rasters in the zonal statistics tool.  I would like the cell size of the new raster (converted from polygon) to be the same as that of the raster parameter. The describe function does not expose cell size for rasters, and I have no other ideas.  Thanks for any help or suggestions!
Tags (2)
1 Solution

Accepted Solutions
NiklasNorrthon
Occasional Contributor III
The meanCellHeight and meanCellWidth properties of the raster bands is what you are looking for.
import arcpy
raster = 'some/path/to/a/raster.tif'
description = arcpy.Describe(raster)
cellsize = description.children[0].meanCellHeight

print cellsize

The children property is documented under "Describe object properties"

View solution in original post

2 Replies
NiklasNorrthon
Occasional Contributor III
The meanCellHeight and meanCellWidth properties of the raster bands is what you are looking for.
import arcpy
raster = 'some/path/to/a/raster.tif'
description = arcpy.Describe(raster)
cellsize = description.children[0].meanCellHeight

print cellsize

The children property is documented under "Describe object properties"
KenMayner
New Contributor

For clarification purposes only, this code will work only in the python window within ArcMap and not within a stand-alone script.

If you need to run this in a separate python script (using ArcMap 10.5), you may need to add the creation of the raster element between of the declaration of the path for the raster and the Describe process.

In my case, if I wasn't creating the Raster element first, I would get an error stating that the list index was out of range cause by, according to my tests, the missing children[0] portion of the code above.

As such, below is the revised version of the code for usage within a python script.

import arcpy  

rasterpath = 'some/path/to/a/raster.tif'
raster= arcpy.Raster(rasterpath)
description = arcpy.Describe(raster)  
cellsize1 = description.children[0].meanCellHeight  # Cell size in the Y axis and / or 
cellsize2 = description.children[0].meanCellWidth # Cell size in the X axis 
 
arcpy.AddMessage(str(cellsize1))   # If you want to show the result in the processing window‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos