本文介绍了无法从python脚本(草6.4,python 2.7,win7)运行草工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是GRASS的新手,我无法开始.我正在尝试编写一个将调用grass工具的python脚本(在那一点上,什么工具都没有关系)我以交互方式创建了位置和地图集,并设置了所有定义.仍然无法获得'grass.gisenv()'来提供除{}以外的任何输出.

I am new to GRASS, and i do not manage to get started.I am trying to write a python script that will call grass tool (at that point it does not really matter what tool)I created the location and mapset interactively, and set all the definitions. still I can't get 'grass.gisenv()' to give any output but {}.

我的代码:

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 as grass
import grass.script.setup as gsetup

gsetup.init(gisbase,gisdbase, location, mapset)

print grass.gisenv()

我没有错误.只有"{}".

I get no error. only "{}".

请帮助

推荐答案

有一些方法可以改善代码,例如,添加环境变量而不是设置它们:

There are some ways how to improve your code, for example, add to environmental variables rather then set them:

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

然后,GISRC变量应指向一个临时文件,而不是用户设置目录中的文件(如果要以普通用户身份运行GRASS会话).但是无论如何,我建议从新的脚本或方法开始.

Then the GISRC variable should point to a temporary file, rather then the file in user settings directory (in case you want to run GRASS session as a normal user). But anyway, I suggest to start over with a new script or approach.

首先,请尽可能使用GRASS GIS 7. 7.0.0版是几个月前发布的,它既可以作为独立版本也可以作为OSGeo4W的一部分用于MS Windows.例如,您可能会得到更好的错误消息.

First, use GRASS GIS 7 if possible. Version 7.0.0 was released several months ago and it is available for MS Windows both as standalone and as part of OSGeo4W. You might get better error messages, for example.

第二,查看.它包含详细的指南,需要对大量的代码示例进行处理.以下是其中一项的基本内容摘录(仅适用于MS Windows):

Second, review related wiki page. It contains a detailed guide what needs to be done with extensive code samples. Here is an extraction of basic things from one of them (for MS Windows only):

import os
import sys
import subprocess

# path to the 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 itself for its 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')
# this could be replaced by using the right gisbase
# directly instead of the 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 and mapset
location = "nc_spm_08"
mapset = "user1"

# import GRASS Python bindings
import grass.script as gscript
import grass.script.setup as gsetup

# launch session
gsetup.init(gisbase,
            gisdb, location, mapset)

但是,请注意,在Python中调用GRASS GIS功能(模块和Python库)的最佳方法是在GRASS会话中进行.因此,通常的工作流程是编写一个脚本,该脚本依赖于所设置的正确环境,即它立即调用GRASS功能.然后启动GRASS,为您提供正确的环境-可以在其中运行脚本的GRASS会话.

However, note that the best how to call GRASS GIS functionality (modules and Python libraries) in Python is from within a GRASS session. So the usual workflow is that you write a script which relies on the right environment being set, i.e. it calls GRASS functionality right away. Then you start GRASS which gives you the right environment - GRASS session where the script can run.

此外,还有一个新选项在GRASS GIS的开发版本中.如果您正在进行一些非生产开发或正在编写实验性产品,则可以尝试使用开发版本的日常构建(Subversion干线,将来的7.1),该版本可以在GRASS的命令行中指定脚本GIS可执行文件.呼叫可能看起来像:

Also, there is a new option in the development version of GRASS GIS. In case you are doing some non-production development (yet) or you are writing something experimental, you can try to use daily build of development version (Subversion trunk, future 7.1) which contains a possibility to specify script in the command line of GRASS GIS executable. The call might look 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()

上述解决方案的优势在于,您可以正确锁定Linux和Mac OS X上的Mapset,以及其他一些事情,而从正确设置GRASS环境开始,无需太多工作.您只需要知道可执行文件的路径或将可执行文件放在PATH上即可.

The advantage of the solution above is that you get proper locking of Mapsets on Linux and Mac OS X plus several other things starting with correctly set GRASS environment without much work. You just need to know path to the executable or have the executable "on PATH".

然后有一个与上一个版本相似的选项,该选项在较旧版本中适用.您可以设置GRASS_BATCH_JOB环境变量. GRASS进程将检查它并执行变量中指定的脚本.

Then there is an option similar to the previous one which works in older versions. You can set GRASS_BATCH_JOB environmental variable. GRASS process will check it and execute script specified in the 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 the variable just to be sure
# but using separate env would be more elegant
del os.environ['GRASS_BATCH_JOB']

与上面的选项(将脚本作为参数传递)不同,您不能将参数传递给被调用的脚本,也不能直接调用GRASS模块.

Unlike the option above (passing script as arguments) you cannot pass parameters to the called script nor call GRASS modules directly.

最后,您可以在系统中设置环境变量,从而使GRASS会话始终可用.提供正确的GISRC变量和文件后,GRASS模块将作为常规程序运行.此选项可能会引起许多问题,例如当使用两个版本的GRASS GIS时,因此不建议使用.但是,可以在两者之间做一些事情:将GRASS可执行文件添加到路径中,然后应该能够像在Linux上一样使用grass70调用GRASS(该路径可以在GRASS GIS图标的属性中找到) .

Finally, you can set the environmental variables in your system and thus have the GRASS session always available. GRASS modules would then run as normal programs when the right GISRC variable and file is provided. This option may potentially cause many problems, e.g. when using two version of GRASS GIS, so it is not recommended. However, it is possible to do something in between: add GRASS executable to the path, then you should be able to call GRASS just with grass70 as it is done on Linux (the path can be found in properties of GRASS GIS icon).

这篇关于无法从python脚本(草6.4,python 2.7,win7)运行草工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 20:59