tylermorganwall / rayshader

R Package for 2D and 3D mapping and data visualization
https://www.rayshader.com/
2.08k stars 211 forks source link

Centering Plots and Renderings #189

Closed mrworthington closed 3 years ago

mrworthington commented 3 years ago

Hi Tyler,

I'm brand new to rayshader, so forgive me if this issue is more appropriate for rayrender, but here's my issue:

I'm trying to render an elevation of Texas using plot_3d(), but I can't seem to center the object. As a result, anytime it renders (either as render_snapshot() or as render_highquality()), it ends up in the upper-left hand corner of the screen and I'm not sure how to get the object to render in the center of the screen.

Is there a way to center the objects? I've posted a reprex below the images.

Resulting Images

plot_3d()

Screen Shot 2021-09-15 at 11 11 31 PM

render_snapshot()

texas_test

render_highquality()

Screen Shot 2021-09-15 at 11 08 22 PM

Code Used to Produce These Images

library(tigris)
#> To enable 
#> caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
library(rayshader)
library(elevatr)
library(raster)
#> Loading required package: sp
library(sf)
#> Warning: package 'sf' was built under R version 4.1.1
#> Linking to GEOS 3.9.1, GDAL 3.2.3, PROJ 7.2.1
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.1.1
#> Warning: package 'tibble' was built under R version 4.1.1
#> Warning: package 'readr' was built under R version 4.1.1
texas <- tigris::states() |>
  filter(NAME == "Texas") |> 
  st_transform(4326)
#>   |                                                                              |                                                                      |   0%  |                                                                              |                                                                      |   1%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |========                                                              |  11%  |                                                                              |========                                                              |  12%  |                                                                              |===========                                                           |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  17%  |                                                                              |=============                                                         |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  23%  |                                                                              |================                                                      |  24%  |                                                                              |=================                                                     |  24%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |===================                                                   |  28%  |                                                                              |====================                                                  |  28%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |======================                                                |  31%  |                                                                              |======================                                                |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |=========================                                             |  35%  |                                                                              |==========================                                            |  37%  |                                                                              |==========================                                            |  38%  |                                                                              |===========================                                           |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  40%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |===============================                                       |  44%  |                                                                              |===============================                                       |  45%  |                                                                              |================================                                      |  45%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |==================================                                    |  48%  |                                                                              |==================================                                    |  49%  |                                                                              |===================================                                   |  49%  |                                                                              |===================================                                   |  50%  |                                                                              |====================================                                  |  51%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  52%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  54%  |                                                                              |======================================                                |  55%  |                                                                              |=======================================                               |  55%  |                                                                              |=======================================                               |  56%  |                                                                              |========================================                              |  57%  |                                                                              |========================================                              |  58%  |                                                                              |=========================================                             |  58%  |                                                                              |==========================================                            |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |===========================================                           |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |============================================                          |  62%  |                                                                              |=============================================                         |  65%  |                                                                              |==============================================                        |  65%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |================================================                      |  68%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |===================================================                   |  72%  |                                                                              |===================================================                   |  73%  |                                                                              |=======================================================               |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  80%  |                                                                              |========================================================              |  81%  |                                                                              |=========================================================             |  81%  |                                                                              |=========================================================             |  82%  |                                                                              |==========================================================            |  82%  |                                                                              |==========================================================            |  83%  |                                                                              |===========================================================           |  84%  |                                                                              |===========================================================           |  85%  |                                                                              |============================================================          |  85%  |                                                                              |============================================================          |  86%  |                                                                              |=============================================================         |  87%  |                                                                              |=============================================================         |  88%  |                                                                              |==============================================================        |  88%  |                                                                              |==============================================================        |  89%  |                                                                              |===============================================================       |  89%  |                                                                              |===============================================================       |  90%  |                                                                              |================================================================      |  91%  |                                                                              |================================================================      |  92%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  94%  |                                                                              |==================================================================    |  95%  |                                                                              |===================================================================   |  95%  |                                                                              |===================================================================   |  96%  |                                                                              |====================================================================  |  96%  |                                                                              |====================================================================  |  97%  |                                                                              |===================================================================== |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================|  99%  |                                                                              |======================================================================| 100%
dem <- get_elev_raster(texas, z = 7)
#> Mosaicing & Projecting
#> Note: Elevation units are in meters.
dem_tx <- mask(dem, texas)
mat_tx <- raster_to_matrix(dem_tx)
mat_tx_reduced <- resize_matrix(mat_tx, 0.7)

mat_tx_reduced |>
  sphere_shade(texture = create_texture(lightcolor = "#F1ECD7",
                                        shadowcolor = "#333f48",
                                        leftcolor = "#bf5700",
                                        rightcolor = "#005f86",
                                        centercolor = "#9cadb7",
                                        cornercolors = c(NW = "#ffc500",
                                                         NE = "#387642",
                                                         SW = "#d27441",
                                                         SE = "#296176")),
               colorintensity=0.12,
               sunangle = 10) |>
  add_water(detect_water(mat_tx_reduced)) |>
  plot_3d(mat_tx_reduced,
          windowsize = c(800, 600),
          zscale = 5,
          zoom = 0.65,
          phi = 80,
          theta = 0,
          fov = 0,
          shadow = TRUE,
          shadowcolor = "#5da5da",
          water = TRUE,
          watercolor= "lightblue",
          waterlinecolor="lightblue")

  render_camera(zoom=0.4)

  render_snapshot(filename="texas_test.png",
                  title_text = "Texas",
                  title_offset = c(20,20),
                  title_color = "#2d2d2d",
                  title_size = 35,
                  title_font = "Gotham",
                  title_bar_alpha = 0.5,
                  title_position = "northwest",
                  bring_to_front = TRUE,
                  vignette = 0.2,
                  webshot = TRUE,
                  width = 2000,
                  height = 2000,
                  background = "#5da5da")

  render_highquality(filename = "texas.png", samples = 10, width = 6000, height = 6000)

Created on 2021-09-15 by the reprex package (v2.0.1)

tylermorganwall commented 3 years ago

Does the underlying raster contain a lot of NA values? The 3D map will be centered on the bounding box of the extent of the underlying elevation data. If there's a large amount of NA values south and east of the land, it will include those values while centering the 3D data.

mrworthington commented 3 years ago

Hey! I guess it did have some kind of underlying NAs in the elevation data. Although I wasn't sure how to actually check for NAs, I used the clip="bbox" argument when I pulled the elevation data and that fixed everything. Is there a recommended way to inspect elevation data for NAs?

tylermorganwall commented 3 years ago

One easy way is to plot a boolean matrix, e.g.

plot_map(is.na(mat_tx_reduced))