number of no NoData cells in raster

2243
6
01-11-2012 10:21 PM
martinmaretta
Occasional Contributor
Hi is there some way how to find out number of no NoData cells in raster? (without looping whole raster)  thanks a lot.
Tags (2)
0 Kudos
6 Replies
RaphaelR
Occasional Contributor II
there´s probably a better way to do this, but i´d try with numpy:

import arcpy
import numpy as np
import numpy.ma as ma

inRaster = r"E:\testraster.tif"

# convert inraster to numpy array, set nodata values to -9999
npArray = arcpy.RasterToNumPyArray(inRaster,"","","",-9999)
    
# mask the non-nodata values, and take them out by compressing
npM = ma.masked_where(npArray <> -9999, npArray).compressed()
    
# print how many cells are nodata    
print len(npM)
0 Kudos
RaphaelR
Occasional Contributor II
maybe with numpy?

import arcpy
import numpy as np
import numpy.ma as ma


inRaster=r"E:\test1.tif"
# convert inraster to numpy array, set nodata values to -9999
npArray = arcpy.RasterToNumPyArray(inRaster,"","","",-9999)
    
# mask the non-nodata values, and take them out by compressing
npM = ma.masked_where(npArray <> -9999, npArray).compressed()
    
print npM.size
    

0 Kudos
AndrewChapkowski
Esri Regular Contributor
You might be able to use the bincount() function on numpy to do what you are looking for:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.bincount.html
the values have to be non-negative though.
0 Kudos
DanPatterson_Retired
MVP Emeritus
Check out the IsNull tool in the spatial analyst toolset, it will produce a grid of 0's or 1's, simply open its table to get the cell count.
0 Kudos
curtvprice
MVP Esteemed Contributor
If it's an integer raster, you can calculate the number of data cells by summing up COUNT in the raster table and subracting it from rows * columns.

For floating grids, you must convert the cells to 0/1 with IsNull and sniff the COUNT value from the output raster RAT (I'd do that with a cursor for a table view generated for VALUE = 1).
0 Kudos
martinmaretta
Occasional Contributor
Thanks a lot, all of you
0 Kudos