Creating masks of layers

In this vignette, we will see a way to use information in a layer to mask another layer. This has a very practical application: the CHELSA2 data also cover open water, which is usually not something we want for most SDM applications. Thankfully, they share a grid with CHELSA1 data, and so we have a quick and dirty (we prefer "clever") way to mask the CHELSA2 layers.

using SpeciesDistributionToolkit
using Dates
using CairoMakie

We will focus on the various islands around the Java sea:

spatial_extent = (left = 104.523, bottom = -9.925, right = 128.979, top = -1.669)
(left = 104.523, bottom = -9.925, right = 128.979, top = -1.669)

Getting the precipitation layer for september in the CHELSA2 dataset is simply:

prec2 =
    SimpleSDMPredictor(
        RasterData(CHELSA2, Precipitation);
        month = Month(9),
        spatial_extent...,
    )
SDM predictor → 991×2936 grid with 2909576 UInt16-valued cells
  Latitudes	-9.925139209149997 ⇢ -1.666805908849988
  Longitudes	104.51652663975001 ⇢ 128.98319320855

The same layer can be queried from CHELSA1. Note that we are specifying the same layer and month here, which is not strictly speaking required (if we had multiple CHELSA2 layers, we would only want a single CHELSA1 layer to mask them anyways) but is done in for the sake of clarity:

prec1 = SimpleSDMPredictor(
    RasterData(CHELSA1, Precipitation);
    month = Month(9),
    spatial_extent...,
)
SDM predictor → 991×2936 grid with 671224 Int16-valued cells
  Latitudes	-9.925139209149997 ⇢ -1.666805908849988
  Longitudes	104.51652663975001 ⇢ 128.98319320855

The simplest way to make a mask is to use a layer to mask the other (the first argument is used as a template, the second is being masked):

masked_prec2 = mask(prec1, prec2)
SDM predictor → 991×2936 grid with 671224 UInt16-valued cells
  Latitudes	-9.925139209149997 ⇢ -1.666805908849988
  Longitudes	104.51652663975001 ⇢ 128.98319320855

We can confirm that the number or filled cells has decreased, and visual inspection of the result will further show that we have accomplished our goal;

heatmap(
    sprinkle(masked_prec2)...;
    colormap = :deep,
    figure = (; resolution = (800, 300)),
    axis = (; xlabel = "Longitude", ylabel = "Latitude"),
)
current_figure()

This page was generated using Literate.jl.