Problem with elevatr::get_elev_raster - what am I doing wrong?

420 Views Asked by At

I'm trying to make a topographic map in r. First step, download elevation data:

    ex_df <- data.frame(x=c(5.86, 7.7, 7.7, 5.86, 5.86), y=c(50.59, 50.59, 49.8, 49.8, 50.59))
    crs_obj <- crs("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
    elev <- get_elev_raster(ex_df, z=12, prj=crs_obj, clip = "bbox")

I get the following error:

Error in sp::CRS(prj) : 
  PROJ4 argument-value pairs must begin with +: GEOGCRS["unknown",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]],
        ID["EPSG",6326]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433],
        ID["EPSG",8901]],
    CS[ellipsoidal,2],
        AXIS["longitude",east,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433,
                ID["EPSG",9122]]],
        AXIS["latitude",north,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433,
                ID["EPSG",9122]]]]

Searching SO gave me this solution (Create topographic map in R):

    # Generate a data frame of lat/long coordinates.
    ex.df <- data.frame(x=seq(from=-73, to=-72.5, length.out=10), 
                        y=seq(from=41, to=41.5, length.out=10))
    
    # Specify projection.
    prj_dd <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
    
    # Use elevatr package to get elevation data for each point.
    elev <- get_elev_raster(ex.df, prj = prj_dd, z = 10, clip = "bbox")

And... same error. Clearly there is something wrong with the prj argument, but searching online is not clarifying how that should be fixed. Any ideas?

1

There are 1 best solutions below

2
On

Try with sf package:

library(sf)
library(magrittr)

ex.df %>% 
   st_as_sf(coords = c("x", "y"), crs = 4326) %>% 
   elevatr::get_elev_point()

Simple feature collection with 10 features and 2 fields
geometry type:  POINT
dimension:      XY
bbox:           xmin: -73 ymin: 41 xmax: -72.5 ymax: 41.5
geographic CRS: WGS 84 (with axis order normalized for visualization)
   elevation elev_units                   geometry
1       0.00     meters             POINT (-73 41)
2       0.00     meters POINT (-72.94444 41.05556)
3       0.00     meters POINT (-72.88889 41.11111)
4       0.00     meters POINT (-72.83333 41.16667)
5       0.00     meters POINT (-72.77778 41.22222)
6      52.59     meters POINT (-72.72222 41.27778)
7      56.54     meters POINT (-72.66667 41.33333)
8      85.23     meters POINT (-72.61111 41.38889)
9     171.73     meters POINT (-72.55556 41.44444)
10     87.20     meters         POINT (-72.5 41.5)