[GRASS-user] r.mapcalc and the neighborhood modifier

Glynn Clements glynn at gclements.plus.com
Tue Jul 16 11:16:44 PDT 2013

Rainer M Krug wrote:

> I assume from that error message, that I can not supply a map go get the
> values for the neighborhood modifier, but have to use constant values.


The main reason is that, like most GRASS raster modules, r.mapcalc
works row-by-row. For each input map, it records the minimum and
maximum row offset used in neighbourhood modifiers; it does this
before any maps are opened.

In theory, there's no reason why the column offset can't be an
arbitrary expression (or an index instead of an offset), but that
isn't currently supported.

Given how r.mapcalc works, it should be fairly simple to add a
function to allow unrestricted column indexing, e.g.
column(data, index) where both data and index are arbitrary

Allowing the row offset to be an arbitrary expression would be
somewhat harder to implement (or to implement efficiently, at least),
as r.mapcalc would need to be able to fetch rows on-demand based upon
the results of calculations, and to know when each row was no longer

A more restricted form, where the row offset is simply the value taken
from a map would be more straightforward, as the code which determines
the range of offsets could examine the range of the map used for the
row offset. However, this would run the risk of feature creep, i.e. a
demand for extending the offset map to map+offset up to arbitrary
expressions involving maps and constants.

An alternative option would be to allow arbitrary expressions but with
explicit constant bounds.

However, while these options are viable, neither is exactly trivial.

> I could put these into if statements, but I wanted to avoid these if
> possible.
> Is there a way that I can use the offset from a map?

If the range of possible offsets is small (e.g. {-1,0,+1}), I'd go
with "if" statements.

Otherwise, you might want to look into using one of the Python APIs (I
think we have four now: grass.script, grass.script.array, grass.lib
and grass.pygrass).

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-user mailing list