在安装好grass之后,只需设置如下的环境变量,便可在python中调用grass中的模块功能:
GISBASE= D:\Program Files (x86)\GRASS GIS 7.0.svn
PATH=%GISBASE%\etc;%GISBASE%\etc\python;%GISBASE%\lib;%GISBASE%\bin;%GISBASE%\extrabin;C:\Python27\ArcGIS10.1;%GISBASE%\scripts;%GISBASE%\msys\bin;%GISBASE%\Python27\lib
LD_LIBRARY_PATH= %GISBASE%\lib
PYTHONPATH= %GISBASE%\etc\python
PYTHONLIB= C:\Python27\ArcGIS10.1
GRASS_SH= %GISBASE%\msys\bin\sh.exe
示例Python程序如下:
import sys
import os
import grass.script as grass
kk = r"E:\Exercise\test\grasstest\dem2.tif"
r = grass.run_command('r.in.gdal',
flags='o',
input=kk,
output="mm",
overwrite=True)
print r
r = grass.run_command("g.region", rast="mm")
print r
但是我们需要注意的几个问题:
第一幅图是启动grass时报错 unable to get current geographic extent. Force quiting wxGUI.,然后根据提示在grass的cmd中运行命令g.region,又报错:g.region.exe无法找到入口,无法定位程序输入点sqlite3_prepare_v2于动态链接库sqlite3.dll上。此时的解决办法是
The issue is caused by different sqlite3.dll files accessible “on path” to all application. In case of GRASS GIS, some incompatible sqlite3 DLL is picked up by the system rather than using the correct one shipped with GRASS GIS.
Solution: search for all sqlite3.dll files on your computer and remove rename them one by one while trying at the same time to start GRASS GIS successfully. Note that removing some of the other sqlite3.dll files may break the applications which use them (say, while this is possible it has never actually been reported by any user). So, it is a good idea to just rename the DLL file in case you discover that another application needs that particular sqlite3 DLL and you have to reverse the process.