Errors when getting elevation data using R

117 Views Asked by At

I am trying to get elevation data for Russia. Below are my codes. I used geoboundaries to get the boundary of Russia first. Then use get_elev_raster to get the elevation data.

library(rgeoboundaries)
library(elevatr)

russia <- rgeoboundaries::geoboundaries(country = "Russia",adm_lvl = "adm0")
get_elev_raster(russia,z=4)

However, it returns an error message:

Error in proj_expand(locations, prj, expand) : 
  The elevatr package requires longitude in a range from -180 to 180.

What's wrong with my codes? I tried similar codes for some European countries like France and Italy. The codes all worked well.

Thank you so much!

1

There are 1 best solutions below

0
On

The problem is that the region you are trying to get crosses the international date line, which elevatr doesn't like. You will need to get the East and West parts separately

library(rgeoboundaries)
library(elevatr)
library(sf)

russia <- rgeoboundaries::geoboundaries(country = "Russia",adm_lvl = "adm0")

rus <- as.data.frame(st_coordinates(russia))


r_west <- get_elev_raster(rus[rus$X > 0 & rus$X < 180,], z = 4,
                prj = "+proj=longlat +datum=WGS84 +no_defs")

r_east <- get_elev_raster(rus[rus$X < 0 & rus$X > -180,], z = 4,
                          prj = "+proj=longlat +datum=WGS84 +no_defs")

Now you have two rasters for the East and West parts of Russia. We can plot this as follows:

df <- setNames(as.data.frame(r_west, xy = TRUE), c("x", "y", "elevation"))
df2 <- setNames(as.data.frame(r_east, xy = TRUE), c("x", "y", "elevation"))

ggplot(df, aes(x, y, fill = elevation)) +
  geom_raster() +
  geom_raster(data = df2) +
  geom_sf(data = russia, fill = NA, inherit.aes = FALSE) +
  scale_fill_viridis_c()

enter image description here