Interfacing Spatial Statistics sofware and GIS: a case study

Preview:

Citation preview

Interfacing Spatial Statistics sofware and GIS: a case study

TerraLib: Open source GIS library

Data management All of data (spatial + attributes) is

in database Functions

Spatial statistics, Image Processing, Map Algebra

Innovation Based on state-of-the-art

techniques Same timing as similar commercial

products

Web-based co-operative development

http://www.terralib.org

Operational Vision of TerraLib

TerraLib

API for Spatial

Operations

Oracle SpatialAccess

MySQLPostgre

SQL

DBMS

Geographic Application

Spatial Operations

Spatial Operations

TerraLib MapObjects + ArcSDE + cell spaces + spatio-temporal models

TerraLib applications

Cadastral Mapping Improving urban management of

large Brazilian cities Public Health

Spatial statistical tools for epidemiology and health services

Social Exclusion Indicators of social exclusion in

inner-city areas Land-use change modelling

Spatio-temporal models of deforestation in Amazonia

Emergency action planning Oil refineries and pipelines

(Petrobras)

TerraLib Structure

VisualizationControls

Functions

Spatio-TemporalData Structures

DBMS

File and DBMS Access

ExternalFiles

I/O Drivers

Java Interface COM Interface OGIS Services

kernel

C++ Interface

TerraView

TerraLib client Connects to DBMS

Oracle, PostGIS, MySQL and SQLServer Visualization with ‘views’ and ‘themes’

Images, Grids, vectors and tables Brushing between graphics and maps

Analysis Geocoding, ESDA, Clustering (more to come)

I/O SHP, MIF, and SPRING vectors TIFF and GEOTIFF rasters

Creating a MySQL spatial database in TerraView

File...Open Database menu

Importing GIS files

File...Import data Select the rio_inf_mort.shp (Rio infant mortality data) Use OBJET_ID_1 as the column Use Projection... Lat/Long and Datum SAD69

Databases, layers, views and themes

A database has many layers This is a storage concept

Visualization is based on views and themes

A view is a set of spatial objects from a layer

A theme is a way to display these objects

Changing the legend of a theme

Theme...Change legend

Change the legend to display the NEO_RATE Quantiles Five slices

Resulting TerraView interface

Producing a scatterplot

Theme...Graphic Parameters Selection a DISPERSION graphic

X – TOTBIRTH Y – NEO_RATE

Brushing

Operation...TileWindows Shows both graphics and map Brush between the two and the table

R – TerraLib integration

Strong coupling mechanism

R – TerraLib integration

Enables R to access a TerraLib database

Results in R are incorporated in a GIS database directly

Data is analysed in R using packages such as gstat or geoR

Results are visualized in TerraView

Integration is performed by the aRT API

aRT: API R-TerraLib

aRT is an R package that provides the integration between the software R and the GIS classes TerraLib.

Developed by UFPR, Brazil

http://www.est.ufpr.br/aRT/

aRT: API R-TerraLib

Classes to manipulate data and functions in TerraLib

aRT Enables connection to the DBMS for database

administration aRTdb

Creation and access to a database aRTlayer

Enables manipulation of layers in aRT aRTtheme

Enables visualização of themes in TerraView

Integration R - TerraLib

R graphics

TerraView map (Neto et al., 2005)

R-TerraLib integration: example 1

Load the geoR and arT packages into R manually

geoR is a library for advanced geostatistics (also developed by Paulo Ribeiro, UFPR)

Get the geoR and arT packages from the course homepage

Expand the zip files and copy them to the directory C:\Program Files\R\rw2011\library

R-TerraLib integration: example 1

#### loading packages##require(geoR)require(aRT)

#### Exemple 1: data for parana state##data(parana)points(parana, bor=borders)

A new type: geodata> class (parana)[1] "geodata"> parana$coords east north [1,] 402.9529 164.52841 [2,] 501.7049 428.77100 [3,] 556.3262 445.27065 [4,] 573.4043 447.04177 [5,] 702.4228 272.29590 [6,] 668.5442 261.67070 [7,] 435.8477 286.54044 [8,] 434.0125 317.90506 [9,] 432.4622 288.37001

