1
library(raster)
library(rgdal)
library(elevatr)
library(rgeos)
library(leaflet)
library(RColorBrewer)
library(rgl)

r1 <- raster()
s1 <- stack()
base <- 'https://portal.opentopography.org/API/globaldem?demtype=AW3D30'
api_key <- '<API-KEY>'
tmp_south <- 36.738884
tmp_north <- 38.091337
tmp_west <- -120.168457
tmp_east <- -118.465576
url <- paste0(base, '&south=', tmp_south, '&north=', tmp_north, '&west=', tmp_west, '&east=', tmp_east, '&API_Key=', api_key)
download.file(url, "./display.tif", mode = "wb")
r1 <- raster(system.file("display.tif"), package="raster")
image(r1)

The Error:

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file.

1 Answers1

1

The problem is your call to system.file().

I notice in the raster docs this is used in an example:

filename <- system.file("external/test.grd", package="raster")

However, system.file(), exists to find names of R package files, rather than to create file path objects. The reason it is used in the tutorial is to load demo data that is installed with the package. The tutorial says:

Do not use this system.file construction of your own files (just type the file name; don’t forget the forward slashes).

The raster() function accepts a first argument which is character vector. You can do:

r1  <- raster("./display.tif")

Then you can successfully run:

image(r1)

enter image description here

SamR
  • 8,826
  • 3
  • 11
  • 33