Finding sum of values in floating point raster

2369
6
05-10-2010 12:55 PM
MarkAndersen1
New Contributor III
I am trying to find the quickest way (in terms of processing speed) to find the total (sum) of all values in a raster.  In other words, I am looking for a tool that will add the value for all cells within a particular raster, and just give me that value as a number.  Ideally, this would spit out a table with just one row, as the "Get Raster Properties" tool does.  The "Get Raster Properties" tool in the "Raster Properties" toolbox in ArcToolbox looks like it is very close, in that it provides summary statistics for a raster, but there are two problems: 1) there is not an option for "sum"; and 2) it appears to round-off the answer to 6 decimal places, and I need much greater precision than that.

The only way I've found to do this is to use the "Zonal Statistics As Table" tool and set a dummy "zone" raster that has a single value ("1") for all pixels covering my study area, so that it is finding the statistics (including sum) for all cells in the raster.  However, this is pretty slow--mostly because I think it's comparing each pixel to see which zone it's in. 

It seems that there should be a built-in way to get a sum of all cell values in a raster.  Help!
0 Kudos
6 Replies
MarkAndersen1
New Contributor III
I should also mention that I need to do this with over 2,000 grids, so I'm hoping for something that's easy to batch either in Model Builder or simply by using the batch functionality of ArcToolbox.
0 Kudos
DanPatterson_Retired
MVP Emeritus
presuming that the mean in Get Raster Properties is determined from the sum divideded by the number of cells (avg = sum/N), then perhaps you could exploit COLUMNCOUNT * ROWCOUNT to determine N.  This would not of course work if there are nodata cells in the raster, and I would suggest that you explore this since there is no indication in the help whether nodata values are accounted for when calculating the statistical properties
0 Kudos
MarkAndersen1
New Contributor III
Dan--I thought of that. The problem is that the mean that is given by the "Get Raster Properties" is rounded to 6 decimal places. My grids contain very small values, and over 400,000,000 cells, so any rounding error at all means that such a multiplication would produce erroneous results.
0 Kudos
DanPatterson_Retired
MVP Emeritus
Ok, large grid...presuming that the output is a table, perhaps, the display is just set to 6 decimal points.  If you had any capabilities to get the raster out to a numpy array, then the summation is simple (raster shown as a list of lists)
>>> import numpy
>>> a = numpy.array([[1,1],[2,2]])
>>> a.sum()
6

and all your conversion could be done to arrays using Python as your programming platform
0 Kudos
MarkAndersen1
New Contributor III
Bill,

The number of significant digits far exceeds the number of decimal places being shown.  For example, a value of 0.000000045821  is a valid value (a probability density function, where the probability for all cells across the study area sum to 1), but would be rounded to 0.000000 as it's being displayed when I use the Get Raster Properties.  When I sum the grid values, if there is this much rounding in the calculations, they will sum to much less than 1.  That's what I mean by erroneous.

I do most of my scripting with VBA. Is this something that might be better done with Python?  I haven't done much scripting with rasters, so I'm not sure how to access the values for individual cells. Any samples you have handy for something like this?

Thanks!
0 Kudos
MarkAndersen1
New Contributor III
Bill,

Is there any reason to expect that the zonal statistics function (using a "zone" raster mask where all zone values are the same) will use single precision?  When I do it that way (use a raster layer that simply has values of "1" for all cells overlaying my original raster), the "sum" I get seems reasonable.
0 Kudos