R Raster error: Cannot create a RasterLayer object from this file. (file does not exist) - r

Trying to pull in and scale a Landsat image but receive an error, this seems to be a somewhat common issue given the older questions and various ways of creating a RasterLayer object.
After loading the libraries I need I get to this and hit an issue:
Directory <- "D:/Geo Files/LANDSAT"
prefix <- "CU_LC08.001_"
suffix <- "_doy2020222_aid0001.tif"
## Get band 2 reflectance (blue, 0.45-0.515 micron)
sr2 <- raster(paste0(Directory,prefix,"SRB2",suffix))
Error in .local(.Object, ...) :
*Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
Cannot create a RasterLayer object from this file. (file does not exist)*
> sr2 = sr2*0.0001 #Scale
Error: object 'sr2' not found
The full name of the first (SRB2) file I want to use is:
CU_LC08.001_SRB2_doy2020222_aid0001.tif - the name of the file as it's downloaded, unchanged.
The rest of the code continues through the other files to go through each SRB and later calculate NDVI, etc
i.e. ## Get band 3 reflectance (green, 0.533-0.590 micron) sr3 <- raster(paste0(Directory,prefix,"SRB3",suffix)) sr3 = sr3*0.0001 #Scale
Edit 1: I tried using try using \ instead of forward slashes and got exactly the same error.
R version is R-4.0.3
Any help would be appreciated, this was provided to us in a hackathon and I am new to R, it should be simple to run but if the file cannot be read..?
EDIT 2: The issue was with the end of the path, it needed "\"
Directory <- "D:\\Geo Files\\LANDSAT\\"
But when trying to get another file to work from a different folder later in the code, the same issue appears, and the above fix did nothing:
Directory <- "D:\\Geo Files\\ECOSTRESS\\USCities\\"
LST_ECO <- raster(paste0(Directory,"SDS_LST_doy2020260040819_aid0001.tif"))
Error in .local(.Object, ...) :
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
Cannot create a RasterLayer object from this file. (file does not exist)
EDIT 3: Changed \Geo Files\ to "\GeoFiles\" as suggested, tried again but problem still occurs as above.

I had the same issue and the following was the process that finally worked.
# location of the file
loc_1 = "D:\\Geo Files\\ECOSTRESS\\USCities\\SDS_LST_doy2020260040819_aid0001.tif"
# check if the file can be read into r
tif1 = readr::read_file(loc_1)
It raised the following error (edited):
Error: Cannot read file "D:\\Geo Files\\ECOSTRESS\\USCities\\SDS_LST_doy2020260040819_aid0001.tif": The cloud file provider is not running.
The reason was because R could not read the file as it was in an online folder.
Confirm if the file is available offline. If it can be read by read_file function then the raster function should work.

I had the same issue
i was loading a raster file from my local storage
I added ".tif" extension to the name of the file and it worked

Related

Error when uploading Raster files that previously worked

I have a set of .tif files that I am trying to import using the Raster package. Previously it was working fine. I have ensured that the .tif files are all in the folder on my hard drive that I am trying to pull them from using the following code:
rm(list=ls())
unlink(".RData")
library(rgeos)
library(sp)
library(raster)
library(rgdal)
library(PopGenReport)
mydsn<-"~/RasterFiles"
setwd(mydsn)
CRS("+init=epsg:4267")
ColNrthResist<-raster(paste0(mydsn,"colnrthresist01.tif"))
The error I am getting is as follows:
Error in .local(.Object, ...) :
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
Cannot create a RasterLayer object from this file. (file does not exist)
##check wd is set properly
getwd()
[1] "C:/Users/mhugh/Documents/RasterFiles"
I have also tried using the readr::read_file(file_name) and get a similar error. I was running this code last week with no issues and am unsure where the error is coming from. I know this has been asked a couple times but none of the answers posted are working for me.
Edit: I have also tried completely uninstalling and reinstalling R with no solution (still getting the same error)

Finding and setting the working directory within a super computer

