A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs ›...

24
A Process and Environment for Embedding The R Software into TerraLib Pedro Ribeiro de Andrade Neto Paulo Justiniano Ribeiro Junior GeoInfo 2005

Transcript of A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs ›...

Page 1: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

A Process and Environment for Embedding The R Software into

TerraLib

Pedro Ribeiro de Andrade NetoPaulo Justiniano Ribeiro Junior

GeoInfo 2005

Page 2: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Outline

● Introduction● Related Works● aRT● Process for Integration● Conclusions

Page 3: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

TerraLib● Library for building customized GIS● For programmers● Needs specialized statistics analysis

DBMS

TerraLib

Jfddfjh gfsdfgdfssf fsdf fsdfsd sdfsdfJfddfjh gfsdfgdfssf fsdf fsdfsd sdfsdf

Jfddfjh gfsdfgdfssf fsdf fsdfsd sdfsdf

Page 4: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

R Project for Statistical Computing

● Free software

● Point pattern analysis: spatstat, splancs● Geostatistics: gstat, geoR, geoRglm● Areal data analysis: DCluster, spdep

● 32 packages currently available in the Spatial Task View on the CRAN repository

● Shared library (.dll, .so)

Page 5: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Integration

● Coupling R inside TerraLib● Can benefit from specialized spatial statistical methods:

● Point pattern analysis● Geostatistics● Areal data analysis

Page 6: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Objective

Propose an efficient way to incorporate R analysis scripts into TerraLib

● Use R as an evolving library for building personalized TerraLib based GIS

● Meet the needs of three agents:● Statisticians● TerraLib users (programmers)● GIS users

Page 7: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Related Works for R Users

R packages to access GIS data/functionalities:● GRASS (Bivand and Netler 2000)● RArcInfo (Gómes-Rubio 2005)● rgdal (Keith and Bivand 2004)● aRT (Andrade Neto et. al. 2005)

Problem:● They require R knowledge, and then the target is statisticians/R users

Page 8: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Example of an R Scriptconn = openConn(user="pedro")db = openDb(conn, "dbname")thpoints = openTheme(db, "themepoints")thborder = openTheme(db, "themeborder")

points = getPoints(thpoints)border = getPolygons(thborder)data = getData(thpoints)

raster = krige(points, border, data)

l = createLayer(db, "lraster")addRaster(l, raster)thraster = createTheme(l, "themeraster")

Page 9: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Related Works for GIS UsersGraphic interfacesto R:● Ono et. al. 2003● Tait et. al. 2004

Problems:● They describe the functionality, but not the proccess to aggregate new functionalities● They exchange information with R using files

Page 10: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Related Works for GIS Programmers

No works...

Page 11: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

The aRT package: R-TerraLib API

● R package that can connect toTerraLib databases

● Scripts for statistical analysisof data

● Rule for aRT scripts: all datausing database

Page 12: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

aRT scriptconn = openConn(user="pedro")db = openDb(conn, "dbname")thpoints = openTheme(db, "themepoints")thborder = openTheme(db, "themeborder")

points = getPoints(thpoints)border = getPolygons(thborder)data = getData(thpoints)

raster = krige(points, border, data)

l = createLayer(db, "lraster")addRaster(l, raster)thraster = createTheme(l, "themeraster")

Page 13: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

aRT results

Page 14: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

How to integrate

● Option 1Execute R from the GIS and run the entire scriptProblem: two connections to the database, and after

that the GIS must reconnect to read metadata

● Option 2Use aRT internal functions to read from the

database and build R objects to be usedProblem: TerraLib programmers must know R and

aRT internally.

Page 15: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

TeDatabase*

How to integrate: Option 3

● Share a database connection (TeDatabase)● Require little changes in the original script

Page 16: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Example of an aRT Scriptconn = openConn(user="pedro")db = openDb(conn, "dbname")thpoints = openTheme(db, "themepoints")thborder = openTheme(db, "themeborder")

points = getPoints(thpoints)border = getPolygons(thborder)data = getData(thpoints)

raster = krige(points, border, data)

l = createLayer(db, "lraster")addRaster(l, raster)thraster = createTheme(l, "themeraster")

Page 17: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Wrapping an R codetlKriege = function(db, tpointsname, tcontourname, lrastername, trastername){ thpoints = openTheme(db, tpointsname) thborder = openTheme(db, tbordername)

points = getPoints(thpoints) border = getPolygons(thborder) data = getData(thpoints)

raster = krige(points, border, data)

l = createLayer(db, lrastername) addRaster(l,raster) thraster = createTheme(l, trastername) return(invisible())}

Page 18: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

TeRwrapper

● Uses MyR library● Operations:

● Initialize the environment● Declare a TeDatabase● Execute an R command

Page 19: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

TerraLib code

TeDatabase* db; char* str;TeRwrapper* Rwrapper; string error;...Rwrapper = new TeRwrapper();Rwrapper->declareDatabase(db, "db");asprintf(&str, "tlKrige(db,%s,%s,%s,%s)", themepoints.c_str(), themeborder.c_str(), layerraster.c_str(), themeraster.c_str());error = Rwrapper->execute(str);if(error != "") ...

OnPaint();

Page 20: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Integration Diagram

Page 21: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Results

Note that aRT alone could do that, but the focus now is how to use this functionality without any R knowledge.

Page 22: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Conclusions

● Powerful analysis environment for statistics professionals● Follows TerraLib project purposes● Enables each agent to focus on his/her environment and skills

Page 23: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

Future Work

● TerraCitrus, developing friendly interfaces● Define a language for documentation of the functions, and try to generate part of the graphic interface for the programmers● Ideas??

Page 24: A Process and Environment for Embedding The R Software ... › ~pedro › aRT › docs › ProccessAndEnvironment.pdf · R Project for Statistical Computing Free software Point pattern

A Process and Environment for Embedding The R Software into

TerraLib

Pedro Ribeiro de Andrade NetoPaulo Justiniano Ribeiro Junior

GeoInfo 2005