[GRASS-dev] Re: [GRASS-user] Check if a raster map has any valid pixel

Glynn Clements glynn at gclements.plus.com
Tue Oct 26 10:46:33 EDT 2010


Kim Besson wrote:

> > > I have a python script where I need to check if an input raster map has
> > any
> > > valid pixel (at least one pixel that is not null). How can I check this
> > > (easy and quick way?) in order to be integrated in my Script?
> >
> > Check the range:
> >
> >        $ r.mapcalc --o 'foo = null()'
> >        $ r.info -r foo
> >        min=NULL
> >        max=NULL
> >
> > The range can only have min=max=NULL if there are no non-null cells.
> 
> Thank Glynn. But how can I retrieve min and max using  a Python Script?

	import grass.script as grass
	
	def isempty(map):
	    kv = grass.parse_command('r.info', flags = 'r', map = map)
	    return kv['min'] == 'NULL' and kv['max'] == 'NULL'

[CC'd to grass-dev]

You can't use grass.raster_info() as it tries to convert min and max
to floats, and raises ValueError on "NULL".

r44049 fixes this, and returns None, but this should be considered
"unstable". Arguably, it should be NaN, but AFAICT that would require
Python >= 2.6, and I don't want to impose that requirement just for
this.

For now, r44049 at least suffices for the case where you don't care
about min/max (i.e. you can still get the bounds and resolution even
if the range is null). If you actually use min/max, r44049 just moves
the exception from the call to raster_info() to the point where you
try to perform arithmetic operations on None.

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


More information about the grass-dev mailing list