GIS for 3D in R

news
code
Author

Michael D. Sumner

Published

December 28, 2015

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)
variables   : 11
# A tibble: 246 × 11
   FIPS  ISO2  ISO3     UN NAME      AREA POP2005 REGION SUBREGION     LON   LAT
   <fct> <fct> <fct> <int> <fct>    <int>   <dbl>  <int>     <int>   <dbl> <dbl>
 1 AC    AG    ATG      28 Antigu…     44  8.30e4     19        29  -61.8   17.1
 2 AG    DZ    DZA      12 Algeria 238174  3.29e7      2        15    2.63  28.2
 3 AJ    AZ    AZE      31 Azerba…   8260  8.35e6    142       145   47.4   40.4
 4 AL    AL    ALB       8 Albania   2740  3.15e6    150        39   20.1   41.1
 5 AM    AM    ARM      51 Armenia   2820  3.02e6    142       145   44.6   40.5
 6 AO    AO    AGO      24 Angola  124670  1.61e7      2        17   17.5  -12.3
 7 AQ    AS    ASM      16 Americ…     20  6.41e4      9        61 -171.   -14.3
 8 AR    AR    ARG      32 Argent… 273669  3.87e7     19         5  -65.2  -35.4
 9 AS    AU    AUS      36 Austra… 768230  2.03e7      9        53  136.   -25.0
10 BA    BH    BHR      48 Bahrain     71  7.25e5    142       145   50.6   26.0
# … with 236 more rows
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 SpatialPolygonsDataFrame 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)
plot3d(allcoords, xlab = "", ylab = "") ## smart enough to treat 3-columns as X,Y,Z
rglwidget()

Plotting in the plane is one thing, but more striking is to convert the vertices from planar longitude-latitude to Cartesizan XYZ. Define an R function to take “longitude-latitude-height” and return spherical coordinates (we can leave WGS84 for another day).

llh2xyz <- 
function (lonlatheight, rad = 6378137, exag = 1) 
{
    cosLat = cos(lonlatheight[, 2] * pi/180)
    sinLat = sin(lonlatheight[, 2] * pi/180)
    cosLon = cos(lonlatheight[, 1] * pi/180)
    sinLon = sin(lonlatheight[, 1] * pi/180)
    rad <- (exag * lonlatheight[, 3] + rad)
    x = rad * cosLat * cosLon
    y = rad * cosLat * sinLon
    z = rad * sinLat
    cbind(x, y, z)
}

## deploy our custom function on the longitude-latitude values
xyzcoords <- llh2xyz(allcoords)

Now we can visualize these XYZ coordinates in a more natural setting, and even add a blue sphere for visual effect.

plot3d(xyzcoords, xlab = "", ylab = "")
spheres3d(0, 0, 0, radius = 6370000, col = "lightblue")
rglwidget()

This is still not very exciting, since our plot knows nothing about the connectivity between vertices.

Organization of polygons

The in-development R package gris provides a way to represent spatial objects as a set of relational tables. I’m leaving out the details because it’s not the point I want to make, but in short a gris object has tables “o” (objects), “b” (for branches), “bXv” (links between branches and vertices) and “v” the vertices.

If we ingest the wrld_simpl layer we get a list with several tables.

EDITOR NOTE (April 2022): see anglr function DEL0() for an updated way to create what gris was doing in 2015.

library(gris)  ## devtools::install_github("r-gris/gris")
library(dplyr)
gobject <- gris(wrld_simpl)

The objects, these are individual countries with several attributes including the NAME.

gobject$o
# A tibble: 246 × 12
   FIPS  ISO2  ISO3     UN NAME      AREA POP2005 REGION SUBREGION     LON   LAT
   <fct> <fct> <fct> <int> <fct>    <int>   <dbl>  <int>     <int>   <dbl> <dbl>
 1 AC    AG    ATG      28 Antigu…     44  8.30e4     19        29  -61.8   17.1
 2 AG    DZ    DZA      12 Algeria 238174  3.29e7      2        15    2.63  28.2
 3 AJ    AZ    AZE      31 Azerba…   8260  8.35e6    142       145   47.4   40.4
 4 AL    AL    ALB       8 Albania   2740  3.15e6    150        39   20.1   41.1
 5 AM    AM    ARM      51 Armenia   2820  3.02e6    142       145   44.6   40.5
 6 AO    AO    AGO      24 Angola  124670  1.61e7      2        17   17.5  -12.3
 7 AQ    AS    ASM      16 Americ…     20  6.41e4      9        61 -171.   -14.3
 8 AR    AR    ARG      32 Argent… 273669  3.87e7     19         5  -65.2  -35.4
 9 AS    AU    AUS      36 Austra… 768230  2.03e7      9        53  136.   -25.0
