class: center, middle, inverse, title-slide # How to get rayshaded ## π ### Nathan βNateβ Day ### 2019-03-06 --- class: center # Rayshade yourself [@rayshaderbot](https://twitter.com/rayshaderbot) <iframe width="560" height="315" src="https://www.youtube.com/embed/7_YrB4FljsQ" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- # 5 Steps to Shade .large[ 1. Find a elevation map 2. Read that map into R 3. Convert it to raster 4. Convert it to matrix 5. π ] --- # 1) Find an elevation map The US government has got you (at least on this) [National Map App](https://viewer.nationalmap.gov/basic/) Download the elevation as IMG format ```r img_file <- "~/Downloads/USGS_NED_13_n38w080_IMG/USGS_NED_13_n38w080_IMG.img" ``` --- # 2) Read that map into R Be careful, they're big fellas ```r library(rgdal) GDALinfo(img_file) ``` ``` ## rows 10812 ## columns 10812 ## bands 1 ## lower left origin.x -80.00056 ## lower left origin.y 36.99944 ## res.x 9.259259e-05 ## res.y 9.259259e-05 ## ysign -1 ## oblique.x 0 ## oblique.y 0 ## driver HFA ## projection +proj=longlat +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +no_defs ## file ~/Downloads/USGS_NED_13_n38w080_IMG/USGS_NED_13_n38w080_IMG.img ## apparent band summary: ## GDType hasNoDataValue NoDataValue blockSize1 blockSize2 ## 1 Float32 TRUE -3.402823e+38 64 64 ## apparent band statistics: ## Bmin Bmax Bmean Bsd ## 1 118.096 1293.38 394.8112 190.6556 ## Metadata: ## BandDefinitionKeyword=NONE ## ConfigKeyword= ## DataType=Elevation ``` --- # 2b) Read *a small chunk of* that map into R Use `offset` and `region.dim` to subset ```r gdal <- readGDAL(img_file, region.dim = c(1000,1000) ) ``` --- # 3) Convert to raster ```r library(raster) rastr <- raster(gdal) ``` --- # 4) Convert to matrix ```r dat <- extract(rastr, extent(rastr), buffer = 1000 ) mat <- matrix(dat, nrow(rastr), ncol(rastr) ) ``` --- # 5) π ```r library(magrittr) library(rayshader) mat %>% sphere_shade() %>% plot_map() ``` ![](get_rayshaded_files/figure-html/ray1-1.png)<!-- --> --- # 5b) π π ποΏ½π ```r mat %>% sphere_shade() %>% add_water(detect_water(mat)) %>% add_shadow(ray_shade(mat)) %>% plot_map() ``` ![](get_rayshaded_files/figure-html/ray2-1.png)<!-- --> --- # 5c) π π¦ ```r mat %>% sphere_shade(texture = "bw") %>% add_water(detect_water(mat), color = "unicorn") %>% plot_map() ``` ![](get_rayshaded_files/figure-html/ray3-1.png)<!-- --> --- # Learn more Watch the best talk from rstudio::conf 2019 <p><a href="https://resources.rstudio.com/rstudio-conf-2019/3d-mapping-plotting-and-printing-with-rayshader?wvideo=rqe461mc6z"><img src="https://embedwistia-a.akamaihd.net/deliveries/6ae49f2cf01491080771ae662a4ec8c6.jpg?image_play_button_size=2x&image_crop_resized=960x540&image_play_button=1&image_play_button_color=4287c7e0" width="400" height="225" style="width: 400px; height: 225px;"></a></p><p><a href="https://resources.rstudio.com/rstudio-conf-2019/3d-mapping-plotting-and-printing-with-rayshader?wvideo=rqe461mc6z">3D mapping, plotting, and printing with rayshader - Tyler Morgan-Wall</a></p> [Rayshader on GitHub](https://github.com/tylermorganwall/rayshader)