2

I'm trying to load elevation data from AWS using the elevatr package, with a region defined as a Simple Features Collection from the sf package. Here's a simple example:

library(elevatr)
library(sf)
library(USAboundaries)

counties <- us_counties(map_date = "1930-01-01", resolution = "high", states = c("NY"))
elevation <- get_elev_raster(counties$geometry, z = 9, src = "aws")

This gives an error:

Error in if (attributes(class(locations)) %in% c("raster", "sp")) { : 
  argument is of length zero
In addition: Warning messages:
1: In if (class(locations) == "data.frame") { :
  the condition has length > 1 and only the first element will be used
2: In if (class(locations) == "SpatialPoints") { :
  the condition has length > 1 and only the first element will be used
3: In if (class(locations) == "SpatialPointsDataFrame") { :
  the condition has length > 1 and only the first element will be used

Presumably because the sfc is a collection of polygons and MULTIPOINT objects, which aren't supported by the get_elev_raster function of the elevatr package.

Is there any way to use these packages together? With the end result being an sfc object that also has various forms of elevation data for each point?


I'm getting the data this way because it doesn't seem like the st_read function from the sf package can read ESRI ASCII files.

1
counties <- us_counties(map_date = "1930-01-01", resolution = "high", states = c("NY"))

this has created an sf object, but:

elevation <- get_elev_raster(counties$geometry, z = 9, src = "aws")

this expects an sp object. Convert sf to sp with:

counties_sp = as(counties, "Spatial")

then this should work:

elevation = get_elev_raster(counties_sp, z=..... etc.....)

I cant replicate your code at the moment because the USABoundariesData package is refusing to install...

  • Thank you, that works. Now I need to read up on how to get RasterLayer objects to play nicely with sf objects. – Michael A Aug 25 '18 at 13:47
  • (And I don't know why the USABoundariesData package won't install for you. This command usually works for me: install.packages("USAboundariesData", repos = "http://packages.ropensci.org", type = "source", dependencies=TRUE) but that's just pulled from the installation of USABoundaries) – Michael A Aug 25 '18 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.