10 BA    BH    BHR      48 Bahrain     71  7.25e5    142       145   50.6   26.0
# … with 236 more rows, and 1 more variable: object_ <int>

The branches, these are individual simple, one-piece “ring polygons”. Every object may have one or more branches (branches may be an “island” or a “hole” but this is not currently recorded). Note how branch 1 and 2 (branch_) both belong to object 1, but branch 3 is the only piece of object 2.

gobject$b
# A tibble: 3,768 × 3
   object_ branch_ island_
     <int>   <int> <lgl>  
 1       1       1 TRUE   
 2       1       2 TRUE   
 3       2       3 TRUE   
 4       3       4 TRUE   
 5       3       5 TRUE   
 6       3       6 TRUE   
 7       3       7 TRUE   
 8       3       8 TRUE   
 9       4       9 TRUE   
10       5      10 TRUE   
# … with 3,758 more rows
plot(gobject[1, ], col = "#333333")
title(gobject$o$NAME[1])

plot(gobject[2, ], col = "#909090")
title(gobject$o$NAME[2])

(Antigua and Barbuda sadly don’t get a particularly good picture here, but this is not the point of the story.)

The links between branches and vertices.

gobject$bXv
# A tibble: 26,264 × 3
   branch_ order_ vertex_
     <int>  <int>   <int>
 1       1      1    5589
 2       1      2    5620
 3       1      3    5605
 4       1      4    5589
 5       2      1    5596
 6       2      2    5611
 7       2      3    5613
 8       2      4    5596
 9       3      1   16101
10       3      2   17581
# … with 26,254 more rows

This table is required so that we can normalize the vertices by removing any duplicates based on X/Y pairs. This is required for the triangulation engine used below, although not by the visualization strictly. (Note that we could also normalize branches for objects, since multiple objects might use the same branch - but again off-topic).

Finally, the vertices themselves. Here we only have X and Y, but these table structures can hold any number of attributes and of many types.

gobject$v
# A tibble: 21,165 × 3
       x_    y_ vertex_
    <dbl> <dbl>   <int>
 1 -61.7   17.0    5589
 2 -61.9   17.1    5620
 3 -61.8   17.2    5605
 4 -61.7   17.6    5596
 5 -61.9   17.6    5611
 6 -61.9   17.7    5613
 7   2.96  36.8   16101
 8   4.79  36.9   17581
 9   5.33  36.6   18103
10   6.40  37.1   18790
# … with 21,155 more rows

The normalization is only relevant for particular choices of vertices, so if we had X/Y/Z in use there might be a different version of “unique”. I think this is a key point for flexibility, some of these tasks must be done on-demand and some ahead of time.

Indices here are numeric, but there’s actually no reason that they couldn’t be character or other identifier. Under the hood the dplyr package is in use for doing straightforward (and fast!) table manipulations including joins between tables and filtering on values.

More 3D already!

Why go to all this effort just for a few polygons? The structure of the gris objects gives us much more flexibility, so I can for example store the XYZ Cartesian coordinates right on the same data set. I don’t need to recursively visit nested objects, it’s just a straightforward calculation and update - although we’re only making a simple point, this could be generalized a lot more for user code.

gobject$v$zlonlat <- 0
do_xyz <- function(table) {
  xyz <- llh2xyz(dplyr::select(table, x_, y_, zlonlat))
  table$X <- xyz[,1]
  table$Y <- xyz[,2]
  table$Z <- xyz[,3]
  table
}

gobject$v <- do_xyz(gobject$v)

gobject$v
# A tibble: 21,165 × 7
       x_    y_ vertex_ zlonlat        X         Y        Z
    <dbl> <dbl>   <int>   <dbl>    <dbl>     <dbl>    <dbl>
 1 -61.7   17.0    5589       0 2892546. -5369047. 1867388.
 2 -61.9   17.1    5620       0 2872491. -5376810. 1875991.
 3 -61.8   17.2    5605       0 2880293. -5370474. 1882167.
 4 -61.7   17.6    5596       0 2879394. -5354145. 1929470.
 5 -61.9   17.6    5611       0 2868217. -5361116. 1926758.
 6 -61.9   17.7    5613       0 2864423. -5358522. 1939577.
 7   2.96  36.8   16101       0 5100196.   264042. 3820852.
 8   4.79  36.9   17581       0 5083067.   425571. 3829093.
 9   5.33  36.6   18103       0 5095693.   475230. 3806402.
