[GRASS-dev] Re: [GRASS-user] 2 r.neighbors questions

Glynn Clements glynn at gclements.plus.com
Thu Oct 29 05:07:54 EDT 2009

Hermann Peifer wrote:

> I started using r.neighbors and came across the following issues:
> I have a CELL type input raster and calculate weighted sums. The
> weights are float, so the calculation result should be float, but
> r.neighbors output raster is again CELL type with truncated integers.

r.neighbors output is always of the same type as the input. This
should probably be changed to use DCELL when weight= is used. It
should also be changed for average, median, stddev, and variance in
7.0, but 6.x (without weight=) needs to maintain the existing
behaviour for compatibility.

> How do I get to rounded integers? Do I see this right: that I have to
> change the input raster to float type (multiply with 1.0?). This will
> trigger float type values in r.neighbors' output raster, which I could
> eventually round() to integer. Is this the best way to do it, or am I
> overlooking a convenient switch or something?

As you suggest, you need to use r.mapcalc float(), r.neighbors,
r.mapcalc round().

> Another question is what exactly is the reason behind the
> recommendation in r.neighbors documentation:
> > for method=sum, the sum of the weights should be 1

That's incorrect. If the sum of the weights is 1, the result will be
identical to method=average. method=sum gives sum(cell[i]*weight[i])
while method=average gives sum(cell[i]*weight[i])/sum(weight[i]).

If you are using the weights= option simply to define a custom window
shape, using a weight of 1 for cells inside the window and zero for
outside (possibly with intermediate values on the boundary) will give
results which are consistent with the built-in rectangular and
circular windows.

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-dev mailing list