[GRASS-dev] How to use a rules file in add-on script without hardcoding the path

Moritz Lennert mlennert at club.worldonline.be
Sat Nov 24 02:26:08 PST 2012


On 23/11/12 23:45, Robin Wilson wrote:
> Hi all,
>
> I'm fairly new to GRASS, so I apologise if this is a silly question -
> but I couldn't find the answer anywhere in either the User or
> Developer Wikis. I have written a (very simple) Python add-on which
> uses the r.reclass tool. Of course, to use this tool I need to
> provide a file with the reclass details in it. I am currently having
> issues getting GRASS to find this file when I run the script and I
> would like it to do this without hard-coding the script path - ie. by
> using relative paths. I have two questions:
>
> 1. If I am running this script from somewhere outside of the GRASS
> scripts directory (eg. from somewhere in my home directory), how
> should I write my script so that it simply uses the reclass file in
> the same directory as the script? Currently my script contains the
> line:
>
> grass.run_command("r.reclass", input = options['input'], output =
> options['output'], rules = "./zerotonull.rules")
>
> where zerotonull.rules is the file that contains the declass rules,
> but this always gives an error saying it can't find that file.
>
> 2. If I want to distribute this script (either just to
> friends/colleagues or by adding it to the GRASS add-on repository),
> how should I set up my Makefile so that the reclass rules file gets
> put in the right place, and the script can then find it?

I don't have an answer to that question, but you can also define the 
rules within your python script using write.command() with the stdin 
parameter and so get rid of the file issue. I.e. something like this:

reclass_rules = "1 2 3 = 1 Forest\n4 5 = 2 Water\n6 thru 10 = 3 Built-up"

grass.write_command('r.reclass', map='YourMap', 
output='YourReclassedMap', stdin=reclass_rules)

Moritz


More information about the grass-dev mailing list