Looking at the code of gdal_merge what I understand is this:
The effect of the "-n" option is the same as using input files with the NoDataValues set (to the same number). In both cases, gdal_merge ignores the pixels which present these values (the ones set beforehand as NoData or the ones set by the "-n" option). The value it places at their place is the value set by the "-init" option (if set, otherwise 0).
So, in your case you should just add the option "-init -32768" at the gdal_merge call (and you can ignore the -n option.
N option is useful, I guess, only if your input files don't have NoDataValues set, if they are set to different numbers or if you want to ignore other values, apart from the ones already set to NoData to your input.
My conclusion is that it's extremely important to always set your -init value in order to keep control of your dataset.
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)
Best Answer
This problem happened to me too, but it was because my directory was large. R was unable to find the way, so he gave this answer: Error in .rasterObjectFromFile (x, band = band, objecttype = "RasterLayer",: Cannot create a RasterLayer object from this file. (File does not exist)
After I put it in another folder with a smaller path, it worked.