[GRASS-user] relative path to GISbase in Scripts
Glynn Clements
glynn at gclements.plus.com
Mon May 16 00:59:35 EDT 2011
Johannes Radinger wrote:
> 1) I think it is not possible but I just want to ask: Can that method be anyhow used also for the
> g.parser options? I tried:
>
> #%Option
> #% key: PS_map
> #% type: string
> #% required: no
> #% multiple: no
> #% key_desc: name
> #% description: Name for PostScript output file
> #% gisprompt: new_file,file,output
> #% guisection: Optional
> #% answer: os.path.join(gisdbase, location, mapset, 'PS_map_result')
> #%end
>
> But for sure this is just a string and therefore doesn't work. But is it somehow possible to use
> relative information about the path to a file in the answer-option for g.parser?
No. Even when it's possible (i.e. in C modules), dynamically setting
defaults is a bad idea, as the option information is used to generate
the documentation.
Instead, I would suggest allowing GRASS variables to appear in the
option value, and expanding them at run-time, e.g.:
...
#% answer: $GISDBASE/$LOCATION_NAME/$MAPSET/PS_map_result
...
import string
import os.path
import grass.script as grass
...
def main():
PS_map = string.Template(options['PS_map']).substitute(grass.gisenv())
PS_map = os.path.normpath(PS_map)
BTW: option names shouldn't contain upper-case letters (r.terraflow's
STREAM_DIR= is a bug which wasn't caught until it was too late to
change). 7.x won't recognise option names containing upper-case
letters.
> 2) How can I get the path to the script-file i am running at the
> moment in GRASS? like ./././GRASS/6.5/Modules/bin/script.py? This
> should work on every operating system.
import os.path
print os.path.abspath(__file__)
--
Glynn Clements <glynn at gclements.plus.com>
More information about the grass-user
mailing list