Cannot create a RasterLayer object from this file - Chelsa climate tif file - r

I downloaded Chelsa bio files on my local directory.
And when I tried to create raster for my R project, I keep getting error with following message.
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
Cannot create a RasterLayer object from this file.
My code was simple and I was able to create worldclim climate raster file with the same code.
and, cannot figure out the difference.
download.file(url = "https://os.zhdk.cloud.switch.ch/envicloud/chelsa/chelsa_V2/GLOBAL/climatologies/1981-2010/bio/CHELSA_bio1_1981-2010_V.2.1.tif",
destfile = "Chelsa/bio1.tif")
bio1 <- raster("Chelsa/bio1.tif")
Could anyone advice on this?
I also tried
bio1 <- stack("Chelsa/bio1.tif")
But, similar error message popped out.
I also changed my directory - instead of putting it under subdirectory (named Chelsa), I directly put the file into my home directory. But, all didn't work.

Related

Cannot open the destfile when I download the data from the internet using the R function download.file

I tried to download the file from the internet, but the downloaded file (raster format) cannot load.
# download the data
download.file("https://zenodo.org/record/4287825/files/ChinaClim_baseline_prec_01.tif?download=1", destfile="prec_01.tif")
# load into Raster
library(raster)
raster("prec_01.tif")
# Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
# Cannot create a RasterLayer object from this file.
Anything wrong with my code? Thanks.
download.file("https://zenodo.org/record/4287825/files/ChinaClim_baseline_prec_01.tif?download=1", destfile="prec_01.tif", mode="wb") # On windows add mode = "wb" to download.file() as suggested by Martin Morgan
# load into Raster
library(raster)
raster("prec_01.tif")

Worldclim: getData() in R (latitude/longitude coordinates): Error in utils::unzip(zipfile, exdir = dirname(zipfile)) : 'exdir' does not exist

Uploading longitude and latitude coordinates in a .csv file using getData():
I am sorry for asking such a basic question but I am new to R, and I am having trouble uploading my .csv file containing latitude and longitude coordinates using the function getData(). The idea is to upload the data in worldlcim.
The code I used was:
bioclim.data <- getData(name = "worldclim",
var = "bio",
res = 2.5,
path "~/Documents/TerneyProposal/UpalPublishedPapers/Blue_Whale_Paper/Data_Blue_Whale_Project/Blue_Whale_GPS_CSV.csv")enter code here
However, I keep on getting this error message:
trying URL 'https://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/bio_2-5m_bil.zip'
Content type 'application/zip' length 129319755 bytes (123.3 MB)
==================================================
downloaded 123.3 MB
Could not download file -- perhaps it does not exist
Error in utils::unzip(zipfile, exdir = dirname(zipfile)) :
'exdir' does not exist
I don't understand this message because my .csv file is not in a zip file, and this file also opens easily when I use the function read.csv("data")
Very kind regards if anyone could please kindly advise?
Many thanks in advance!
Welcome to the community! These kinds of errors are a bit misleading... I think the main issue is probably that your file is not in the folder it expects. Run:
getwd()
to see what the working folder is... that is where it's expecting the file. Either move the file into that folder, or you can change your session's working directory to the folder where the file is, by writing:
setwd("pathToYourFile")
If you're on windows, remember to change those pesky \ to /.
Give it a try!

Reading multiple images in R for image processing

I am trying to use image processing in R, which is a new concept and a new language to me. I am aiming to read a lot of images from few folders to train. As a test scenerio, I kept two images in a folder, named: n04197391_11_0, n04197391_74_0. I plan to import the images, add an extra column, and then do require processing.
The following is my code I am using:
setwd("C:/Users/Ethan/Desktop/R")
library(EBImage)
files <- list.files(path="C:\\Users\\Desktop\\data\\images\\0", pattern=".jpg",all.files=T, full.names=F, no.. = T)
#This imports the files as: files - chr[1:2] "n04197391_11_0" "n04197391_74_0"
mypic1 <- list()
for (i in 1:length(files))
{
mypic1[[i]] <- readImage(files[i])
}
print(mypic1[[1]])
The following is the error that I am getting:
Error in transpose(y) : object is NULL
In addition: Warning message:
In .loadFun(files, ...) :
cannot open file 'n04197391_11_0.jpg': No such file or directory
print(mypic1[[1]])
Error in mypic1[[1]] : subscript out of bounds
I dont understand if the images are being read in files, why is it showing an error. Is there a workaround that I can take to read multiple images from folders in such a case? Thank you in Advance.

SHP file reading getting error in R

Hi I am trying to read and plot on a custom shape file in R which is not a map.
This is the code I use and the error I get in return:
library(rgdal)
mySHP<- choose.files()
myFile<- readOGR(mySHP)
Error in ogrListLayers(dsn = dsn) : Cannot open data source
If your file is a shapefile, you need to specify the dsn which is the directory where is saved the shapefile and layer which is the name of the shapefile without the extension. You cannot really do it with choose.files. At least not that simply.
myFile <- readOGR(dsn='path.to.folder', layer='nameOfShapefile')

readOGR with geojson giving me "cannot open file" error

I'm pretty new to using rgdal so I'm hoping this is something simple that I'm missing, but I've been googling around about it for a few hours and I can't figure out the issue.
Basically I'm trying to make a leaflet map in a shiny app, but I'm getting snarled right at the beginning, trying to load country data like so:
library(rgdal)
countries <- readOGR("https://raw.githubusercontent.com/datasets/geo boundaries-world-110m/master/countries.geojson", "OGRGeoJSON")
but every time I get the following error:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open file
I've gone to the address and I see the raw geojson file there, so it's not a missing file. I've also downloaded the file manually into a data folder and then tried to access it with
countries <- readOGR("data/countries.geojson", "OGRGeoJSON")
and I get the same error. Any ideas would be much appreciated.
I'm running R on Windows 7.

Resources