Pages Menu
TwitterRss
Categories Menu

Posted by on May 8, 2013 in R Spatial, Resources | 2 comments

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:

rgl_basic_example

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[[1]]$xres
  ny = map$tiles[[1]]$yres
  xmin = map$tiles[[1]]$bbox$p1[1]
  xmax = map$tiles[[1]]$bbox$p2[1]
  ymin = map$tiles[[1]]$bbox$p1[2]
  ymax = map$tiles[[1]]$bbox$p2[2]
  xc = seq(xmin,xmax,len=ny)
  yc = seq(ymin,ymax,len=nx)
  colours = matrix(map$tiles[[1]]$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:

rgl_3d_map

The satellite version, simply replacing ‘osm’ with ‘bing’ in the code..
rgl_3d_satellite

Code @ Github

Share on Facebook
Bookmark this on Google Bookmarks
Share on reddit
Bookmark this on Digg
Share on StumbleUpon
Share on LinkedIn

2 Comments

  1. Thanks for the tutorial! Do you know how one might go about plotting an ESRI shapefile in RGL? I’ve been looking for a way to plot an ESRI shapefile basemap on some 3D data in RGL, but have thus far been unsuccessful. Alternatively, do you know how to convert an ESRI shapefile to an OSM map object (one that doesn’t have to be uploaded) and can then be plotted in a manner similar to your Github code? Thanks!

  2. I wonder what a semi-transparent interpolated layer would look like over those points? Something to try sometime I reckon. A similar map showing usage of tube stations rather than rental might be interesting too.

Post a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>