A new type: geodata

$data [1] 306.09 200.88 167.07 162.77 163.57 178.61 301.54 [8] 282.07 319.12 244.67 233.31 224.46 206.12 248.99 [15] 237.87 222.87 263.10 236.91 247.01 240.58 304.28 [22] 351.73 277.92 323.08 253.32 315.33 379.94 197.09 [29] 199.91 167.00 182.88 197.10 257.50 205.16 224.07 [36] 212.50 242.08 247.79 187.27 222.54 313.60 269.92 [43] 321.69 208.89 238.62 248.76 193.48 240.48 265.56 [50] 302.13 335.41 330.87 329.49 262.81 365.88 359.08 [57] 344.59 366.10 201.84 218.27 200.38 229.40 235.07 [64] 236.25 228.82 258.12 232.17 248.17 240.66 184.59 [71] 165.46 320.31 232.80 266.27 301.10 244.78 248.57

A new type: geodata

$borders east north [1,] 670.2049 111.7610 [2,] 663.7187 107.0510 [3,] 656.0667 105.2420 [4,] 649.9714 100.7915 [5,] 642.6346 97.9930 [6,] 635.2717 101.1545 [7,] 628.7421 105.5405 [8,] 622.7559 110.7495 [9,] 617.2708 116.3970 [10,] 610.4570 120.5795 [11,] 604.4217 119.6670 [12,] 600.2620 121.8980 [13,] 592.6210 119.9675

A new type: geodata

$loci.paper [,1] [,2][1,] 300.336 484.453[2,] 647.755 317.463[3,] 361.764 438.781[4,] 410.100 260.373

R-TerraLib integration: example 1

## ## connection to MySQL##> con <- openConn(user=“gilberto")> conObject of class aRTconn

DBMS: "MySQL"User: "gilberto"Password: ""Port: 3306Host: "localhost"

Cleaning the database

If the database exists, clean it

> showDbs(con)[1] "ifgi" "intro" "mysql" "parana" "test"

> if(any(showDbs(con)=="parana")) deleteDb(con, "parana", force=T)

Deleting database 'parana' ... yes

Creating a new database

## creating a new database> pr = createDb(con, "parana")Connecting with database 'parana' ... noCreating database 'parana' ... yesCreating conceptual model of database 'parana' ...

yesLoading layer set of database 'parana' ... yesLoading view set of database 'parana' ... yes

Creating a layer in the database

#### Creating a layer to hold the data## l_data <- createLayer(pr, "data")Building projection to layer 'data' ... yesCreating layer 'data' ... yes

Loading points into the database

## loading points> artcoords <- .gr2aRTpoints(parana)# adding points to a layer> addPoints(l_data, artcoords)Converting points to TerraLib format ... yesAdding 143 points to layer 'data' ... yesReloading tables of layer 'data' ... yes

# adding a table to the layer> t_data = createTable(l_data, "t_data",gen=T)Creating static table 't_data' on layer 'data' ... yesCreating link ids ... yes

Plotting the data

> plot(l_data)

Inserting attributes on the GIS table

> artdata <- data.frame(.gr2aRTattributes(parana))> > artdata[1:5,] id data1 1 306.092 2 200.883 3 167.074 4 162.775 5 163.57> > names(artdata)[1] "id" "data"> createColumn(t_data, "data")Checking for column 'data' in table 't_data' ... noCreating column 'data' in table 't_data' ... yes> updateColumns(t_data, artdata)Converting 2 attributes to TerraLib format ... yesConverting 143 rows to TerraLib format ... yesUpdating columns of table 't_dados' ... yes

Creating views and themes for TerraView

