Can't run grass tools from python script (grass 6.4, python 2.7, win7) -


i new grass, , not manage started. trying write python script call grass tool (at point not matter tool) created location , mapset interactively, , set definitions. still can't 'grass.gisenv()' give output {}.

my code:

import sys import os  os.environ['gisbase'] = r'c:\osgeo4w\apps\grass\grass-6.4.4' os.environ['gisrc']= r'c:\users\user\appdata\roaming\grass6\grassrc6' os.environ['ld_library_path']= r'c:\osgeo4w\apps\grass\grass-6.4.4\lib' os.environ['path']= r'c:\osgeo4w\apps\grass\grass-6.4.4\etc;c:\osgeo4w\apps\grass\grass-6.4.4\etc\python;c:\osgeo4w\apps\grass\grass-6.4.4\lib;c:\osgeo4w\apps\grass\grass-6.4.4\bin;c:\osgeo4w\apps\grass\grass-6.4.4\extralib;c:\osgeo4w\apps\grass\grass-6.4.4\msys\bin;c:\python27;' os.environ['pythonlib']= r'c:\osgeo4w\apps\python27' os.environ['pythonpath']= r'c:\osgeo4w\apps\grass\grass-6.4.4\etc\python'   sys.path.append(os.path.join(os.environ['gisbase'], 'etc', 'python'))  gisbase = os.environ['gisbase'] #= "/usr/local/src/grass_trunk/dist.x86_64-unknown-linux-gnu"  gisdbase = os.path.join(r'c:\users\user\documents', "grassdata") location = "israel" mapset   = "permanent"  import grass.script grass import grass.script.setup gsetup  gsetup.init(gisbase,gisdbase, location, mapset)  print grass.gisenv() 

i no error. "{}".

please help

there ways how improve code, example, add environmental variables rather set them:

os.environ['path'] += os.pathsep + os.path.join(gisbase, 'bin') 

then gisrc variable should point temporary file, rather file in user settings directory (in case want run grass session normal user). anyway, suggest start on new script or approach.

first, use grass gis 7 if possible. version 7.0.0 released several months ago , available ms windows both standalone , part of osgeo4w. might better error messages, example.

second, review related wiki page. contains detailed guide needs done extensive code samples. here extraction of basic things 1 of them (for ms windows only):

import os import sys import subprocess  # path grass gis launch script grass7bin = r'c:\osgeo4w\bin\grass70svn.bat' # uncomment when using standalone wingrass installer # grass7bin = r'c:\program files (x86)\grass gis 7.0.0\grass70.bat'  # query grass 7 gisbase startcmd = [grass7bin, '--config', 'path']  p = subprocess.popen(startcmd, shell=false,                      stdout=subprocess.pipe, stderr=subprocess.pipe) out, err = p.communicate() if p.returncode != 0:     print >>sys.stderr, "error: cannot find grass gis 7 start script (%s)" % startcmd     sys.exit(-1) gisbase = out.strip('\n\r') # replaced using right gisbase # directly instead of executable  # set gisbase environment variable os.environ['gisbase'] = gisbase  # define grass-python environment gpydir = os.path.join(gisbase, "etc", "python") sys.path.append(gpydir)  # data gisdb = os.path.join(os.path.expanduser("~"), "documents/grassdata")  # specify (existing) location , mapset location = "nc_spm_08" mapset = "user1"  # import grass python bindings import grass.script gscript import grass.script.setup gsetup  # launch session gsetup.init(gisbase,             gisdb, location, mapset) 

however, note best how call grass gis functionality (modules , python libraries) in python within grass session. usual workflow write script relies on right environment being set, i.e. calls grass functionality right away. start grass gives right environment - grass session script can run.

also, there a new option in development version of grass gis. in case doing non-production development (yet) or writing experimental, can try use daily build of development version (subversion trunk, future 7.1) contains possibility specify script in command line of grass gis executable. call might like:

grass7bin = r'c:\program files (x86)\grass gis 7.0.0\grass70.bat' gisdb = os.path.join(os.path.expanduser("~"), "documents/grassdata") location = "nc_spm_08" mapset = "user1" full_mapset = os.path.join(gisdb, location, mapset) cmd = [grass7bin, full_mapset, '--exec', 'your_script.py'] p = subprocess.popen(cmd, shell=false,                      stdout=subprocess.pipe, stderr=subprocess.pipe) out, err = p.communicate() 

the advantage of solution above proper locking of mapsets on linux , mac os x plus several other things starting correctly set grass environment without work. need know path executable or have executable "on path".

then there option similar previous 1 works in older versions. can set grass_batch_job environmental variable. grass process check , execute script specified in variable.

grass7bin = r'c:\program files (x86)\grass gis 7.0.0\grass70.bat' gisdb = os.path.join(os.path.expanduser("~"), "documents/grassdata") location = "nc_spm_08" mapset = "user1" full_mapset = os.path.join(gisdb, location, mapset) os.environ['grass_batch_job'] = 'your_script.py'  cmd = [grass7bin, full_mapset] p = subprocess.popen(cmd, shell=false,                      stdout=subprocess.pipe, stderr=subprocess.pipe) out, err = p.communicate() # delete variable sure # using separate env more elegant del os.environ['grass_batch_job'] 

unlike option above (passing script arguments) cannot pass parameters called script nor call grass modules directly.

finally, can set environmental variables in system , have grass session available. grass modules run normal programs when right gisrc variable , file provided. option may potentially cause many problems, e.g. when using 2 version of grass gis, not recommended. however, possible in between: add grass executable path, should able call grass grass70 done on linux (the path can found in properties of grass gis icon).


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -