[GRASS-user] relative path to GISbase in Scripts

Hello,

at the moment I am connecting to the sqlite database with the use of a absolute path in a pythonscript like:

database = sqlite3.connect('/users/Johannes Radinger/Documents/GRASS-GIS/Treene_location/Treene/sqlite.db')
db = database.cursor()

I think it should be possible to set a relative Path in pyton like

database = sqlite3.connect('$relative/$path/$to/$location/sqlite.db') but how can that be done?
It is just because I want to use my script in different location and don't want to change the path each time.

/johannes

On Thu, May 12, 2011 at 1:46 PM, Johannes Radinger <jradinger@gmx.at> wrote:

Hello,

at the moment I am connecting to the sqlite database with the use of a absolute path in a pythonscript like:

database = sqlite3.connect('/users/Johannes Radinger/Documents/GRASS-GIS/Treene_location/Treene/sqlite.db')
db = database.cursor()

I think it should be possible to set a relative Path in pyton like

database = sqlite3.connect('$relative/$path/$to/$location/sqlite.db') but how can that be done?
It is just because I want to use my script in different location and don't want to change the path each time.

I think that the variable GISDBASE should work. There may be a Python
function to
read it.

Markus

Johannes Radinger wrote:

at the moment I am connecting to the sqlite database with the use of a absolute path in a pythonscript like:

database = sqlite3.connect('/users/Johannes Radinger/Documents/GRASS-GIS/Treene_location/Treene/sqlite.db')
db = database.cursor()

I think it should be possible to set a relative Path in pyton like

database = sqlite3.connect('$relative/$path/$to/$location/sqlite.db') but how can that be done?

  import grass.script as grass
  import os.path

  env = grass.gisenv()

  gisdbase = env['GISDBASE']
  location = env['LOCATION_NAME']
  mapset = env['MAPSET']

  path = os.path.join(gisdbase, location, mapset, 'sqlite.db')

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

On Sat, May 14, 2011 at 2:50 AM, Glynn Clements
<glynn@gclements.plus.com> wrote:

Johannes Radinger wrote:

at the moment I am connecting to the sqlite database with the use of a absolute path in a pythonscript like:

...

   import grass\.script as grass
   import os\.path

   env = grass\.gisenv\(\)

   gisdbase = env\[&#39;GISDBASE&#39;\]
   location = env\[&#39;LOCATION\_NAME&#39;\]
   mapset = env\[&#39;MAPSET&#39;\]

   path = os\.path\.join\(gisdbase, location, mapset, &#39;sqlite\.db&#39;\)

Added to
http://grass.osgeo.org/wiki/GRASS_and_Python#Path_to_GISDBASE

Markus

Am 14.05.2011 um 08:32 schrieb Markus Neteler:

On Sat, May 14, 2011 at 2:50 AM, Glynn Clements
<glynn@gclements.plus.com> wrote:

Johannes Radinger wrote:

at the moment I am connecting to the sqlite database with the use of a absolute path in a pythonscript like:

...

       import grass.script as grass
       import os.path

       env = grass.gisenv()

       gisdbase = env['GISDBASE']
       location = env['LOCATION_NAME']
       mapset = env['MAPSET']

       path = os.path.join(gisdbase, location, mapset, 'sqlite.db')

Added to
http://grass.osgeo.org/wiki/GRASS_and_Python#Path_to_GISDBASE

Thank you for adding it to the wiki, I think that can be useful for a lot other people.
Just two related questions:

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?

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.

/johannes

Markus
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

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@gclements.plus.com>