## criando vistas e temas automaticas para o TV (opcional)> th_data <- createTheme(l_data, “stations", view = "view")Checking for theme ‘stations' in layer 'parana' ... noCreating theme ‘stations' on layer 'dados' ... yesChecking for view 'view' in database 'parana' ... noCreating view 'view' ... yesInserting view 'view' in database 'parana' ... yesChecking tables of theme ‘stations' ... yesSaving theme ‘stations' ... yesBuilding collection of theme ‘stations' ... yes> setVisual(tema_dados, visualPoints(pch=22, color="black",

size=5))

Creating a layer to store borders

> artpols <- .gr2aRTpolygons(parana)> > l_pol<-createLayer(pr, “borders")Building projection to layer ‘borders' ... yesCreating layer ‘borders' ... yes> addPolygons(l_pol, artpols)Converting polygons to TerraLib format ... yesAdding 1 polygons to layer ‘borders' ... yesReloading tables of layer ‘borders' ... yes> createTable(l_pol, "t_pol")Creating static table 't_pol' on layer ‘borders' ... yesCreating link ids ... yesObject of class aRTtableTable: "t_pol"Type: staticLayer: “borders"Rows: 1Attributes: id: string[16] (key)> plot(l_pol)

Creating a view and theme for the polygons

> tema_pol<-createTheme(l_pol, “borders", view="view")Checking for theme ‘borders' in layer 'parana' ... noCreating theme ‘borders' on layer ‘borders' ... yesChecking for view 'view' in database 'parana' ... yesChecking tables of theme ‘borders' ... yesSaving theme ‘borders' ... yesBuilding collection of theme ‘borders' ... yes> setVisual(tema_pol, visualPolygons())

Creating a grid for the interpolated surface

> gx <- seq(50,800, by=10)> gy <- seq(-100,650, by=10)> loc0 <- expand.grid(gx,gy)> points(parana, bor=borders)> points(loc0, pch=".", col=2)> loc1 <- polygrid(loc0, bor=parana$bor)> points(loc1, pch="+", col=4)

Kriging using geoS: model fitting

> # Kriging using geoS > # Step 1 – fitting the variogram> ml <- likfit(parana, trend="1st", ini=c(1000, 100))---------------------------------------------------likfit: likelihood maximisation using the function optim.likfit: WARNING: This step can be time demanding!---------------------------------------------------likfit: end of numerical maximisation.

Kriging using geoS: interpolation

> kc <- krige.control(trend.d="1st", trend.l="1st", obj=ml)> kc <- krige.conv(parana, loc=loc0, krige= KC,

bor=parana$borders)krige.conv: results will be returned only for prediction

locations inside the borderskrige.conv: model with mean given by a 1st order polynomial

on the coordinateskrige.conv: Kriging performed using global neighbourhood > save.image()> > image(kc, col=terrain.colors(15))

Exporting the grid to TerraLib

> # preparing object for aRT> georpred <- .prepare.graph.kriging(locations=loc0,

borders=parana$borders, values=kc$pred)> artpred <- .gr2aRTraster(georpred)> # creating a layer in TerraLib> l_pred <- new("aRTlayer", pr, layer="pred", create=T)Building projection to layer 'pred' ... yesCreating layer 'pred' ... yes

> addRaster(l_pred, artpred)Adding raster data to layer 'pred' ... yesReloading tables of layer 'pred' ... yes

Creating a theme and a view for the grid

> th<-createTheme(l_pred, "raster", view="view")Checking for theme 'raster' in layer 'parana' ... noCreating theme 'raster' on layer 'pred' ... yesChecking for view 'view' in database 'parana' ... yesChecking tables of theme 'raster' ... yesSaving theme 'raster' ... yes> > setVisual(th, visualRaster(), mode="r")

Plotting the data in R

> # plotting the data in R> plot(l_pred, col=terrain.colors(15))> plot(l_dados, add=T)> plot(l_pol, add=T)

Now, let’s see the data in TerraView

File...OpenDatabase... Parana database is there!

Create Views and Themes (if needed)

Create View parana

Create themes Borders Data Pred

Play with the data!

R data in TerraView