有一些方法可以改进你的代码,例如,添加环境变量而不是设置它们:
os.environ['PATH'] += os.pathsep + os.path.join(gisbase, 'bin')
然后GISRC
变量应该指向一个临时文件,而不是用户设置目录中的文件(如果你想以普通用户身份运行 GRASS 会话)。但无论如何,我建议从新的脚本或方法重新开始。
首先,如果可能,请使用 GRASS GIS 7。7.0.0 版是几个月前发布的,它可用于 MS Windows,既可以作为独立的,也可以作为 OSGeo4W 的一部分。例如,您可能会收到更好的错误消息。
其次,查看相关的wiki页面。它包含一个详细的指南,需要使用大量的代码示例来完成。以下是其中一个基本内容的摘录(仅适用于 MS Windows):
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 会话。
此外,GRASS GIS 的开发版本中还有一个新选项。如果您正在进行一些非生产开发(尚未)或者您正在编写一些实验性的东西,您可以尝试使用每日构建的开发版本(Subversion 主干,未来 7.1),其中包含在 GRASS 的命令行中指定脚本的可能性GIS 可执行文件。调用可能如下所示:
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 上正确锁定 Mapsets 以及从正确设置的 GRASS 环境开始的其他几件事,而无需太多工作。您只需要知道可执行文件的路径或将可执行文件“打开PATH
”即可。
然后有一个与旧版本类似的选项。您可以设置GRASS_BATCH_JOB
环境变量。GRASS 进程将检查它并执行变量中指定的脚本。
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 模块。
最后,您可以在系统中设置环境变量,从而使 GRASS 会话始终可用。GISRC
当提供正确的变量和文件时,GRASS 模块将作为正常程序运行。此选项可能会导致许多问题,例如在使用两个版本的 GRASS GIS 时,因此不推荐使用。但是,可以在两者之间做一些事情:将 GRASS 可执行文件添加到路径中,然后您应该能够grass70
像在 Linux 上那样调用 GRASS(路径可以在 GRASS GIS 图标的属性中找到)。