在python中调用grass的模块

在安装好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

但是我们需要注意的几个问题:

  1. 目前grass官网上能下载的最新版本的wingrass(Stand-alone Installer) 版本为7.0.4,https://grass.osgeo.org/download/software/ms-windows/,但是安装之后安装目录下没有文件夹msys,因此环境变量中凡是与msys有关的无法进行设置。本文的解决办法是下载一个旧版本的grass即可,我用的是WinGRASS-7.0.svn版。
  2. 有时候在安装旧版本的grass(指的是7.0.3之前的版本)后,启动时会报错。如下图:

第一幅图是启动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.

你可能感兴趣的:(python)