Tuesday, 3 November 2015

grass - spgrass6 and GIS_LOCK



I would like to thank's everyone first, i learn a lot here (gis.stackexchange) about GIS. I have one question about the use of GRASS within R session, i'm able to do some basic task, but i have one important issue is to use GRASS graphics through spgrass6::execGRASS.


I'm aware of the capabilities of R in graphics (lattice, ggplot and others), but i would like to be able to use GRASS graphics because of some option like nviz.


Here is a minimal exemple


require(spgrass6)
require(spatstat)
# to use SpatialGridDataFrame in ggplot
fortify.SpatialGridDataFrame <- function(data) as.data.frame(as.im(as.image.SpatialGridDataFrame(data)))

# initialisation and the use of spearfish60 data
initGRASS(gisBase = "/usr/lib/grass64", home = tempdir(), gisDbase = "/home/dickoa/GIS/grass/grassdata/", location = "spearfish60", mapset = "usr1", override = TRUE)


# verif
gmeta6()

execGRASS("g.list", parameters = list(type = "vect"))

parseGRASS("d.mon")

# and this command gave me errors
execGRASS("d.mon", parameters = list(start="x0"))


and the error message is :


ERREUR :Unable to get GIS_LOCK enviroment variable value

And generaly, i really like the R workflow using emacs (ESS) and the idea of keeping everything in script, it's one of the main raison for me to use grass within R, so i want also to ask for other R and GRASS users if this integration through spgrass6 (Bivand 2011) can allow me to do everything as in interactive session of GRASS.


here is my R session info (i use Kubuntu 10.10 as OS)


R version 2.12.2 (2011-02-25)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:

[1] LC_CTYPE=fr_FR.UTF-8 LC_NUMERIC=C
[3] LC_TIME=fr_FR.UTF-8 LC_COLLATE=fr_FR.UTF-8
[5] LC_MONETARY=C LC_MESSAGES=fr_FR.UTF-8
[7] LC_PAPER=fr_FR.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] grid stats graphics grDevices utils datasets
[7] methods base


other attached packages:
[1] spgrass6_0.6-26 XML_3.2-0 rgdal_0.6-33
[4] sp_0.9-79 spatstat_1.21-5 deldir_0.0-13
[7] mgcv_1.7-4 ggplot2_0.8.9 proto_0.3-9
[10] reshape_0.8.4 plyr_1.4 MASS_7.3-11

loaded via a namespace (and not attached):
[1] lattice_0.19-17 Matrix_0.999375-48 nlme_3.1-98

Answer




It is way easier to call R within a GRASS session. Then you don't have to set much since R will figure out by itself. See here for examples: http://grass.osgeo.org/wiki/R_statistics


If you want instead to call R-GRASS functionality as R batch job, this should work:


library(spgrass6)

# initialisation and the use of spearfish60 data
initGRASS(gisBase = "/usr/local/grass-6.4.1", home = tempdir(),
gisDbase = "/home/neteler/grassdata/",
location = "spearfish60", mapset = "user1", SG="elevation.dem",
override = TRUE)


system("g.region -d")
# verify
gmeta6()

spear <- readRAST6(c("geology", "elevation.dem"),
cat=c(TRUE, FALSE), ignore.stderr=TRUE,
plugin=NULL)

summary(spear$geology)


Run this script with


R CMD BATCH batch.R

The result is (shorted here):


cat batch.Rout

R version 2.10.0 (2009-10-26)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
...

> library(spgrass6)
Loading required package: sp
Loading required package: rgdal
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.7.2, released 2010/04/23
Path to GDAL shared files: /usr/local/share/gdal
Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009
Path to PROJ.4 shared files: (autodetected)
Loading required package: XML
GRASS GIS interface loaded with GRASS version: (GRASS not running)

>
> # initialisation and the use of spearfish60 data
> initGRASS(gisBase = "/usr/local/grass-6.4.1", home = tempdir(), gisDbase = "/home/neteler/grassdata/",
+ location = "spearfish60", mapset = "user1", SG="elevation.dem", override = TRUE)
gisdbase /home/neteler/grassdata/
location spearfish60
mapset user1
rows 477
columns 634
north 4928010

south 4913700
west 589980
east 609000
nsres 30
ewres 30
projection +proj=utm +zone=13 +a=6378206.4 +rf=294.9786982 +no_defs
+nadgrids=/usr/local/grass-6.4.1/etc/nad/conus +to_meter=1.0
Warning messages:
1: In dir.create(gisDbase) : '/home/neteler/grassdata' already exists
2: In dir.create(loc_path) :

'/home/neteler/grassdata//spearfish60' already exists
>
> system("g.region -d")
> # verify
> gmeta6()
gisdbase /home/neteler/grassdata/
location spearfish60
mapset user1
rows 477
columns 634

north 4928010
...
>
> spear <- readRAST6(c("geology", "elevation.dem"),
+ cat=c(TRUE, FALSE), ignore.stderr=TRUE,
+ plugin=NULL)
>
> summary(spear$geology)
metamorphic transition igneous sandstone limestone shale
11693 142 36534 74959 61355 46423

sandy shale claysand sand NA's
11266 14535 36561 8950
>
>
> proc.time()
user system elapsed
2.891 0.492 3.412

Hope this helps.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...