Spatial interpolation is commonly used to estimate meteorological
variables at locations where no direct observations are available. The
kriging_inmet() function implements ordinary
kriging using weather station observations and spatial
prediction locations provided as sf objects.
This vignette demonstrates how to interpolate accumulated rainfall from INMET weather stations.
The example uses rainfall observations from the 2024 Rio Grande do Sul flood event together with municipality geometries.
data("floods_rs")
data("mun_stations")
# Alternative to: geobr::read_municipality(year = 2024)
tmp <- tempfile(fileext = ".rda")
on.exit(unlink(tmp), add = TRUE)
download.file(
"https://github.com/kaiorb52/dados_municipais/raw/main/mun_24.rda",
destfile = tmp,
mode = "wb"
)
load(tmp)The station observations are converted to an sf object
and projected to a planar coordinate reference system, which is required
for distance-based spatial analysis.
The kriging_inmet() function computes an empirical
variogram, fits a spherical variogram model, and performs ordinary
kriging predictions for the target locations.
krig_result <- kriging_inmet(
stations_df = rain_sf,
mun_geo = mun_grid_sf
)
#> [using ordinary kriging]The predicted values (var1.pred) and prediction
variances (var1.var) are then attached to the municipality
dataset.
The following map displays municipality-level rainfall estimates obtained through ordinary kriging.
For comparison, the map below assigns each municipality the rainfall value from its closest weather station.
data("inmet_stations")
mun_stations2 <- mun_stations |>
left_join(inmet_stations |> select(code_wmo, creation_year)) |>
filter(creation_year <= 2024) |>
arrange(distance) |>
distinct(code_ibge7, .keep_all = TRUE)
mun_floods <- floods_rs |>
left_join(
mun_stations2,
by = c("code_wmo" = "code_wmo")
)
p2 <- mun_24 |>
left_join(
mun_floods |>
select(code_ibge7, total_rainfall),
by = c("code_muni" = "code_ibge7")
) |>
ggplot() +
geom_sf(aes(fill = total_rainfall), color = NA) +
labs(title = "Nearest Station") +
scale_fill_distiller(
palette = "RdYlGn",
direction = 1,
na.value = "grey80"
) +
theme_void() +
theme(
legend.position = c(0.9, 0.1)
)The figure below compares rainfall estimates generated using ordinary kriging against values obtained from the nearest-station approach.
Ordinary kriging generally produces smoother spatial surfaces and incorporates information from multiple nearby stations, whereas the nearest-station method assigns the same value to all municipalities linked to a given station and may introduce abrupt spatial discontinuities.
Each approach has advantages and limitations:
The histograms below compare the distribution of municipality-level estimates from both approaches.