3D Mapping in R

This tutorial has been kindly contributed by Robin Edwards (from UCL CASA).

RGL is R’s box of power-tool for 3D object rendering, with functionality for creating 3d mesh objects and curved surfaces, and for using materials and directional lighting.  For example the line:

`plot3d(rnorm(100),rnorm(100),rnorm(100))`

creates a 3d scatterplot of x-y-z normal distributions, producing: OpenStreetMap provides a nice way to import map tiles via the OSM API (among others). A helpful StackOverLoader (Spacedman) has provided this useful function for adding ‘z’ values to OSM map objects, enabling them to be plotted in 3d:

```map3d <- function(map, ...){
if(length(map\$tiles)!=1){stop("multiple tiles not implemented") }
nx = map\$tiles[]\$xres
ny = map\$tiles[]\$yres
xmin = map\$tiles[]\$bbox\$p1
xmax = map\$tiles[]\$bbox\$p2
ymin = map\$tiles[]\$bbox\$p1
ymax = map\$tiles[]\$bbox\$p2
xc = seq(xmin,xmax,len=ny)
yc = seq(ymin,ymax,len=nx)
colours = matrix(map\$tiles[]\$colorData,ny,nx)
m = matrix(0,ny,nx)
surface3d(xc,yc,m,col=colours, ...)
}```

A benefit of the approach is that it enables you to adjust the map to the ideal perspective for representing the data in the final rendered image. Here I’ve applied the function to data on London’s rental costs (for the year to December 2012), extruding thick lines for cost comparisons: The satellite version, simply replacing ‘osm’ with ‘bing’ in the code.. Code @ Github

Posted in R Spatial, Resources Previous Post

Prof. Mike Batty Talks Big Data and Smart Cities Next Post