Home > database >  masking big raster using shapefile
masking big raster using shapefile

Time:04-22

I have a global raster at 0.0833 degree resolution which covers both land and ocean. I am trying to extract raster only for the land areas. The way I am doing is to use a global shapefile and use it as a mask. However, this is taking ages (more than 2 hours and still not done) and wondered if there's anything fundamentally wrong I am doing

library(raster)

# this is my raster
class      : RasterLayer 
dimensions : 4320, 10800, 46656000  (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333  (x, y)
extent     : -180, -90, -90, -54  (xmin, xmax, ymin, ymax)
crs        :  proj=longlat  datum=WGS84  no_defs 
source     : 1_High_2050_.tif 
names      : X1_High_2050_ 
values     : 3, 3  (min, max)

# my shapefile 
library(maptools)
data('wrld_simpl')
    
# my approach 
mask_raster <- mask(temp_rast, wrld_simpl)
    

CodePudding user response:

This is much faster with the terra package (the replacement for raster)

library(terra)
r <- rast(res=1/120, xmax=-90, ymax=-54)
r <- init(r, "cell")

library(geodata)
w <- world(path=".")

system.time(x <- mask(r, w))
#   user  system elapsed 
#   1.36    0.53    1.89 
  • Related