rmasklayermaxent

Modifying and Masking Environmental Layers within specific asia area in R


I am trying to prepare the environmental layers (constrained in specific Asia area) for use in Maxent model. However, I ran into some error messages in the last line:

library(sp)
library(maptools)
library(rworldmap)
library(dismo)
# A specified range of Asia area that suitable for special species
tsta <- read.csv('CM10_Kop_Shp_V1.2/Asiaclip/Asiaclipt.csv',as.is=TRUE)[https://drive.google.com/file/d/0B4vIx9MCfJgfbHpINTlyUGZVbXc/view?usp=sharing][1]
tsta <- tsta[,seq(1,4)]
coordinates(tsta) = c("Lon", "Lat")
gridded(tsta) <- TRUE
ra <- raster(tsta)

# a Rasterstack contains global range of 40 bioclim variables
files3 <- list.files(path=paste
                     ("CM10_1975H_Bio_ASCII_V1.2/CM10_1975H_Bio_V1.2"),
                     , pattern='txt',full.names=TRUE )[https://www.climond.org/Core/Authenticated/Data/CM10_V1.2/CM10_Bio_V1.2/CM10_Bio_ASCII_V1.2/CM10_1975H_Bio_ASCII_V1.2.zip][1]

predictors3 <- stack(files3)
asia.predictors3 <- mask(predictors3,ra)

Error in compareRaster(x, mask) : different extent

The details for predictors3 were

predictors3
class       : RasterStack 
dimensions  : 857, 2160, 1851120, 40  (nrow, ncol, ncell, nlayers)
resolution  : 0.1666667, 0.1666667  (x, y)
extent      : -180, 180, -59.16667, 83.66667  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
names       : CM10_1975H_Bio01_V1.2, CM10_1975H_Bio02_V1.2, CM10_1975H_Bio03_V1.2, CM10_1975H_Bio04_V1.2, CM10_1975H_Bio05_V1.2, CM10_1975H_Bio06_V1.2, CM10_1975H_Bio07_V1.2, CM10_1975H_Bio08_V1.2, CM10_1975H_Bio09_V1.2, CM10_1975H_Bio10_V1.2, CM10_1975H_Bio11_V1.2, CM10_1975H_Bio12_V1.2, CM10_1975H_Bio13_V1.2, CM10_1975H_Bio14_V1.2, CM10_1975H_Bio15_V1.2, ... 

The details for ra were:

ra
class       : RasterLayer 
dimensions  : 213, 290, 61770  (nrow, ncol, ncell)
resolution  : 0.1666667, 0.1666667  (x, y)
extent      : 97.5, 145.8333, 18.16667, 53.66667  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : in memory
names       : Location 
values      : 168505, 377653  (min, max)

My goal is to prepare a RasterLayer or Rasterstack contains all variables of "predictors3" but limited in the range of "ra". As you can see the extent of ra was included in the extent of predictors3 and their resolutions were identical. How should I fix the error?


Solution

  • In this case, as the origin and resolution of ra and predictors3 are the same, you can use crop

    predictors3 <- raster(xmn=-180, xmx=180, ymn=-59.16667, ymx=83.66667, res=1/6)
    ra <- raster(xmn=97.5, xmx=145.8333, ymn=18.16667, ymx=53.66667, res=1/6)
    
    x <- crop(predictors3, ra)
    

    In other cases, you may need to use (dis)aggregate or resample