Exemplo: Kernel do Splancs

Exemplo: Kernel do Splancs

Example of a spatial point proccess
Data from the splancs package
The example tranfer the data to the DBMS as well a raster with a kernel estimate of the intensity function

1. Getting started: loading required packages and the data

require(aRT)
require(splancs)
data(bodmin)

getting to know the data

names(bodmin)
polymap(bodmin$poly)
pointmap(as.points(bodmin), add=TRUE) see the plot!


2. An analysis: kernel intensity estimate

Kernel estimate from the intensity function of the point proccess performed within R using splancs' function

ker <- kernel2d(as.points(bodmin), bodmin$poly, h0=2, nx=100, ny=100)

plot splancs results

image(ker, asp=1, col=rev(heat.colors(21)))
polygon(bodmin$poly, lwd=2)
pointmap(as.points(bodmin), add=TRUE, pch=19) see the plot



3. Creating a database and storing the results

connecting to the DBMS

con = openConn()
con

how to delete an existing database (if already exists)

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

creating a new database

bod = createDb(con, "bodmin")
bod

aRT uses "sp" representations of spatial objects. So we start converting the data to the "sp" format

a. Preparing and transfering the points

xy <- as.data.frame(bodmin[1:2])
coordinates(xy) <- c("x", "y")
##
l_points <- importSpData(bod, xy, "coords")

checking the status of the database

bod

Note: the importSpData() command above encapsulates several steps (which could also be done individually with aRT):

  1. converting to SpatialPointsDataFrame (with ID specification).
  2. creating a layers, addpoints() and creating a table

b. Preparing and transfering the polygon

pol <- SpatialPolygons(list(Polygons(list(Polygon(bodmin$poly)),"1")),1)
l_pol <- createLayer(bod, "contorno")
addPolygons(l_pol, pol)
createTable(l_pol, "t_pol")

c. Preparing and transfering the image (raster) general approach: converting the kernel to "sp" format

g <- cbind(expand.grid(x = ker$x, y = ker$y), as.vector(ker$z))
coordinates(g) <- c("x", "y")
gridded(g) <- TRUE
 
l_kernel <- createLayer(bod, "kernel")
addRaster(l_kernel, g)

alternativelly, splancs kernel objects are also directly accepted

l_splancs <- createLayer(bod, "kernelsplancs")
addRaster(l_splancs, ker)

checking the status of the data base

bod

open and inspect the database in terraView!!!



4. visualisation of results in R a. plot from the "sp" objects

image(g, col=rev(heat.colors(21)))
contour(ker, add = T)  
fullgrid(g)=TRUE
plot(pol, add=T, lwd=2)

Ploting the DB layers – plot from aRT

plot(l_kernel, col=rev(heat.colors(21)))
plot(l_points, add=T, pch=19)
plot(l_pol, add=T, lwd=2)



5. (Optional) Setting visualisations for terraView

thp <- createTheme(l_points, "points", view="vbod1")
th <- createTheme(l_pol, "borders", view="vbod1")
th <- createTheme(l_kernel, "raster", view="vbod1")
setVisual(th, visualRaster(color=rev(heat.colors(21))), mode="r")

other view using the alternative splancs layer

thp <- createTheme(l_points, "points", view="vbod2")
th <- createTheme(l_pol, "borders", view="vbod2")
 
th <- createTheme(l_splancs, "kernelsplancs", view="vbod2")
setVisual(th, visualRaster(color=rev(heat.colors(21))), mode="r")

checking the sattus of the data base

bod


open and visualise the database in terraView!!!