[GRASS-user] Python - GRASS module output, r.univar

Glynn Clements glynn at gclements.plus.com
Mon Feb 6 11:26:22 EST 2012


Johannes Radinger wrote:

> I am not sure what is the best way to find out if there are
> only NULL cells. I tried it with r.univar which gives no result
> when there are only NULL cells in a raster (and no result is acutally
> also a result :)). But how can I get that value to variable
> in the python script? In the wiki there is something stated about
> the grass.script.raster_info (e.g. for the max value):
> max = grass.raster_info(inmap)['max']

If a map contains only null cells, its minimum and maximum will be
"NULL":

	$ r.mapcalc 'foo = null()'
	$ r.info -r foo
	min=NULL
	max=NULL

Using the Python API, The 'min' and 'max' values in the result of the
raster_info() function will be None.

> Is there a special function also for the counts of cells? Or how
> can I parse the result of r.univar to a python variable?

Counting cells is far more expensive than simply determining whether
there are any non-null cells. Counting cells requires reading the
entire map, while the r.info approach only needs to read the metadata
files.

If you do need to count cells, r.stats is likely to be more efficient
than r.univar.

> My loop:
> 
> while (count > 0):
>     ...
>     ...
>     count = number of !isnull of raster A
> 
> What would you do? Suggestions are mostly welcome :)

	while grass.raster_info(inmap)['max'] is not None:
	    ...

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-user mailing list