GIS for 3D

GIS data structures are not well suited for generalization, and visualizations and models in 3D require pretty forceful and ad hoc approaches.

Here I describe a simple example, showing several ways of visualizing a simple polygon data set. I use the programming environment R for the data manipulation and the creation of this document via several extensions (packages) to base R.

Polygon “layer”

The R package maptools contains an in-built data set called wrld_simpl, which is a basic (and out of date) set of polygons describing the land masses of the world by country. This code loads the data set and plots it with a basic grey-scale scheme for individual countries.

library(maptools)
data(wrld_simpl)
print(wrld_simpl)
## class       : SpatialPolygonsDataFrame 
## features    : 246 
## extent      : -180, 180, -90, 83.57027  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0 
## variables   : 11
## names       : FIPS, ISO2, ISO3,  UN,           NAME,    AREA,    POP2005, REGION, SUBREGION,      LON,     LAT 
## min values  :     ,   AD,  ABW,   4, Aaland Islands,       0,          0,      0,         0, -102.535, -10.444 
## max values  :   ZI,   ZW,  ZWE, 894,       Zimbabwe, 1638094, 1312978855,    150,       155,  179.219,  78.830
plot(wrld_simpl, col = grey(sample(seq(0, 1, length = nrow(wrld_simpl)))))

We also include a print statement to get a description of the data set, this is a SpatialPolgyonsDataFrame which is basically a table of attributes with one row for each country, linked to a recursive data structure holding sets of arrays of coordinates for each individual piece of these complex polygons.

These structures are quite complicated, involving nested lists of matrices with X-Y coordinates. I can use class coercion from polygons, to lines, then to points as the most straightforward way of obtaining every XY coordinate by dropping the recursive hierarchy structure to get at every single vertex in one matrix.

allcoords <- coordinates(as(as(wrld_simpl, "SpatialLines"), "SpatialPoints"))
dim(allcoords)
## [1] 26264     2
head(allcoords)  ## print top few rows
##      coords.x1 coords.x2
## [1,] -61.68667  17.02444
## [2,] -61.88722  17.10527
## [3,] -61.79445  17.16333
## [4,] -61.68667  17.02444
## [5,] -61.72917  17.60861
## [6,] -61.85306  17.58305

(There are other methods to obtain all coordinates while retaining information about the country objects and their component “pieces”, but I’m ignoring that for now.)

We need to put these “X/Y” coordinates in 3D so I simply add another column filled with zeroes.

allcoords <- cbind(allcoords, 0)
head(allcoords)
##      coords.x1 coords.x2  
## [1,] -61.68667  17.02444 0
## [2,] -61.88722  17.10527 0
## [3,] -61.79445  17.16333 0
## [4,] -61.68667  17.02444 0
## [5,] -61.72917  17.60861 0
## [6,] -61.85306  17.58305 0

(Note for non-R users: in R expressions that don’t include assignment to an object with “<-” are generally just a side-effect, here the side effect of the head(allcoords) here is to print the top few rows of allcoords, just for illustration, there’s no other consequence of this code).

OpenGL in R

In R we have access to 3D visualizations in OpenGL via the rgl package, but the model for data representation is very different so I first plot the vertices of the wrld_simpl layer as points only.

library(rgl)
library(rglwidget)  ## allows embedding OpenGL vis in RMarkdown HTML
plot3d(allcoords, xlab = "", ylab = "") ## smart enough to treat 3-columns as X,Y,Z
subid <- currentSubscene3d()
rglwidget(elementId="plot3d_wrld")