Geostat course, Albacete 2016
Cesar Martinez Izquierdo - www.scolab.es
The following software is required for the course:
Press Down key for details
Download and install from CRAN
Test that RExternal is correctly installed by launching RShell using the gvSIG Scripting launcher
It should open a new R console
Check the next slide if you find any problem
gvSIG/extensiones/org.gvsig.rexternal.app.mainplugin/rpath.properties
within your gvSIG installation folder
# File: gvSIG/extensiones/org.gvsig.rexternal.app.mainplugin/rpath.properties
# keep this empty to search R binary in current PATH
R_BIN_PATH=
## You can manually set the path to the R binary. Examples:
#
# Typical path for Linux:
# R_BIN_PATH=/usr/bin/R
# Typical path for Windows 64 bits:
# R_BIN_PATH=c:\\Program Files\\R\\R-3.2.3\\bin\\x64\\R.exe
# Typical path for Windows 32 bits:
# R_BIN_PATH=c:\\Program Files\\R\\R-3.2.3\\bin\\x32\\R.exe
## Note that in Windows, the path to R will likely change when
## you update R to a newer version!!!!!
## You can also use an internally-installed R instance
# R_BIN_PATH=R/bin/R
Install the following R packages: rgdal, sp and raster.
Type in R console:
install.packages("sp")
install.packages("raster")
install.packages("rgdal")
See Exercise 3 for detailed documentation and usage examples
Download and uncompress the sample data and sample scripts for the exercises
Goal: getting familiar with gvSIG basics
http://www.ign.es/wmts/pnoa-ma?request=GetCapabilities&service=WMTS
Goal: Demonstrate the geoprocessing toolbox
We have some information about species distribution (from the SPANISH INVENTORY OF TERRESTRIAL SPECIES) and want to learn about the habitat preferences of a particular species (Otis tarda).
We also have information about land cover and elevation
Image: Francesco Veronesi (CC BY-SA 2.0)
Image: Governor of Volgograd Oblast (CC BY-SA 3.0)
Goal: Demonstrate the gvSIG-R interface using RExternal plugin
We are going to learn how to integrate R scripts in the gvSIG Geoprocessing Toolbox and how to easily distribute them as new gvSIG plugins
geostatsplugin
folder. We'll have 4 components:autorun.py
script to register the script in the Toolbox__init__.py
fileHave a closer look to each file. In test.r
script we have: 1) a function to load some
libraries:
load_libraries <- function() {
library(rgdal)
library(raster)
return(1)
}
Have a closer look to each file. In test.r
script we have: 2) a doalmostnothing function
which loads a vector layer and copies one raster from inrasterpathname to outrasterpathname:
doalmostnothing <- function(shpdsn, shpname, inrasterpathname, outrasterpathname){
shp<-readOGR(dsn=shpdsn,layer=shpname)
message("shp loaded")
rasterimage<-raster(inrasterpathname)
message("raster loaded")
writeRaster(rasterimage, filename=outrasterpathname, format="GTiff", overwrite=TRUE)
return (1)
}
Have a closer look to each file. In gluecode.py we basically have: 1) some imports, a class declaration extending from RProcess class and an __init__ method defining the path to the rscript (and optionally the working directory):
import rlib
reload(rlib)
from rlib.process import *
import os
class TestProcess1(RProcess):
def __init__(self):
baseDir = os.path.dirname(os.path.abspath(__file__))
rscript = os.path.join(baseDir, "rscript.r")
RProcess.__init__(self, rscript, baseDir)
Have a closer look to each file. In gluecode.py we basically have: 2) a defineCharacteristics method with the declaration of the inputs and outputs:
def defineCharacteristics(self):
# Process name
self.setName("Test R process1")
# Process group
self.setGroup("Geostat course")
params = self.getParameters()
# Define an input vector parameter, named IN_VECTOR, of type polygon and make it mandatory
params.addInputVectorLayer("IN_VECTOR","Input vector layer", SHAPE_TYPE_POLYGON, True)
# Define an input raster parameter named IN_RASTER and make it mandatory
params.addInputRasterLayer("IN_RASTER","Input raster layer", True)
# Define an output raster layer, name "OUT_RASTER"
self.addOutputRasterLayer("OUT_RASTER", "Output raster")
Have a closer look to each file. In gluecode.py we basically have: 3) a callRProcess method defining the methods of the R script to be called:
def callRProcess(self, *args):
"""
Calls the main method of the defined R script
"""
self.R.call("load_libraries")
self.R.call("doalmostnothing", *args)
Have a closer look to each file. In autorun.py
script we instance the TestProcess1 that
we previously declared in gluecode.py and we register it on the toolbox:
import gvsig
from gluecode import *
import gluecode
process = gluecode.TestProcess1()
process.selfregister("Scripting")
This is enough to have the R plugin available on the gvSIG toolbox and process modeller
But we can do the same even with less code. Have a look to the geostatsplugincompact sample plugin. It only contains:
If you look to autorun.py in geostatsplugincompact, you'll notice:
Now you are ready to integrate your own scripts. Follow these steps
Once you test and validate your plugin, you are ready to distribute it. It's very easy!!
This creates a .gvspkg file, which can be easily installed on other computer using gvSIG's Add-on Manager
You can also contact us to get your plugin included on the Standard installation list. In this way, your users will not need to download the package themselves