You can manage multi-extent-problem resampling your data before mask()
function. This work for aligned and non-aligned pixels (for non-aligned, choose wisely method
argument). Also, you can use extend
to align boundaries of your data. I'd made an reproducible example for recreate our problem:
library(raster)
a <- raster(xmn=-100, xmx=100, ymn=-90, ymx=90,res=10)
# for reproducible example
r1 <- raster(xmn=-180, xmx=180, ymn=-90, ymx=90,res=10)
r2 <- raster(xmn=-190, xmx=180, ymn=-80, ymx=10,res=10)
r3 <- raster(xmn=-50, xmx=130, ymn=-80, ymx=90,res=10)
a <- setValues(a, 1:ncell(a))
r1 <- setValues(r1, 1:ncell(r1))
r2 <- setValues(r2, 1:ncell(r2))
r3 <- setValues(r3, 1:ncell(r3))
files <- list()
files[[1]] <- r1
files[[2]] <- r2
files[[3]] <- r3
results <- list()
for(i in 1:length(files)) {
e <- extent(a)
r <-files[[i]] # raster(files[i])
rc <- crop(r, e)
if(sum(as.matrix(extent(rc))!=as.matrix(e)) == 0){ # edited
rc <- mask(rc, a) # You can't mask with extent, only with a Raster layer, RStack or RBrick
}else{
rc <- extend(rc,a)
rc<- mask(rc, a)
}
# commented for reproducible example
results[[i]] <- rc # rw <- writeRaster(rc, outfiles[i], overwrite=TRUE)
# print(outfiles[i])
}
env_data<- stack(results)
The result will depends of the nature of you data, in this case I exaggerated the problem. After this code, we got this:
plot(env_data)
Edit:
Test your layers
library(raster)
a <- raster(xmn=-100, xmx=100, ymn=-90, ymx=90,res=10)
# for reproducible example
r1 <- raster(xmn=-180, xmx=180, ymn=-90, ymx=90,res=10)
r2 <- raster(xmn=-190, xmx=180, ymn=-80, ymx=10,res=10)
r3 <- raster(xmn=-50, xmx=130, ymn=-80, ymx=90,res=10)
# Other posibilities
r4 <- raster(xmn=-200, xmx=-150, ymn=-80, ymx=90,res=10)
r5 <- raster(xmn=-100, xmx=100, ymn=-90, ymx=90,res=5)
r6 <- a
files <- list()
files[[1]] <- r1
files[[2]] <- r2
files[[3]] <- r3
files[[4]] <- r4
files[[5]] <- r5
files[[6]] <- r6
library(testthat)
test <- list()
for(i in 1:length(files)){
test[[i]] <- capture_warnings(compareRaster(a,files[[i]], res=T, orig=T, stopiffalse=F, showwarning=T))
}
test
[[1]]
[1] "different extent" "different number or columns"
[[2]]
[1] "different extent" "different number or columns"
[3] "different number or rows"
[[3]]
[1] "different extent" "different number or columns"
[3] "different number or rows"
[[4]]
[1] "different extent" "different number or columns"
[3] "different number or rows"
[[5]]
[1] "different number or columns" "different number or rows"
[3] "different resolution"
[[6]]
character(0)
Lets look closely at the error, and, in conjunction with some knowledge on how R works, figure out what is actually happening, and how to debug it:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘shapefile’ for signature ‘"NULL"’
If you read the help for shapefile:
Usage:
## S4 method for signature 'character'
shapefile(x, stringsAsFactors=FALSE, verbose=FALSE, warnPRJ=TRUE, ...)
## S4 method for signature 'Spatial'
shapefile(x, filename='', overwrite=FALSE, ...)
There are two valid signatures - one for when the first argument is a character, in which case it treats that as a shapefile path and reads it into R, and the second where the first argument is a "Spatial" object and it gets written to a Shapefile specified by the filename
argument.
What you've done, somehow, is to call shapefile
with a NULL argument. I can replicate your error:
> shapefile(NULL)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘shapefile’ for signature ‘"NULL"’
It looks like you are trying to write some data to a shapefile, so for some reason which I don't think we can reproduce because we don't have your data, you've got an empty data object, and R is refusing to write a shapefile with no data in it.
Tracing this back in your code, rhrIsopleths(locoh[[i]])
must be a NULL object for some value of i
. Which value of i
? You need to figure that out and then make sure locoh[[i]]
is valid and then figure out why rhrIsopleths(locoh[[i]])
is NULL. Maybe there's no data and hence no isopleths.
Best Answer
Oops, found a bug with the extent not being coerced to the correct object class. Just posted a new version of the function in the development version. You can install it using:
remotes::install_github("jeffreyevans/spatialEco")
No clear idea on why it is working on Windows 7, as the package version should be the same and this is, in fact, a bug. Perhaps you have an older version of R running under Windows 7 that is using an older package version before this bug was introduced