Overlaying an image onto Rayshader map so that they are aligned

229 Views Asked by At

The below code gets the elevation data for an area in London and the equivalent bing map. I plot the elevation code using rayshader and overlay the bing map however they don't match up. This is because the bing map dimensions don't match the rayshader map. I could change the dimensions via the width and height variables on the png function and get them to match using trial and error. However I would like to do this programatically without the need for such manual tweaking.

library(elevatr)
library(rayshader)
library(OpenStreetMap)

#london bbox
mapextent<-c(51.477482,-0.120438,51.527517,0.051910)

#get elevation data
elev_data <- get_elev_raster(
  locations = data.frame(x = c(mapextent[2],mapextent[4]), y = c(mapextent[1],mapextent[3])),
  z = 10,
  prj = "EPSG:4326",
  clip = "bbox")
elev_mat<-raster_to_matrix(elev_data)

#create bing png to overlay
png("bing_map.png")

map<-openmap(upperLeft=c(mapextent[1], mapextent[2]), 
             lowerRight=c(mapextent[3],mapextent[4]), 
             type = "bing")

plot(map)
dev.off()
bing_map <- png::readPNG("bing_map.png")

#put together in a 3d map
elev_mat %>%
  sphere_shade() %>%
  add_overlay(bing_map, alphalayer = 0.9) %>%
  plot_3d(elev_mat, zscale = 10, fov = 0, theta = 0, phi = 60,
          windowsize = c(600, 450),
          zoom = 0.7,
          background = "lightgrey")

This is how the map looks, you can see that the bing map is too narrow.

enter image description here

Does anyone know how to get the dimensions of the rayshader map in advance so I can then pass these to the png function? Im aware there are other ways to overlay maps but I want to use the above method.

1

There are 1 best solutions below

0
On

Every entry in elev_mat is a point in the rendered image. So

height=nrow(elev_mat)
width=ncol(elev_mat)