Geoscience Reference
In-Depth Information
External
databases
External
GIS functionality
R/GIS
interfaces
GIS/application
GUI
External
files
FIGURE 14.6
Positioning of software components involving R and GIS.
the first FOSS4G OSGeo conference in Lausanne, Switzerland.
spgrass6
is loose-coupled, using
GDAL on both sides of the interface to exchange vector and raster data by writing to and reading
from a temporary directory. If a GRASS-GDAL plug-in is present, data can be read directly from
GRASS into
R
using GDAL, but using temporary files is robust and not very wasteful of time or
disk space.
From April 2009,
spgrass6
was revised to support a second mode of operation (Bivand 2011).
The earlier way of using
R
within a GRASS session was supplemented by the ability to initiate a
GRASS session from
R
, setting up the environment variables used by GRASS and, if necessary,
creating a throwaway location for use until the termination of the
R
session. This was comple-
mented by interfacing most GRASS commands directly in a cross-platform fashion, using the
-
interface
-
descriptio
n flag that GRASS commands use to return their flags, parameters and
other attributes in an XML file. Using the
XML
package in
R
to parse the interface descriptions
made it possible to write
parseGRASS
to parse the interface once for each GRASS command
used, caching the results;
doGRASS
to collate a string comprising the GRASS command and user-
supplied flags and parameters, all checked against the interface description; and
execGRASS
to
run the command through
system
in a portable way. The arguments to the two latter functions
have recently been simplified thanks to suggestions from Rainer Krug.
In the first example, we initiate a GRASS session from
R
, using the
SpatialGridDataFrame
object from
pycno
as a location template. Next, we export a
SpatialPolygonsDataFrame
object to GRASS with
writeVECT6
and apply a helper function
vect2neigh
written by
Markus Neteler, exploiting the topological vector representation in GRASS to return rook
neighbours (with non-zero length shared boundaries) with shared boundary length in metres and
total boundary length per feature, with features identified by their GRASS category numbers in
this case:
> library(spgrass6)
> set.ignore.stderrOption(TRUE)
> initGRASS("/home/rsb/topics/grass/g642/grass-6.4.2", home = tempdir(),
+ SG = NYp)
> writeVECT6(NY, vname = "NY", v.in.ogr_flags = "o")
> bl <- vect2neigh("NY", ID="cat", units="me")
> str(bl)