[GRASS-dev] pyGRASS and mapcalc's pixel modifiers

Moritz Lennert mlennert at club.worldonline.be
Wed May 6 06:52:55 PDT 2015


On 06/05/15 14:42, Nikos Alexandris wrote:
> * Moritz Lennert <mlennert at club.worldonline.be> [2015-05-06 12:59:16 +0200]:
>
>> On 06/05/15 12:25, Nikos Alexandris wrote:
>>> Hello programmers.
>>>
>>> Consider a Python class with methods that derive a somewhat complex mapcalc
>>> expression which contains the following pixel modifiers:
>>>
>>> ['A[-1, -1]', 'A[-1, 0]', 'A[-1, 1]', 'A[0, -1]', 'A[0, 0]', 'A[0, 1]',
>>> 'A[1, -1]', 'A[1, 0]', 'A[1, 1]']
>>>
>>>
>>> These modifiers built-up a moving window to compute some values (eg. mean, median and other in-between values) and finally end up in a polynomial like:
>>>
>>> -9.674 + 0.653 * SomeValue + 9.087 * SomeValue^2
>>>
>>>
>>> As far as my understanding goes, I can't check for the validity of the
>>> in-between computed values with mapcalc's powers.  For example, check if the
>>> average value as derived from the above defined pixel modifiers, lies within a
>>> specific range.  I have build helper functions, as part of the Python class,
>>> which could do this.  Then again, I don't have the powers of mapcalc to process
>>> the data.
>>>
>>> I wonder how I should approach this, excluding rewriting my program in C (which
>>> I don't speak easily).  If I go step-by-step, that is:
>>>
>>> 1) compute a map based with the mean of neighboring pixels (see pix-modifiers above)
>>> 2) use pyGRASS' RasterRow, for example, to be able to apply some helper function and check or do something
>>> 3) compute another, required, in-between map
>>> 4) use pyGRASS again
>>> 5) etc.
>>>
>>> up until to reach the final equation of interest.  This sounds really messy.  How would you approach this (excluding an
>>> implementation in C)?
>>
>> Have a look at the eval function of r.mapcalc (check the specific
>> section of the man page). It allows you to create intermediate results
>> which you can then use in further processing.
>
> Thank you Moritz.  Already in use.   And quite happy about it.  eval, anyhow,
> isn't an answer to my question.
>
> A simple example of what would satisfy the current need, would be the
> application of a helper function, like the following, on the fly.  That is
> while reading or computing pixel values.
>
> def check_cwv(cwv):
>      """
>      Check whether a column water vapor value lies within a "valid" range.
>      """
>      if cwv < 0.0 or cwv > 6.3:
>          raise ValueError('The column water vapor estimation is out of the '
>                           'expected ranfe [0.0, 6.3]')
>      else:
>          return True
>

It's clear that you cannot raise an error with such a message in 
r.mapcalc, but I don't understand why you couldn't do such a test (as 
long as this test is pixel by pixel) ? Instead of the message you could 
give those pixels a specific value (e.g. -1 or -9999 or whatever).

But I guess I just don't really understand what you are after...

Moritz


More information about the grass-dev mailing list