GRASS GIS、rgrass7 のインストール方法
昨日メモりました。
Ubuntu 14.04にGRASS GIS 7.0とrgrass7をインストールした時のメモ - Technically, technophobic.
これでインストールできたと思うでしょう? さっそく、library()してみます。
library("rgrass7") #> Loading required package: XML #> Loading required package: sp #> GRASS GIS interface loaded with GRASS version: (GRASS not running)
GRASS not running、だと…
GRASSを自動で走らせるための設定
なんかいろいろ環境変数を設定しておかないと、GRASSは自動で起動してくれないようです。
It is possible to access GRASS modules without explicitly starting a "GRASS session". GRASS libraries require certain environment variables to be set. In fact a "GRASS session" is just a set of processes (e.g. a shell and/or GUI) which have the necessary environment settings, specifically:
- GISBASE needs to be set to the top-level directory of the GRASS installation.
- GISRC needs to contain the absolute path to a file containing settings for GISDBASE, LOCATION_NAME and MAPSET.
- PATH needs to include $GISBASE/bin and $GISBASE/scripts.
If the GRASS libraries are shared libraries, the loader needs to be able to find them. This normally means that LD_LIBRARY_PATH (Linux, Solaris), DYLD_LIBRARY_PATH (MacOSX) or PATH (Windows) need to contain $GISBASE/lib, although there are other means to the same end (e.g. on Linux, putting $GISBASE/lib (with $GISBASE replaced by its actual value) into /etc/ld.so.conf then running ldconfig).
(Working with GRASS without starting it explicitly - GRASS-Wiki)
私の環境はLinuxなので、GISBASE・GISRC・PATH・LD_LIBRARY_PATHを設定します。
まず、そもそもR実行時の環境変数ってどう設定するのでしょう?
Rの環境変数を設定する方法
?Startupでヘルプが見れます。よくわかりませんが、ホームディレクトリ下に.Renvironというファイルを作ればいいようです。
Unless
--no-environwas given on the command line, R searches for site and user files to process for setting environment variables. The name of the site file is the one pointed to by the environment variableR_ENVIRON; if this is unset,‘R_HOME/etc/Renviron.site’is used (if it exists, which it does not in a‘factory-fresh’installation). The name of the user file can be specified by theR_ENVIRON_USERenvironment variable; if this is unset, the files searched for are‘.Renviron’in the current or in the user's home directory (in that order). See ‘Details’ for how the files are read.
.Renvironファイルの形式
同じく?Startupで表示されるヘルプのDetailsやExamplesに詳しく書いてありますが、
## Example ~/.Renviron on Unix R_LIBS=~/R/library PAGER=/usr/local/bin/less
のような記法で書けます。(ちなみに、イコールの前後には空白を入れても問題ないっぽいです)
${foo}という書き方でほかの環境変数を参照することもできます。
PATH=${PATH}:/path/to/somedir/
注意すべき点として、シェルスクリプトと違ってこの{}は必須です。$PATHと書いてしまうと"$PATH"という文字列として扱われます。
GRASS用に環境変数を設定する
とりあえずこんな感じで書けば動きました。
GISRC = ${HOME}/.grass7/rc GISBASE = /usr/lib/grass70 PATH = ${PATH}:${GISBASE}/bin/:${GISBASE}/scripts/ LD_LIBRARY_PATH = ${GISBASE}/lib
GISRC
これは、GRASSの作業ディレクトリ設定のファイルを指定します。こんな感じの中身のファイルです。
MAPSET: user1 GISDBASE: /path/to/grass_data LOCATION_NAME: nc_basic_spm_grass7 GUI: text
${HOME}/.grass7/rcには、一番最近使ったマップセットの情報が記録されます。とりあえずこのファイルを指定しておきます。(あとでサンプルデータを読み込むときにできるので今は存在しなくても大丈夫です)
GISBASE
GRASSのディレクトリです。apt-get install grassでインストールした場合は、/usr/lib/grass70にあるはずです。
PATH
GRASSのコマンドにパスを通しておかないといけないらしいです。GISBASEで指定したディレクトリの下にbinとscriptsというディレクトリ
があるはずなのでそれを指定します。
LD_LIBRARY_PATH
これは動的に読み込まれるライブラリへのパスです。OSによって環境変数名が違うみたいです。Linuxの場合はLD_LIBRARY_PATHです。
GISBASEで指定したディレクトリの下にlibというディレクトリがあるはずなのでそれを指定します。
GRASSのサンプルデータを読み込む
rgrass7を使う前に、サンプルデータをダウンロードして、一度コマンドラインからGRASSを起動してみます。
cd /path/to/WORKDIR wget http://grass.osgeo.org/sampledata/north_carolina/nc_basic_spm_grass7.tar.gz tar xf nc_basic_spm_grass7.tar.gz # サンプルデータで一度GRASSを起動すると、${HOME}/.grass7/rcがつくられる grass -text nc_basic_spm_grass7/user1
すると、こんな感じの画面が出ます。とりあえず今はexitしときましょう。
__________ ___ __________ _______________
/ ____/ __ \/ | / ___/ ___/ / ____/ _/ ___/
/ / __/ /_/ / /| | \__ \\_ \ / / __ / / \__ \
/ /_/ / _, _/ ___ |___/ /__/ / / /_/ // / ___/ /
\____/_/ |_/_/ |_/____/____/ \____/___//____/
Welcome to GRASS GIS 7.0.0
GRASS GIS homepage: http://grass.osgeo.org
This version running through: Bash Shell (/bin/bash)
Help is available with the command: g.manual -i
See the licence terms with: g.version -c
Start the GUI with: g.gui wxpython
When ready to quit enter: exit
GRASS 7.0.0 (nc_basic_spm_grass7):/path/to/WORKDIR >
これで、${HOME}/.grass7/rcができているはずです。
MAPSET: user1 GISDBASE: /path/to/WORKDIR LOCATION_NAME: nc_basic_spm_grass7 GUI: text
rgrass7のexampleを動かしてみる。
まず、新しい環境変数を読み込むためにいちどRのセッションを再起動します。
それからlibrary()でrgrass7を読み込むと、今度はちゃんと起動しているはずです。
library("rgrass7") #> Loading required package: sp #> Loading required package: XML #> GRASS GIS interface loaded with GRASS version: GRASS 7.0.0 (2015) #> and location: nc_basic_spm_grass7
で、exampleを動かしてみます。
example(rgrass7) #> rgrss7> if (nchar(Sys.getenv("GISRC")) > 0 && #> rgrss7+ read.dcf(Sys.getenv("GISRC"))[1,"LOCATION_NAME"] == "nc_basic_spm_grass7") { #> rgrss7+ require(rgdal) #> rgrss7+ elevation <- readRAST("elevation", ignore.stderr=TRUE, plugin=FALSE) #> rgrss7+ summary(elevation) #> rgrss7+ grd <- gmeta2grd(ignore.stderr=TRUE) #> rgrss7+ grd #> rgrss7+ set.seed(1) #> rgrss7+ pts <- spsample(elevation, 200, "random") #> rgrss7+ smple <- SpatialPointsDataFrame(pts, data=over(pts, elevation)) #> rgrss7+ summary(smple) #> rgrss7+ writeVECT(smple, "sp_dem", v.in.ogr_flags="overwrite", ignore.stderr=TRUE) #> rgrss7+ bugsDF <- readVECT("schools", ignore.stderr=TRUE, mapset="PERMANENT") #> rgrss7+ summary(bugsDF) #> rgrss7+ vInfo("streams", ignore.stderr=TRUE) #> rgrss7+ vColumns("streams", ignore.stderr=TRUE) #> rgrss7+ vDataCount("streams", ignore.stderr=TRUE) #> rgrss7+ streams <- readVECT("streams", type="line", #> rgrss7+ remove.duplicates=FALSE, ignore.stderr=TRUE, plugin=FALSE) #> rgrss7+ summary(streams) #> rgrss7+ } #> Loading required package: rgdal #> rgdal: version: 0.9-1, (SVN revision 518) #> Geospatial Data Abstraction Library extensions to R successfully loaded #> Loaded GDAL runtime: GDAL 1.11.1, released 2014/09/24 #> Path to GDAL shared files: /usr/share/gdal/1.11 #> Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480] #> Path to PROJ.4 shared files: (autodetected) #> Creating BIL support files... #> Exporting raster as floating values (bytes=4) #> #> WARNING: Datum <unknown> not recognised by GRASS and no parameters found #> Check if OGR layer <sp_dem> contains polygons... #> #> WARNING: Vector map <sp_dem> already exists and will be overwritten #> Importing 200 features (OGR layer <sp_dem>)... #> #> ----------------------------------------------------- #> Building topology for vector map <sp_dem@user1>... #> Registering primitives... #> #> 200 primitives registered #> 200 vertices registered #> Building areas... #> #> 0 areas built #> 0 isles built #> Attaching islands... #> Attaching centroids... #> #> Number of nodes: 0 #> Number of primitives: 200 #> Number of points: 200 #> Number of lines: 0 #> Number of boundaries: 0 #> Number of centroids: 0 #> Number of areas: 0 #> Number of isles: 0 #> Exporting 167 features... #> #> v.out.ogr complete. 167 features (Point type) written to <schools> #> (ESRI_Shapefile format). #> Exporting 8554 features... #> #> v.out.ogr complete. 8554 features (Line String type) written to <streams> #> (ESRI_Shapefile format). #> Object of class SpatialLinesDataFrame #> Coordinates: #> min max #> x 629851.9 645264.2 #> y 214638.0 228882.0 #> Is projected: TRUE #> proj4string : #> [+proj=lcc +lat_1=36.16666666666666 +lat_2=34.33333333333334 +lat_0=33.75 +lon_0=-79 #> +x_0=609601.22 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0] #> Data attributes: #> cat FNODE_ TNODE_ LPOLY_ RPOLY_ #> Min. : 40102 Min. : 42043 Min. : 42046 Min. :0.0000 Min. :0.0000 #> 1st Qu.: 75598 1st Qu.: 79168 1st Qu.: 79172 1st Qu.:0.0000 1st Qu.:0.0000 #> Median : 83078 Median : 86896 Median : 86888 Median :0.0000 Median :0.0000 #> Mean : 77835 Mean : 81394 Mean : 81395 Mean :0.6937 Mean :0.6937 #> 3rd Qu.: 95135 3rd Qu.: 99410 3rd Qu.: 99410 3rd Qu.:2.0000 3rd Qu.:2.0000 #> Max. :101347 Max. :105801 Max. :105800 Max. :2.0000 Max. :2.0000 #> #> LENGTH FULL_HYDRO FULL_HYD_1 FTYPE FCODE #> Min. : 0.001 Min. : 40102 Min. : 84728 ARTIFICIAL PATH: 755 Min. :33400 #> 1st Qu.: 60.370 1st Qu.: 75598 1st Qu.:163202 CANAL/DITCH : 343 1st Qu.:33600 #> Median : 171.338 Median : 83078 Median :211732 CONNECTOR :2052 Median :46000 #> Mean : 293.022 Mean : 77835 Mean :186607 STREAM/RIVER :5404 Mean :43347 #> 3rd Qu.: 395.517 3rd Qu.: 95135 3rd Qu.:228227 3rd Qu.:46000 #> Max. :3966.165 Max. :101347 Max. :234417 Max. :55800 #> #> NAME RCH_CODE I_vs_P USGSstrm #> Walnut Creek : 232 03020201001186: 80 intermittent:3506 usgs_stream:5048 #> Lynn Branch : 93 03020201001099: 57 perennial :5048 NA's :3506 #> Swift Creek : 89 03020201002397: 52 #> Rocky Branch : 80 03020201002579: 51 #> Pigeon House Branch: 71 03020201002552: 46 #> (Other) : 305 (Other) :1765 #> NA's :7684 NA's :6503
…正直、私この処理が何やってるのかわからないんですが(汗)、とりあえず動きましたね!
ここから先、どこへ進めばいいのかわからないのでGISガチ勢の方のマサカリをお待ちしています。