NOTE: none of these 3D scenes can be viewed on github so a temporary copy of this readme with the figures rendered is here: http://rpubs.com/cyclemumner/367003
Topological forms for plotting spatial data
The ‘anglr’ package illustrates some generalizations of GIS-y tasks in R with a database-y approach.
The basic idea is to showcase topological forms of objects from a variety of sources:
- silicate forms
- simple features
- Spatial features
- rgl 3D objects
- trip objects (and general animal tracking data types)
- regular raster grids
- others - let me know!
To do this anglr works with forms defined by
silicate and (after
for each of the models
TRI. Here we add two
DEL (for high-quality triangulation) and
The general approach is re-model your data using one of these models,
copy_down to augment the model with a Z coordinate, and
library(anglr) model <- sf::st_read("some/shapefile.shp") araster <- raster::raster("some/gridraster.tif") mesh <- copy_down(silicate::SC(model), araster) plot3d(mesh)
The copy down process will copy feature attributes (a constant measure) or raster attributes (a continuous measure) in the appropriate way. After copy down the mesh will be unique in XYZ, whereas the usual starting point is uniqueness in XY.
These mesh or topological forms can be used to merge disparate data
into a single form, or used to convert standard spatial objects to
An example of merging vector and raster can be seen with this.
## a global DEM data("gebco1", package = "anglr") library(sf) ## North Carolina, the sf boilerplate polygon layer nc <- read_sf(system.file("shape/nc.shp", package="sf")) library(raster) library(anglr) library(silicate) p_mesh <- DEL(nc, max_area = 0.002) ## a relief map, composed of triangles grouped by polygon with ## interpolated raster elevation p_mesh <- copy_down(p_mesh, z = gebco1) ## plot the scene library(rgl) rgl.clear() ## rerun the cycle from clear to widget in browser contexts plot3d(p_mesh) bg3d("black"); material3d(specular = "black") aspect3d(1, 1, .1) rglwidget() ## not needed if you have a local device
z argument to
copy_down is a raster, and so the
coordinate of the mesh is updated by extracting values from the raster.
z_ is alternately set to a column in the layer or a specific vector
of values this is used as a constant offset for the
z_ value, and the
mesh is separated by feature.
We only use TRI here both to illustrate its availability, but also because we only need poor quality triangles for planar geometry.
## either form works #c_mesh <- copy_down(TRI(nc), z = p_mesh$object$BIR74) c_mesh <- copy_down(TRI(nc), z = "BIR74") rgl.clear() a <- plot3d(c_mesh) bg3d("black"); material3d(specular = "black") aspect3d(1, 1, .2) rglwidget() ## not needed if you have a local device
In the silicate models, complex objects are decomposed to a set of related, linked tables. Object identity is maintained with attribute metadata and this is carried through to colour and other aesthetics.
Plot (3D) methods take those tables and generate the “indexed array”
structures needed for ‘rgl’. (
plot3d will return the rgl-model form).
This gets us part of the way towards having the best of both worlds of
GIS and 3D graphics.
Another example shows this approach applied to a 3D multipatch shapefile, with a few easy steps we can plot a wiremesh or roof/floor polygons from a civic building footprint.
The core work for translating spatial classes is done by the unspecialized ‘silicate::PATH’ function and its underlying decomposition generics. Ongoing work in the silicate package will improve and support these types more fully.
Planar polygons and lines are described by the same 1D primitives, and this is easy to do. Harder is to generate 2D primitives and for that we rely on Jonathan Richard Shewchuk’s Triangle.
Triangulation in DEL is with
RTriangle package using “constrained
mostly-Delaunay Triangulation” from the Triangle library, and TRI in
silicate uses ear-clipping via Mapbox’s
earcut.hpp (this is analogous
to but faster and more robust than
rgl::triangulate). Independent work
for mostly-Delaunay methods is in the
With RTriangle we can set a max area for the triangles, so it can wrap around curves like globes and hills, and this can only be done by the addition of Steiner points. All of this takes us very far from the path-based types generally used by GIS-alikes.
Raster gridded data are decomposed to
QUAD forms, essentially a 2D
primitive with four corners rather than three. This works well in rgl
and is super fast using the quadmesh package that can translate from the
Texture mapping is possible with rgl, but it needs a local coordinate system mapped to the index space of a PNG image. It’s easy enough but requires a bit of awkward preparation, not yet simplified.
Some different approaches:
Deprecated use of rangl here, but shows the general texture coordinate approach required:
We must use a version of R that is 3.3.2 or later, and
anglr can only
be installed from Github, and that at least requires the package
Also required are packages ‘rgl’ and ‘RTriangle’ and ‘sf’, so first make sure you can install and use these. On a fresh installation of R these package together require a lot of other contributed packages, and that can take some time.
install.packages("rgl") install.packages("RTriangle") install.packages("sf") install.packages("devtools")
With that out of the way, install from Github using devtools.
Feel free to contact me via the Issues if you have problems, or these notes don’t make sense.
On Linux you will need at least the following installed by an administrator, here tested on Ubuntu Xenial 16.04 (note the apt/sources.list is specific to version).
## key for apt-get update, see http://cran.r-project.org/bin/linux/ubuntu/README echo 'deb https://cloud.r-project.org/bin/linux/ubuntu xenial/' >> /etc/apt/sources.list apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9 ## up to date GDAL and PROJ.4 and GEOS ## https://launchpad.net/~ubuntugis/+archive/ubuntu/ubuntugis-unstable add-apt-repository ppa:ubuntugis/ubuntugis-unstable --yes apt update apt upgrade --assume-yes ## Install 3rd parties apt install libproj-dev libgdal-dev libgeos-dev libssl-dev libgl1-mesa-dev libglu1-mesa-dev libudunits2-dev ## install R, if you need to ## apt install r-base r-base-dev
Then in R
install.packages("devtools") install.packages(c("dplyr", "proj4", "raster", "rgl", "rlang", "RTriangle", "spbabel", "tibble", "viridis")) devtools::install_github("hypertidy/anglr")
Let me know if you have problems, or are interested in this work. See the issues tab to make suggestions or report bugs.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.