pythonでgrassを呼び出すモジュール

2183 ワード

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公式サイトでダウンロードできる最新バージョンのwingrass(Stand-alone Installer)バージョンは7.0.4で、https://grass.osgeo.org/download/software/ms-windows/ただし、インストール後インストールディレクトリの下にフォルダmsysがないため、環境変数ではmsysに関する設定はできません.本文の解決方法は古いバージョンのgrassをダウンロードすればいいです.私はWinGRASS-7.0を使っています.svn版.
  • 古いバージョンのgrass(7.0.3以前のバージョンを指す)をインストールした後、起動時にエラーが発生することがあります.次の図:
  • 最初の図はgrassタイムズエラーunable to get current geographic extentを起動することです.Force quiting wxGUI.,次にgrassのcmdでコマンドg.regionを実行し、エラー:g.region.exeはエントリが見つかりません.プログラム入力ポイントsqlite 3を位置決めできません.prepare_v 2はダイナミックリンクライブラリsqlite 3にある.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.