[GRASS-user] How to create+use reclass or recode rules in a python script

I'm pretty sure someone has already answered this, but cannot get my hands on the relevant mail: which grass python lib function should I use and how in order to be able to feed r.recode, r.reclass, r.colors, etc rules on the fly ?

Moritz

Moritz Lennert wrote:

I'm pretty sure someone has already answered this, but cannot get my
hands on the relevant mail: which grass python lib function should I use
and how in order to be able to feed r.recode, r.reclass, r.colors, etc
rules on the fly ?

feed_command() or write_command().

The former returns a Popen object, so you can write to its .stdin
member. You need to call p.stdin.close() and p.wait() once you're
done.

The latter accepts the input as a string via the stdin= parameter,
waits for completion and returns the exit status.

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

On 01/12/11 07:40, Glynn Clements wrote:

Moritz Lennert wrote:

I'm pretty sure someone has already answered this, but cannot get my
hands on the relevant mail: which grass python lib function should I use
and how in order to be able to feed r.recode, r.reclass, r.colors, etc
rules on the fly ?

feed_command() or write_command().

The former returns a Popen object, so you can write to its .stdin
member. You need to call p.stdin.close() and p.wait() once you're
done.

The latter accepts the input as a string via the stdin= parameter,
waits for completion and returns the exit status.

Thanks, write_command() is perfect for me !

Just one question using it with r.recode:

When I use it without a rules parameter it works, but when I use it with rules='-' it gives me 'ERROR :Unable to open file <->'. Is this expected behaviour ?

Moritz

Moritz wrote:

Just one question using it with r.recode:

When I use it without a rules parameter it works, but when
I use it with
rules='-' it gives me 'ERROR :Unable to open file
<->'. Is this expected behaviour ?

it is a bug. apparently the option description got backported
from trunk but the actual code to check for the '-' didn't (yet).

Hamish

Moritz wrote:
> Just one question using it with r.recode:
>
> When I use it without a rules parameter it works, but when
> I use it with rules='-' it gives me 'ERROR :Unable to open file
> <->'. Is this expected behaviour ?

it is a bug. apparently the option description got backported
from trunk but the actual code to check for the '-' didn't
(yet).

r.recode fixed in svn.

There are a number of other changes to backport for r.reclass, so for that
one I'll hold off for 6.4.3 then merge them in order.

Hamish