[GRASS-dev] possible bug on r.mapcalc round() G7

Glynn Clements glynn at gclements.plus.com
Sat Mar 16 16:17:48 PDT 2013


Margherita Di Leo wrote:

> trying to rescale a map to 0:255 range, I run:
> 
> r.univar -g map
> n=9599576
> null_cells=0
> cells=9599576

> min=-1.55537462234497  <== ***NOTE ***

> max=274.231811523438
> range=275.787186145782
> 
> taking the max, i run:
> 
> r.mapcalc "map.invscaled = round((1.0 - map / 274.231811523438) * 255.0)"

The minimum value of the map is negative (-1.555...).

Your expression maps the maximum value to 0 and zero to 255, so the
minimum value will be mapped to slightly over 255. To be precise:

	round((1.0 - (-1.55537462234497 / 274.231811523438)) * 255.0)
=	round((1.0 - -0.005671751259288296) * 255.0)
=	round(1.0056717512592883 * 255.0)
=	round(256.4462965711185)
=	256

See Markus' reply for an alternative which maps the minimum and
maximum rather than zero and the maximum.

But note that it doesn't partition the data into 256 equal "bins", due
to the use of round(). The first and last bins (0 and 255) only cover
half the range of the other bins. The first and last bins each cover
1/510 of the range, while the others cover 1/255.

If you want equal-sized bins, use:

r.mapcalc "map.invscaled = int((1.0 - ((map - $min) / $range)) * 255.99999999999)"

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


More information about the grass-dev mailing list