Error adding Open Street Map Data to Rayshader Maps

Hi,

I am having an error when adding Open Street Map data do a rayshader map.

The error is that the overlapping of openstreetmap data is shifted. You may see the result in the image that I am appending.

The code is reproducible, and the data may be downloaded in the link showed in the code.

# based on gist: https://gist.github.com/norwegianblueparrot/b9d5d48f2d591d78a14320bf17459cc5

library("here")
library("sf")
library("rgl") 
library("raster") 
library("rayshader")
library("osmdata") 
library("tidyverse")

## ----load_elevation_data---------------------------------------------------------------
# set this path to wherever you've saved the elevation data - it can be downloaded from 
# https://download.pdok.nl/rws/ahn3/v1_0/05m_dsm/R_13DN2.ZIP

elevation_data <- here::here("data/elevation_data/bourtange")

# this data can be downloaded from
elevation_file <- elevation_data %>%
  file.path("R_13DN2.TIF")

elevation_raster <- elevation_file %>%
  raster::raster()

raster_crs <- elevation_raster %>%
  raster::crs()

## ----zoom_to_fortress---------------------

xmin <- 7.1869551422
xmax <- 7.1971968768
ymin <- 53.0042753099
ymax <- 53.0105807242

cropped_extent <- raster::extent(c(xmin, xmax, ymin, ymax)) %>%
  sf::st_bbox(crs = 4326) %>%
  sf::st_as_sfc() %>%
  sf::st_transform(crs = raster_crs) %>%
  sf::st_bbox() %>%
  raster::extent()

cropped_raster <- elevation_raster %>%
  raster::crop(y = cropped_extent)

plot(cropped_raster)

## ----scale_matrix----------------------
scaling_factor <- 2 # set > 1 to reduce size of matrix

raster_matrix <- cropped_raster %>%
  rayshader::raster_to_matrix(verbose = FALSE) %>%
  rayshader::resize_matrix(scale = 1 / scaling_factor)

# set missing values to -1 (assume this is water)
raster_matrix[is.na(raster_matrix)] <- -1

## ----calculate_zscale---------------------------------------------------------
estimated_zscale <- 0.5


## ----build_base_map-----------------------------------------------------------
base_map <- raster_matrix %>%
  rayshader::height_shade() %>%
  rayshader::add_overlay(rayshader::sphere_shade(raster_matrix, colorintensity = 5),
                         alphalayer = 0.5)  %>%
  rayshader::add_overlay(
    rayshader::sphere_shade(raster_matrix,
                            texture = "desert",
                            colorintensity = 5),
    alphalayer = 0.5
  ) %>%
  rayshader::add_shadow(rayshader::lamb_shade(raster_matrix), 0) %>%
  rayshader::add_shadow(rayshader::ambient_shade(raster_matrix), 0) %>%
  rayshader::add_shadow(
    rayshader::texture_shade(
      raster_matrix,
      detail = 8 / 10,
      contrast = 9,
      brightness = 11
    ),
    0.1
  )

## ----get_osm_overlays---------------------------------------------------------

bbox_long_lat <- sf::st_bbox(cropped_raster) %>%
  sf::st_as_sfc() %>%
  sf::st_transform(crs = 4326) %>%
  sf::st_bbox()

# get highways
bourtange_footways <- osmdata::opq(bbox_long_lat) %>%
  osmdata::add_osm_feature("highway") %>%
  osmdata::osmdata_sf()

footway_geom <- bourtange_footways$osm_lines %>%
  sf::st_transform(crs = raster_crs)

footway_overlay <- footway_geom %>%
  rayshader::generate_line_overlay(
    extent = cropped_extent,
    heightmap = raster_matrix,
    color = "gainsboro",
    linewidth = 2
  )

## ----render_overhead_map-------------------------------------------------------
base_map %>%
  rayshader::add_overlay(overlay = footway_overlay, alphalayer = 0.95) %>%
  rayshader::plot_3d(
    raster_matrix,
    water = TRUE,
    watercolor = "dodgerblue",
    windowsize = c(1200, 1200),
    zscale = estimated_zscale
  )

rayshader::render_camera(theta = 0, phi = 89.999, zoom = 0.75)
rayshader::render_snapshot()

Could you please help me find what is wrong?

Thank you,
Luis

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.