RGL surfaces

3dchiapas.png

Some cool tricks can be achieved in R using the rgl package. I will add code later to produce 3d trees and also to look at the results of multivariate analysis in three dimensions.

Here is a very simple function for making a 3d surface from a digital elevation model held as a SpatialGridDataFrame. The first lines will download an object from an Ecosur ftp site, so the code should produce the results shown above, a 3d map of the state of Chiapas.

library(lattice)
library(sp)
library(rgl)

urlstring<-“ftp://anonymous@200.23.34.16/simulacion/Datos”

a<-paste(urlstring,”chisgrid.rob”,sep=”/”)
load(url(a))

view.3d<-function(grid=chisgrid,dem=1,drape=2,exag=3,pal=sp.theme()$regions$col){
fullgrid(grid)<-TRUE
y<-grid[[dem]]*exag
y[is.na(y)]<-0
dim(y)<-grid@grid@cells.dim

x <- grid@grid@cellsize[1]*(1:grid@grid@cells.dim[1])
z <-grid@grid@cellsize[2]*(1:grid@grid@cells.dim[2])

y2<-grid[[drape]]
y2[!is.na(y2)]<-as.numeric(cut( y2[!is.na(y2)],length(pal)))
y2[is.na(y2)]<-0

ylim <- range(y2)
ylen <- ylim[2] – ylim[1] + 1

pal<-c(“black”,pal)
col <- pal[ y2-ylim[1]+1 ] # assign colors to heights for each point

rgl.clear()
rgl.surface(x, z, y, color=col)
}

view.3d(dem=1,drape=1,pal=terrain.colors(100))

Important note. On testing this I have just found that there seems to be an encoding problem when using WordPress. The site should be using UTM-8 but quotation marks don’t appear to be recognised by R. If this throws an error then copy the code to a text editor and replace the offending quotation marks. I am trying to solve the issue Try this source code view3d.doc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s