Obviously one can find and set working directories with getwd() and setwd(). This question is a bit more complicated. I'm running two files on a super computer (one is a regular R file which calls the other file (a .stan file). The way I submit work to the super computer is that I have to zip a folder containing the data, the .R file, and the .stan file. I upload this folder, and I pull this folder by setting it as one of the parameters in the super computer. I call the data using the standard read.csv() command and everything is hunky dory.
However, when I call the .stan file from the .R file, it can't access it because it needs to know the working directory.
This is the error I get:
> fit <- stan(file="ace_thresholds.stan", data=stanData, cores = 4)
Error in file(fname, "rt") : cannot open the connection
In addition: Warning messages:
1: In normalizePath(file) :
path[1]="ace_thresholds.stan": No such file or directory
2: In file(fname, "rt") :
cannot open file 'ace_thresholds.stan': No such file or directory
Error in get_model_strcode(file, model_code) :
cannot open model file "ace_thresholds.stan"
Calls: stan -> stan_model -> stanc -> get_model_strcode
Execution halted
When I tried setting the working directory to the unzipped NSG_stan folder (which is what I assumed the wd to be, I received this error:
fit <- stan(file="NSG_stan/ace_thresholds.stan", data=stanData, cores = 4)
Error in file(fname, "rt") : cannot open the connection
In addition: Warning messages:
1: In normalizePath(file) :
path[1]="NSG_stan/ace_thresholds.stan": No such file or directory
2: In file(fname, "rt") :
cannot open file 'NSG_stan/ace_thresholds.stan': No such file or directory
Error in get_model_strcode(file, model_code) :
cannot open model file "NSG_stan/ace_thresholds.stan"
Calls: stan -> stan_model -> stanc -> get_model_strcode
Execution halted
So I tried running print(getwd()) within the script and in the printout I see that the wd is
"/projects/ps-nsg/home/nsguser/ngbw/workspace/NGBW-JOB-RTOOL_TG-EBE9CDBF28BF42AF8CB6EC9355006B3E/NSG_stan"
which means that the working directory will shift with every job. So to accurately set the working directory, I'd need to set it to the current folder within the script. I looked for various posts on how to do this like the following
# install.packages("rstudioapi") # run this if it's your first time using it to install
library(rstudioapi) # load it
# the following line is for getting the path of your current open file
current_path <- getActiveDocumentContext()$path
# The next line set the working directory to the relevant one:
setwd(dirname(current_path ))
# you can make sure you are in the right directory
print( getwd() )
The issue with this is that it's a super computer, so it's difficult to install packages, because every time I want to install something, I have to email the folks associated with the super comp and that all takes time.
I've looked over this thread as well: R command for setting working directory to source file location in Rstudio. Appears to be a lot of dissent over what works. I tried a couple of them, and they didn't work.
setwd(getSrcDirectory()[1])
this.dir <- dirname(parent.frame(2)$ofile)
setwd(this.dir)
I've included the .R file below, in the event that it helps, but I think this is probably a pretty easy answer for someone with a decent amount of coding experience.
.R file (so it's the "ace_thresholds.stan" file that I either need to link to the current wd or to include the code that would set the wd, such that this "ace_thresholds.stan" call would work. Does that make sense?
Thanks much!
dat <- ace.threshold.t2.samp
dat <- subset(dat, !is.na(rw))
dat$condition <- factor(dat$condition)
dat$pid <- factor(dat$pid)
nTotal <- dim(dat)[1]
nCond <- length(unique(dat$condition))
nSubj <- length(unique(dat$pid))
intensity <- dat$rw
condition <- as.numeric(dat$condition)
pid <- as.numeric(dat$pid)
correct <- dat$correct_button == "correct"
chancePerformance <- 1/2
stanData <- list(nTotal=nTotal, nLevels=nCond, nSubj = nSubj, subject = pid, intensity=intensity, level=condition, correct=correct, chancePerformance=chancePerformance)
fit.rw <- stan(file="ace_thresholds.stan", data=stanData, cores = 4, control=list(max_treedepth=15, adapt_delta=0.90))

Cannot load a shapefile in R "Error: no features found"

I'm trying to read the shapefile that you can download with this url.
I have a code similar to the next one to download automatically the files:
library("raster")}
url<-"http://www6.gipuzkoa.eus/CATASTRO/Planos/ZIP-A098.zip"
downloader::download(url, dest=paste0(getwd(),"/","my_file.zip"), mode="wb",quiet=T)
zipped_shape_names<-c("098_HELBIDE_SHP/ATRIBUTOAK-A098.cpg","098_HELBIDE_SHP/ATRIBUTOAK-A098.dbf","098_HELBIDE_SHP/ATRIBUTOAK-A098.shp","098_HELBIDE_SHP/ATRIBUTOAK-A098.shx")
unzip("my_file.zip", files=zipped_shape_names)
my_shape<-raster::shapefile("098_HELBIDE_SHP/ATRIBUTOAK-A098.shp")
But what I obtain is the following error:
Error in rgdal::readOGR(dirname(x), fn, stringsAsFactors = stringsAsFactors, :
no features found
In addition: Warning messages:
1: In .local(x, ...) : .prj file is missing
2: In ogrFIDs(dsn = dsn, layer = layer) : no features found
You can access to the original web page by this link and pressing "Descargar planos"
I don't have this problem with others areas, just with this and another one, but I don't know what is happening with this specific area.
Any help will be appreciated.
The error message is quite clear. no features found means that your shapefile is empty. You can check this in multiple ways.
One is to add your shapefile in Q-Gis or any other GIS-software tool. In the case of Q-GIS your shapefile will pop-up in the layers pane, but you won't see any features.
It is also possible to check the .dbf file in R:
library(foreign)
read.dbf("098_HELBIDE_SHP/ATRIBUTOAK-A098.dbf")
The .dbf should contain as many rows as there are features. In your case None.
Simpler code would be
library(raster)
url<-"http://www6.gipuzkoa.eus/CATASTRO/Planos/ZIP-A098.zip"
download.file(url, dest="my_file.zip")
unzip("my_file.zip")
s <- raster::shapefile("098_HELBIDE_SHP/ATRIBUTOAK-A098.shp")
Clearly that file is empty. However, it works for this file:
s <-raster::shapefile("098_LANDALUR_SHP/LANDALUR-PARTZELAK-A098.shp")

Difficulty opening a package data file of unknown type

I am trying to load the state map from the maps package into an R object. I am hoping it is a SpatialPolygonsDataFrame or something I can turn into one after I have inspected it. However I am failing at the first step – getting it into an R object. I do not know the file type.
I first tried to assign the map() output to an R object directly:
st_m <- maps::map(database = "state")
draws the map, but str(st_m) appears to do nothing, unless it is redrawing the same map.
Then I tried loading it as a dataset: st_m <- data("stateMapEnv", package="maps") but this just returns a string:
> str(stateMapEnv)
chr "R_MAP_DATA_DIR"
I opened the maps directory win-library/3.4/maps/mapdata/ and found what I think is the map file, “state.L”.
I tried reading it with scan and got an error message I do not understand:
scan(file = "D:/Documents/R/win-library/3.4/maps/mapdata/state.L")
Error in scan(file = "D:/Documents/R/win-library/3.4/maps/mapdata/state.L") :
scan() expected 'a real', got '#'
I then opened the file with Notepad++. It appears to be a binary or compressed file.
So I thought it might be an R data file with an unusual extension. But my attempt to load it returned a “bad magic number” error:
st_m <- load("D:/Documents/R/win-library/3.4/maps/mapdata/state.L")
Error in load("D:/Documents/R/win-library/3.4/maps/mapdata/state.L") :
bad restore file magic number (file may be corrupted) -- no data loaded
Observing that these responses have progressed from the unhelpful through the incomprehensible to the occult, I thought it best to seek assistance from the wizards of stackoverflow.
This should be able to export the 'state' or any other maps dataset for you:
library(ggplot2)
state_dataset <- map_data("state")

Uploading a model to synapse

I tried to upload a data model to Synapse, and I got the following error message (from remote console):
Error in if (grepl("/$", filePath)) { : argument is of length zero In
addition: Warning messages: 1: In file.copy(object$getCacheDir(),
path, recursive = TRUE) : problem creating directory
/home/creighto#bcm.edu/scratch/tmp/RtmpHjLEQh/cacheRoot5e4dbe7c02d/archive.zip_unpacked/.R_OBJECTS:
No space left on device 2: In unzip(filename, exdir = destdir) : error
1 in extracting from zip file.
Also my directory has frozen up, and I cannot seem to access files. Have I exceeded a space limit here?
Sorry to state the obvious, but it does look like you've ran out of space: you can't unzip file due to space, so you can't create a directory. Can you reduce the file size?

Resources