10   6.40  37.1   18790       0 5056321.   567008. 3846135.
# … with 21,155 more rows

I now have XYZ coordinates for my data set, and so for example I will extract out a few nearby countries and plot them.

localarea <- gobject[gobject$o$NAME %in% c("Australia", "New Zealand"), ]
## plot in traditional 2d
plot(localarea, col = c("dodgerblue", "firebrick"))

The plot is a bit crazy since parts of NZ that are over the 180 meridian skews everything, and we could fix that easily by modifiying the vertex values for longitude, but it’s more sensible in 3D.

rgl::plot3d(as.matrix(localarea$v[c("X", "Y", "Z")]), xlab = "", ylab = "")
rglwidget()

Finally, to get to the entire point of this discussion let’s triangulate the polygons and make a nice plot of the world.

The R package RTriangle wraps Jonathan Shewchuk’s Triangle library, allowing constrained Delaunay triangulations. To run this we need to make a Planar Straight Line Graph from the polygons, but this is fairly straightforward by tracing through paired vertices in the data set. The key parts of the PSLG are the vertices P and the segment indexes S defining paired vertices for each line segment. This is a “structural” index where the index values are bound to the actual size and shape of the vertices, as opposed to a more general but perhaps less efficient relational index.

pslgraph <- gris:::mkpslg(gobject)
dim(pslgraph$P)
[1] 21165     2
range(pslgraph$S)
[1]     1 21165
head(pslgraph$P)
            x_       y_
[1,] -61.68667 17.02444
[2,] -61.88722 17.10527
[3,] -61.79445 17.16333
[4,] -61.72917 17.60861
[5,] -61.85306 17.58305
[6,] -61.87306 17.70389
head(pslgraph$S)
     [,1] [,2]
[1,]    1    2
[2,]    2    3
[3,]    3    1
[4,]    1    1
[5,]    4    5
[6,]    5    6

The PSLG is what we need for the triangulation.

tri <- RTriangle::triangulate(pslgraph)

The triangulation vertices (long-lat) can be converted to XYZ, and plotted.

xyz <- llh2xyz(cbind(tri$P, 0))
open3d()
null 
   5 
triangles3d(xyz[t(tri$T), ], col = "grey", specular = "black")
aspect3d("iso"); bg3d("grey12")
rglwidget()

These are very ugly polygons since there’s no internal vertices to carry the curvature of this sphere. This is the same problem we’d face if we tried to drape these polygons over topography: at some point we need internal structure.

Luckily Triangle can set a minimum triangle size. We set a constant minimum area, which means no individual triangle can be larger in area than so many “square degrees”. This gives a lot more internal structure so the polygons are more elegantly draped around the surface of the sphere. (There’s not really enough internal structure added with this minimum area, but I’ve kept it simpler to make the size of this document more manageable).

tri <- RTriangle::triangulate(pslgraph, a = 9)  ## a (area) is in degrees, same as our vertices
xyz <- llh2xyz(cbind(tri$P, 0))
open3d()
null 
   6 
triangles3d(xyz[t(tri$T), ], col = "grey", specular = "black")
bg3d("gray12")
rglwidget()

We still can’t identify individual polygons as we smashed that information after putting the polygon boundary segments through the triangulator. With more careful work we could build a set of tables to store particular triangles between our vertices and objects, but to finish this story I just loop over each object adding them to the scene.

## loop over objects
cols <- sample(grey(seq(0, 1, length = nrow(gobject$o))))
open3d()
null 
   7 
for (iobj in seq(nrow(gobject$o))) {
  pslgraph <- gris:::mkpslg(gobject[iobj, ])
  tri <- RTriangle::triangulate(pslgraph, a = 9)  ## a is in units of degrees, same as our vertices
  xyz <- llh2xyz(cbind(tri$P, 0))
  triangles3d(xyz[t(tri$T), ], col = cols[iobj], specular = "black")
}
bg3d("gray12")
rglwidget()

Real world topography, Image textures

future work …

See anglr package