[GRASS-user] Mapcalc for rounding float values

Rebecca wrote:

I have a map of local elevation that I would like to alter the
values of and re-class as a experiment to aid visualisation &
interpretation.

firstly, the r.reclass module might help, as may r.contour.

1) Currently the cells are floating point with z-values like
0.0151666768416021 - I would like to do is round the elevation
values to 2dp.

there are a couple ways to do it, here's one:
  r.mapcalc "map.2dp = int(0.5 + (map.15g * 100))/100.0"

the 0.5 is added because int() truncates instead of rounding.

another common way is with log(mapname,10), but the above
may be a little more obvious.

2) I also like to reclassify the resulting map into 5cm wide
elevation ranges (reflecting the accuracy of the original data
capture) e.g. 0-4.90cm, 5-9.90cm etc

erhm, I'm not exactly sure if that is a valid approach to look
at the problem of instrument quantization (I'm fairly sure it
isn't, both technically and fundamentally), but it is probably a
job for r.reclass if you really want to do it; or adjust the
multiply by--truncate--divide by r.mapcalc trick above to get
it to round to whatever interval you like.

Hamish

Hamish wrote:

there are a couple ways to do it, here's one:
r.mapcalc "map.2dp = int(0.5 + (map.15g * 100))/100.0"

the 0.5 is added because int() truncates instead of
rounding.

mind that if your values go negative, int() and floor() are
not the same..

another common way is with log(mapname,10), but the above
may be a little more obvious.

maybe that way still helps though.

Hamish

Hamish wrote:

there are a couple ways to do it, here's one:
  r.mapcalc "map.2dp = int(0.5 + (map.15g * 100))/100.0"

the 0.5 is added because int() truncates instead of rounding.

Note that r.mapcalc has a round() function.

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

Hi,

On Fri, Feb 15, 2013 at 2:09 PM, Glynn Clements <glynn@gclements.plus.com> wrote:

Hamish wrote:

there are a couple ways to do it, here’s one:
r.mapcalc “map.2dp = int(0.5 + (map.15g * 100))/100.0”

the 0.5 is added because int() truncates instead of rounding.

Note that r.mapcalc has a round() function.

Also, note that if you want non integer reclassified output you might want to use r.recode instead of r.reclass.

cheers, madi

Margherita DI LEO
Postdoctoral Researcher

European Commission - DG JRC
Institute for Environment and Sustainability (IES). Unit H03 – FRC
Via Fermi, 2749
I-21027 Ispra (VA) - Italy - TP 261

Tel. +39 0332 78 3600
margherita.di-leo@jrc.ec.europa.eu

Disclaimer: The views expressed are purely those of the writer and may not in any circumstance be regarded as stating an official position of the European Commission.