Search code examples
rstackrasterqgismaxent

Issue with making environmental raster layers identical for Maxent


I have nine raster layers (.tif) and each needs to have the same extent, resolution and CRS in order to work in Maxent. I have tried converting each layer to the same CRS and translating them to .asc format in QGIS. After that I tried to resample the layers in R to match one of the layers, but this resulted in errors, such as that the extents do not overlap. My question is how do I match all these layers in order to proceed with Maxent and also to use the 'stack' function in R?

Here is the zip-file with the rasters: https://drive.google.com/file/d/1lle95SPdQ7FyQSbFoFvmAzyuO2HUt7-L/view?usp=sharing


Solution

  • So the initial problem is to set the crs using the 'crs' function from the raster package (I haven't used the new terra package yet). Then you need to reproject into the same crs. The next step is to resample the rasters so they all have the same cell resolution and size. Last you can put them in a stack. I was in a rush, so I didn't comment very well, but let me know if you have questions. The last point is the bedrock file. You'll need to use QGIS or another program to georeference it first. Try to find a map with a known projection that looks similar to it.

    library(raster)
    ls = list.files(".",pattern ="tif")
    ls = ls[-which(ls == "bedrock.tif")]
    r  = lapply(ls,raster)
    names(r) = ls
    wgs84 = "+proj=longlat +datum=WGS84 +no_defs"
    ETRS = "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs"
    
    crs(r$wc2.1_2.5m_bio_1.tif) = wgs84
    crs(r$wc2.1_2.5m_bio_12.tif) = wgs84
    crs(r$wc2.1_2.5m_elev.tif) = wgs84
    crs(r$SBPC1.tif) = ETRS
    crs(r$SBPC2.tif) = ETRS
    crs(r$SPPC1.tif) = ETRS
    crs(r$SPPC2.tif) = ETRS
    crs(r$U2018_CLC2018_V2020_20u1.tif) = ETRS
    # aggregate for faster processing -- you'll want to change this, but my machine couldn't process it
    ra <- lapply(r,aggregate,fact=10, fun=max)
    # not all need to be reprojected - this is me being lazy
    rp = lapply(ra,projectRaster, crs = ETRS)
    
    # resample rasters to match
    sapply(rp,area)
    rpr = lapply(rp,resample, y = rp$SBPC1.tif)
    sapply(rpr,area)
    rs = stack(rpr)
    plot(rs)