Created
August 28, 2018 06:46
-
-
Save mdsumner/3957a474f1146b6b11277b9c223cc0b2 to your computer and use it in GitHub Desktop.
Exploring triangles and points in 3D with mapdeck
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nc <- sf::read_sf(system.file("shape/nc.shp", package="sf")) | |
bb <- sf::st_bbox(nc) | |
bb <- bb + c(-2, -2, 2, 2) | |
topo <- marmap::as.raster(marmap::getNOAA.bathy(bb["xmax"], bb["xmin"], bb["ymax"], bb["ymin"], | |
resolution = 2)) | |
triangles <- sf::st_cast(sfdct::ct_triangulate(nc, a = .005)) | |
elevate_triangles <- function(g, r) { | |
## assuming geometry of POLYGON | |
sf::st_polygon(list(cbind(g[[1]], raster::extract(r, g[[1]])))) | |
} | |
elevate_sf <- function(x, r, exag = 1) { | |
sf::st_set_geometry(x, sf::st_sfc(purrr::map(sf::st_geometry(x), elevate_triangles, r = r * exag), crs = sf::st_crs(x))) | |
} | |
library(mapdeck) | |
x <- elevate_sf(triangles, topo) | |
p <- st_cast(x, "MULTIPOINT") | |
sc <- as.data.frame(sf::st_coordinates(p)) %>% dplyr::mutate(Z = Z * 10) | |
key <- Sys.getenv("MAPBOX_TOKEN") | |
mapdeck( | |
token = key, style = 'mapbox://styles/mapbox/dark-v9', location = c(145, -37.8), zoom = 10) %>% | |
add_path( | |
data = roads, stroke_colour = "RIGHT_LOC", layer_id = "path_layer" | |
) | |
mapdeck(token = key, style = 'mapbox://styles/mapbox/dark-v9', location = c(mean(bb[c("xmin", "xmax")]), mean(bb[c("ymin", "ymax")])), zoom = 5) %>% | |
add_polygon(data = x, layer_id = "polylayer", fill_colour = "NAME") %>% | |
add_path(data = sf::st_cast(x, "MULTILINESTRING"), layer_id = "pathlayer") %>% | |
add_pointcloud(data = sc, lon = "X", lat = "Y", elevation = "Z", | |
layer_id = "abc", stroke_width = 1, fill_colour = "L1") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment