pfrater / arcpullr

21 stars 9 forks source link

Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...) : Cannot create a RasterLayer object from this file. #16

Open Phil-Longenecker-APD opened 1 year ago

Phil-Longenecker-APD commented 1 year ago

Hi there, I keep getting this error when using the package:

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

I'm trying to pull a portion of this raster using a bounding box: https://server6.tplgis.org/arcgis6/rest/services/Heat_Severity_2021/ImageServer/query?outFields=*&where=1%3D1

Any help would be greatly appreciated!

library(arcpullr) library(sf)

Define the URL for the ArcGIS REST API

heat_severity_url <- "https://server6.tplgis.org/arcgis6/rest/services/Heat_Severity_2021/ImageServer/"

Define the sf object for the bounding box

Define the sf object with the original coordinates

sf_object <- st_sf(st_as_sfc(st_bbox(c(xmin = -123.156738, xmax = -122.036133, ymin = 45.201393, ymax = 45.744527), crs = st_crs(3857))))

Use the get_image_layer function to pull the raster layer from the ArcGIS REST API

heat_severity_layer <- get_image_layer(heat_severity_url, sf_object)

zac-driscoll commented 1 year ago

Hi @Phil-Longenecker-APD ,

Thanks for using the package! It looks like there may be an issue with the CRS that you are using to create your bounding box. I was able to make this work when I changed the CRS to "4326" (rather than the projected CRS "3857"). Could you try again and let us know if it works for you?

library(arcpullr) library(sf)

heat_severity_url <- "https://server6.tplgis.org/arcgis6/rest/services/Heat_Severity_2021/ImageServer/"

sf_object <- st_as_sf(st_as_sfc(st_bbox( c( xmin = -123.156738, xmax = -122.036133, ymin = 45.201393, ymax = 45.744527 ) )), crs = st_crs(4326))

heat_severity_layer <- get_image_layer(heat_severity_url, sf_object) plot_layer(heat_severity_layer)

portland_heat_index

Phil-Longenecker-APD commented 1 year ago

Thanks for the prompt response! I wonder if its something specific to my environment. I uninstalled and reinstalled both packages and am using R 4.3.0, RStudio 2023.03.0

Here is the full error message:

Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...) : Cannot create a RasterLayer object from this file. In addition: Warning message: `~/AppData/Local/Temp/Rtmpq2Uwir/file83cc61931ec' not recognized as a supported file format. (GDAL error 4)

When I look at the temp file folder in file explorer, a file does download but doesn't have a specified file type.

Phil-Longenecker-APD commented 1 year ago

After reinstalling R and Rstudio to the latest, it appears the issue may have to do with sf. Here is the new error message:

library(arcpullr) library(sf)

heat_severity_url <- "https://server6.tplgis.org/arcgis6/rest/services/Heat_Severity_2021/ImageServer/"

sf_object <- st_as_sf(st_as_sfc(st_bbox( c( xmin = -123.156738, xmax = -122.036133, ymin = 45.201393, ymax = 45.744527 ) )), crs = st_crs(4326))

heat_severity_layer <- get_image_layer(heat_severity_url, sf_object)

The legacy packages maptools, rgdal, and rgeos, underpinning the sp package, which was just loaded, will retire in October 2023. Please refer to R-spatial evolution reports for details, especially https://r-spatial.org/r/2023/05/15/evolution4.html. It may be desirable to make the sf package available; package maintainers should consider adding sf to Suggests:. The sp package is now running under evolution status 2 (status 2 uses the sf package in place of rgdal) Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...) : Cannot create a RasterLayer object from this file. In addition: Warning message: `~/AppData/Local/Temp/RtmpsXaPvy/file5578f45678c' not recognized as a supported file format. (GDAL error 4)

zac-driscoll commented 1 year ago

Thanks for the update. The get_image_layer function relies on the "raster" package. This package utilizes the "rgdal" package, which as the message indicates, is being retired. These packages use "sp" rather than "sf". I think the first part of your error is a message warning us that these packages will be going away shortly. We are currently working on overhauling the functions to use "terra" rather than "raster".

I think the second part of the Error is what is causing the function to fail. As you pointed out in your previous message, the get_image_layer function works by downloading a file to a temp folder. From there it will convert the file to a Raster Stack using the raster::stack function. For whatever reason this function appears to be having problems converting this file.

Could there be issues related to permissions within your temp folder? Could you move the file out of this folder and try running raster::stack on it? Could you try running terra::rast on the file to see if you get a similar error?

What version of rgdal / sp are you running?