[GRASS-dev] Use Global variables at Scripts (Python)

Hi

I’m programming a few Scripts and I would like to know How can I access some global variables value such as GRASS_DIR, MAPSET; LOCATIOn or even others that I defined (e.g. GRASS_TEMP). How can I load and access them? (I’m working with Python scripts for GRASS 7)

Thanks

Best regards
Luis

In data giovedì 25 febbraio 2010 19:34:25, Luis Lisboa ha scritto:
: > Hi

I'm programming a few Scripts and I would like to know How can I access
some global variables value such as GRASS_DIR, MAPSET; LOCATIOn or even
others that I defined (e.g. GRASS_TEMP). How can I load and access them?
(I'm working with Python scripts for GRASS 7)

You could do something like:

---------------------------------
#!/usr/bin/python

import grass.script as grs

# get info about my location and mapset
env = grs.gisenv()
pathbase = env['GISDBASE']
location = env['LOCATION_NAME']
mapset = env['MAPSET']

# get absolut path to actually mapset
pathmapset=path.join(pathbase, location, mapset)

# get rasters
rasters=grs.list_grouped('rast')
# get vectors
vectors=grs.list_grouped('vect')

------------------------------------

may be there are something better...

Pietro

Luis Lisboa wrote:

I'm programming a few Scripts and I would like to know How can I access some
global variables value such as GRASS_DIR, MAPSET; LOCATIOn or even others
that I defined (e.g. GRASS_TEMP). How can I load and access them? (I'm
working with Python scripts for GRASS 7)

Environment variables can be accessed using e.g.:

  gisbase = os.environ["GISBASE"]
or:
  gisbase = os.getenv("GISBASE")

The main difference between the two is that the former raises an
exception if the variable doesn't exist while the latter returns None
(or a user-defined value specified by the second argument).

$GISRC variables (GISDBASE, LOCATION_NAME, MAPSET) can be accessed via
grass.script.gisenv(), e.g.

  import grass.script as grass
  env = grass.gisenv()
  mapset = env["MAPSET"]

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