Created
July 2, 2020 22:25
-
-
Save eamcvey/238370bd6da53837a1c4f09573545d8a to your computer and use it in GitHub Desktop.
How to make a 3-D map with GPX and USGS data in R using rayshader
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
| # Setup ------------------------------------------------------------------- | |
| library(tidyverse) | |
| library(sf) | |
| library(elevatr) | |
| library(rgdal) | |
| library(raster) | |
| library(rasterVis) | |
| library(rayshader) | |
| library(magick) | |
| library(leaflet) | |
| library(geosphere) | |
| library(av) | |
| # Specify GPX and associated elevation files ------------------------------ | |
| # You can export GPX from Strava by going to an activity on their web app, | |
| # go to the three dot menu on the left, export GPX | |
| route_tracks_pts <- st_read('strava_data/Couch_Potato_Race.gpx', layer = 'track_points') | |
| # You can get the elevation data for the relevant area by going here: https://viewer.nationalmap.gov/basic/#productSearch | |
| # Then find the area, get the URL for the 3DEP, download, and unzip | |
| usgs_elev <- raster('elevation_data/USGS_13_n36w083.tif') | |
| # Prep for mapping -------------------------------------------------------- | |
| # Adding a buffer to the extent so the trail doesn't go right to the edge | |
| buffer_ll <- 0.01 | |
| cropped_elev <- crop(usgs_elev, extent(route_tracks_pts) + buffer_ll) | |
| # Convert the raster to the matrix rayshader expects | |
| elmat <- raster_to_matrix(cropped_elev) | |
| attr(elmat, 'extent') <- extent(cropped_elev) | |
| # Extract the lat and lon values for the GPX points | |
| trail_lat <- st_coordinates(route_tracks_pts)[,2] | |
| trail_lon <- st_coordinates(route_tracks_pts)[,1] | |
| # Render map -------------------------------------------------------------- | |
| # Optional to start with a clean slate | |
| rgl::rgl.close() | |
| # 3D map | |
| elmat %>% | |
| sphere_shade(texture = "imhof1") %>% | |
| add_shadow(ray_shade(elmat, zscale = 5), 0.5) %>% | |
| plot_3d(elmat, zscale=5) | |
| # Add points for trail | |
| render_points(extent = attr(elmat,"extent"), heightmap = elmat, | |
| lat = trail_lat, long = trail_lon, | |
| zscale=5, color="black", offset=100, size=2) | |
| render_snapshot() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment