[GRASS-user] How to implement 2D lookup table?

Moritz Lennert mlennert at club.worldonline.be
Thu May 23 05:14:15 PDT 2013


On 23/05/13 11:04, Rainer M. Krug wrote:
>
> Moritz Lennert<mlennert at club.worldonline.be>  writes:
>
>> On 23/05/13 10:26, Rainer M Krug wrote:
>>>
>>> Hi
>>>
>>> I have two raster layers
>>>
>>> ,----
>>> | dc: CELL, 1, ..., 7, NULL
>>> | ageClass: CELL, 1,2,3,NULL
>>> `----
>>>
>>> And I want to create a third one, depending on the value in a lookup
>>> table. dc corresponds to the row, ageClass to the column,
>>>
>>> The lookup table looks as follow:
>>>
>>> ,----
>>> |     seedling young adult
>>> | dc1   425000 87500 37500
>>> | dc2   270000 55000 23500
>>> | dc3   127500 26000 11250
>>> | dc4    32500  7100  3200
>>> | dc5     5550  1220   500
>>> | dc6      605   132    55
>>> | dc7       56    12     6
>>> `----
>>>
>>> Now I want to have something like (using r.mapcalc as an example)
>>>
>>> ,----
>>> | r.mapcalc expression = "result = lookupTable[col=ageClass, row=dc]"
>>> `----
>>>
>>> and e.g. for one cell
>>>
>>> ,----
>>> | ageClass = 3
>>> | dc = 5
>>> | ==>
>>> | result = 500
>>> `----
>>>
>>> I haven't found anything like this in GRASS.
>>>
>>> I could use MASKS and then r.reclass(), or generate dynamically a
>>> complex if() construct, but both options sound a little bit
>>> awkward to me.
>>>
>>> I am scripting GRASS from R, so there is always the option of doing it
>>> in R, but there is the issue of reading the data into R and writing it
>>> back to GRASS, which takes time. As I am doing this in a simulation
>>> which runs several times, this is not an unimportant issue.
>>>
>>> Is there better way of achieving this?
>>
>> I don't see a possibility to include the lookup table into r.mapcalc,
>
> Me neither - that was just the easies to show what I want to do.
>
>> but anyway, IIUC, the result you are looking for is not a raster map,
>> but a numerical value, or ?
>
> No - the lookup would be for each cell, therefore resulting in a CELL
> raster map.
>
>>
>> My best first guess is:
>>
>> r.stats -c in=dc,ageClass
>>
>> possibly combined with a little awk to do the lookup:
>>
>> r.stats -c in=dc,ageClass | awk '{if($1==5&&  $2==3) print $3}'
>>
>> Or, if the maps are heavy, feed the data resulting from the r.stats
>> call into a new db table and then use sql queries to do the lookup. Or
>> feed the results of r.stats into an R dataframe.
>
> Hmm - sounds interesting, but also for other purposes. But as the result
> will be a raster map, I don't think this approach is usable here (unless
> I can feed the numbers back into a raster map, i.e. the inverse of
> r.stats -c)

r.cross + r.reclass ?

moritz


More information about the grass-user mailing list