rspatial / raster

R raster package https://rspatial.github.io/raster/reference/raster-package.html
GNU General Public License v3.0
161 stars 52 forks source link

Reading of txt files for raster gives error: "Error in .rasterObjectFromFile(x, band Cannot create a RasterLayer object from this file." #314

Closed LauraMWarmuth closed 1 year ago

LauraMWarmuth commented 1 year ago

After no changes in code or files I suddenly received this error recently when reading in my raster files:

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

This issue applies to all the .txt files attached, but the .asc Bio-ORACLE files still work (I can't upload .asc files but accessible here: https://www.bio-oracle.org/downloads-to-email.php). Did an update in R or the raster package affect this?

I noticed it since around March/April and am now using R version (RStudio 2023.06.0+421 "Mountain Hydrangea" Release (583b465ecc45e60ee9de085148cd2f9741cc5214, 2023-06-05) for macOS Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) RStudio/2023.06.0+421 Chrome/110.0.5481.208 Electron/23.3.0 Safari/537.36) and raster package version (3.6-20).

I can read the .txt files with the terra package but the resulting SpatRaster doesn't work with MaxEnt.

Thanks! Laura

220510T222559_220429T143503_220427_sos_CMIP6_historical_1985_2015_mean-0p5deg.txt 220805T091459_220804T215723_220729_sos_CMIP6_ssp585_2071_2101_mean-0p5deg.txt 211007T114700_210824T225103_210726_sos_CMIP6_ssp585_2071_2101_diff-0p5deg.txt 210920T124039_210824T225103_210726_sos_CMIP6_ssp585_2071_2101_mean-0p5deg.txt

rhijmans commented 1 year ago

You can run maxent with the "predicts" package using SpatRaster objects

LauraMWarmuth commented 1 year ago

I did try that too, I was then able to read in the layers as SpatRaster but I wasn’t able to crop the file. And if I read a smaller file in from the region where the occurrence data is from I always get that error:

me <- MaxEnt(test, occ.train)
Error in .local(x, p, ...) : 
  more than half of the presence points have NA predictor values

Once I converted the occurrence data to SpatialPoints which always solved that error previously, I get another error:

me <- MaxEnt(test, presence.locations)
Error in .getMatrix(p) : 
  data should be  a matrix, data.frame, or SpatVector

My occurrence data is just two columns of coordinates and originally a data frame. Can I not use a SpatialPoints object and if not, how do I solve the first error then?

What's the best way to crop the SpatRaster if the below doesn't work? And how to flip the SpatRaster vertically?

 plot(test)
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'crds': error in evaluating the argument 'x' in selecting a method for function 'as.points': [ext] invalid extent
test.cropped <- crop(test, geographic.extent)
Error: [crop] extents do not overlap

Thanks!

durack1 commented 1 year ago

Some key differences, the Bio-ORACLE data look like:

NCOLS 4320 
NROWS 2160 
XLLCORNER -180 
YLLCORNER -90 
CELLSIZE 0.0833333333333333 
NODATA_value -9999 
-1.716213000000000 -1.716213 -1.716213 ...

Whereas these files look like:

ncols 81
nrows 119
xllcorner 308.5
yllcorner -3.0
cellsize -0.5
NODATA_value -99999
-99999 -99999 -99999 ...

Surely, the capitalization doesn't cause the problem?

rhijmans commented 1 year ago

The problem is the negative value for "cellsize". That is not possible, and now allowed. I would also suggest avoiding the ascii format as it is rather slow to process.