library(magrittr)
library(viridis)
library(raster)
library(rayshader)
Using the DEM available here which the source states is using the LV03 (a.k.a. EPSG 21781) coordinate reference system:
dem.200m.rast <- raster('~/Downloads/data/DHM200.asc')
crs(dem.200m.rast) <- crs('+init=epsg:21781')
Visualising the elevation raster with raster::plot
plot(dem.200m.rast, col = viridis(n = 1e3))
My extraction of the data from the raster
to a matrix
results in a distorted image:
dem.200m.mat <- raster::as.matrix(dem.200m.rast)
apply(X = dem.200m.mat, MARGIN = 2, FUN = rev) %>%
t() %>%
graphics::image(x = ., col = viridis(n = 1e3))
So I guess what we're seeing here is that raster::plot
is adjusting the aspect ratio of the plot in accordance with the coordinate reference system while there is no such information associated with the matrix for image
or rayshader
to use (though I don't think either of these have the functionality to estimate such an adjustment from the CRS).
dem.200m.mat.t <- t(dem.200m.mat)
system.time(
rayshader::sphere_shade(dem.200m.mat.t, texture = 'bw') %>%
rayshader::plot_map()
)
## user system elapsed
## 3.192 0.108 3.300
try
and
But, the "correct" aspect ratio does depend on the cell sizes of the source data if they are different - the .asc format in use here technically doesn't support uneven cell sizes, but it can be done and some software supports it.
Can you show?
print(dem.200m.rast)
It's unfortunate that this format is used, because other better formats actually can store the CRS - so I'd find an alternative if you can.
Otherwise, my general advice is to stay in raster context as much as possible. I'm experimenting with raster inputs for rayshader 3D plots here, for example: https://github.com/hypertidy/cartilage