
Align multiple raster sources onto a common analysis grid
Source:R/foundation_tiles.R
foundation_tile_align.RdProjects every input SpatRaster onto a single template (common
CRS, common resolution, common extent), stacks them channel-wise,
and returns the unified mosaic. Designed to be robust to mixed
resolutions (SoilGrids at 250 m, WorldClim at 30 arc-sec, SRTM at
30 arc-sec, MODIS at 250 m, ERA5 at 0.1 degree) so MoCo v2
pretraining can consume a single multi-channel tensor.
Usage
foundation_tile_align(
sources,
target_res = 0.005,
target_crs = "EPSG:4326",
aoi = NULL,
method = "bilinear"
)Arguments
- sources
Named list of
terra::SpatRasterobjects.- target_res
Numeric resolution of the output grid, in the units of
target_crs.- target_crs
Character EPSG code of the output CRS (default
"EPSG:4326").- aoi
Optional AoI to crop after reprojection (see
foundation_tile_source_soilgrids()).- method
Resampling method for
terra::project(). Defaults to"bilinear"(good for continuous covariates).
Value
A single terra::SpatRaster with one layer per input
layer, aligned.
MODIS and ERA5
Neither is wrapped by edaphos: both need an account and a client
package. Download and pre-process them outside the package, read the
result with terra::rast(), and pass it in sources like any other
raster – nothing downstream cares where a raster came from.
MODIS (NASA EarthData login):
MODIStsp,rgeeor the AppEEARS service. MOD13Q1 stores NDVI as int16 with a 1e-4 scale factor and -3000 as the fill value, and both surviveterra::rast()untouched: set values below -2000 toNAand multiply by 1e-4 before aligning, or the channel mixes index values with sentinels. A 16-day composite is not a snapshot – neighbouring pixels can come from different days.ERA5-Land (Copernicus Climate Data Store key): the
ecmwfrpackage. The CDS bounding box is ordered north, west, south, east – the reverse of thec(xmin, xmax, ymin, ymax)everyfoundation_tile_*function expects. ERA5-Land is a 0.1 degree (about 9 km) reanalysis; projected onto a 250 m grid with bilinear resampling it becomes a smooth channel with no information below 9 km, harmless for a contrastive encoder and misleading in a covariate-importance table.
Examples
# \donttest{
if (requireNamespace("terra", quietly = TRUE)) {
# Two Cerrado covariates on deliberately mismatched grids: soil at
# 0.05 degrees, terrain at 0.10, on extents that only partly
# overlap.
set.seed(1)
soc <- terra::rast(nrows = 40, ncols = 40,
xmin = -50, xmax = -48, ymin = -16, ymax = -14,
crs = "EPSG:4326")
terra::values(soc) <- stats::rnorm(terra::ncell(soc), 22, 4)
names(soc) <- "soc"
elev <- terra::rast(nrows = 20, ncols = 20,
xmin = -49.5, xmax = -47.5,
ymin = -15.5, ymax = -13.5, crs = "EPSG:4326")
terra::values(elev) <- stats::rnorm(terra::ncell(elev), 900, 80)
names(elev) <- "elev"
fused <- foundation_tile_align(list(soil = soc, topo = elev),
target_res = 0.05)
print(terra::res(fused)) # both sources now on the 0.05 grid
print(names(fused)) # names come from the rasters, not from
# the names of `sources`
# The template is the UNION of the source extents, not their
# intersection, so each layer is NA wherever its own source did not
# reach -- 36 percent of the grid here, for both layers.
print(terra::global(fused, "isNA") / terra::ncell(fused))
# `aoi` cuts the template back to ground both sources cover, and
# that is what removes the NA frame.
inner <- foundation_tile_align(list(soil = soc, topo = elev),
target_res = 0.05,
aoi = c(-49.5, -48, -15.5, -14))
print(terra::global(inner, "isNA"))
# Mixed CRS is handled, but the union is taken AFTER reprojection.
# A UTM 23S tile several degrees east of the soil tile produces a
# mosaic that is 73 and 98 percent NA -- and a contrastive encoder
# will train on it perfectly happily.
utm <- terra::rast(nrows = 30, ncols = 30,
xmin = 700000, xmax = 760000,
ymin = 8250000, ymax = 8310000,
crs = "EPSG:31983")
terra::values(utm) <- stats::rnorm(terra::ncell(utm), 14, 3)
names(utm) <- "clay"
far <- foundation_tile_align(list(soil = soc, clay = utm),
target_res = 0.05)
print(dim(far))
print(round(terra::global(far, "isNA") / terra::ncell(far), 2))
# `target_res` is in the units of `target_crs`. The 0.005 default is
# degrees; carrying it over to a metre-based CRS asks for 5 mm
# cells. Give metres -- and read the realised resolution off the
# result, because terra fits a whole number of cells into the
# extent instead of honouring `target_res` exactly.
polyconic <- foundation_tile_align(list(soil = soc),
target_res = 5000,
target_crs = "EPSG:5880")
print(terra::res(polyconic)) # near 5000 m, not exactly 5000
# Two sources that each carry a layer called "soc" give a stack with
# two layers called "soc", and [["soc"]] then returns both of them
# with only a warning. Rename before aligning, not after.
dup <- foundation_tile_align(list(a = soc, b = soc), target_res = 0.05)
print(names(dup)) # "soc" "soc"
print(terra::nlyr(dup[["soc"]])) # 2, after a warning that reads
# "[subset] using a name that is
# not unique"
# method = "bilinear" is for continuous covariates. Use "near" for
# anything categorical -- interpolating soil-class codes invents
# classes that do not exist.
}
#> [1] 0.05 0.05
#> [1] "soc" "elev"
#> isNA
#> soc 0.36
#> elev 0.36
#> isNA
#> soc 0
#> elev 0
#> [1] 40 149 2
#> isNA
#> soc 0.73
#> clay 0.98
#> [1] 4968.697 4990.300
#> [1] "soc" "soc"
#> Warning: [subset] using a name that is not unique
#> [1] 2
# }