Filling Raster Gaps with Cokriging
The kriging article on this site predicts a value everywhere from scattered points, using only how far apart those points are. But a raster with gaps is a different problem: you already have a dense grid, most of it valid, and a second band of the same file that’s fully populated and correlated with the one that isn’t (elevation next to a cloud-gapped spectral band, say). Ignoring that second band and kriging the gaps from the target’s own neighbours alone throws away real information. Cokriging is kriging that uses it: fit a model of how the two bands vary together, not just how one varies with distance, then predict the missing values from wherever the covariate is available.
Prefer to skip straight to a result? The standalone tool runs the same computation: upload your own multi-band raster, pick the two bands, and it fills the gaps for you.
1. Upload your raster
A multi-band GeoTIFF (.tif/.tiff) with at least two bands: one you want filled (has real gaps, NoData cells), one that doesn’t. That second band is the covariate: elevation, another spectral band, anything correlated with the first.
2. Why a covariate band helps
If you’ve read the kriging article, you’ve already met the variogram: bin every pair of points by distance, average their squared difference within each bin (half of it, actually, which is why the axis says semivariance, not variance), fit a smooth curve to those bins. That curve’s nugget, sill, and range describe how one variable varies with itself over distance. Cokriging needs one more piece: the cross-variogram, the same idea applied to two variables at once. It asks how much the target’s value at one location tends to differ from the covariate’s value at another location, as a function of the distance between them.
Fitting a target’s variogram, a covariate’s variogram, and their cross-variogram together, consistently enough that the combined system stays mathematically valid (technically: positive definite), is a linear model of coregionalization (LMC). Once fit, predicting the target at a gap uses both: the target’s own nearby known values, and the covariate’s value right there, weighted by how strongly the two actually move together. Where the covariate is missing too, there’s nothing extra to weight in. Cokriging can’t fill that pixel any differently than plain kriging could, which is why this article (and the standalone tool) leaves those cells empty rather than guessing.
4. Compute: fit the model in R — runnable here; Python — reference only
This code doesn’t run on this page, and it isn’t the same algorithm. See why Python takes a different approach here.
# Reference only -- not executable in this browser tool, and NOT a
# port of the R cell to the left. GSTools' "collocated cokriging"
# assumes the covariate is known at every point you're predicting
# (true here -- the covariate band has no NoData either, at any pixel
# the target needs filling), and uses that single co-located value
# directly instead of fitting a full cross-variogram between the two
# variables. Simpler, and it asks less of the data, but it's a
# genuinely different method from the fit.lmc() coregionalization
# model the R cell above fits -- see this article's own note on why
# that distinction matters before treating the two as interchangeable.
5. The target’s own variogram
This is the target band’s direct variogram, the same shape as the kriging article’s own chart: half the average squared difference within each distance bin, with the fitted curve’s nugget, sill, and range marked on it. It’s not the whole story. The actual cokriging fit above also needs the covariate’s own variogram and the cross-variogram between the two, neither shown separately here. But it’s the piece that carries over directly from ordinary kriging, and the one place this article’s chart can show something the reader already knows how to read.
Once the model above looks right, predict the gaps themselves:
Still a different method, continued. If GSTools’ collocated cokriging were used instead of the R cell’s LMC, this is the step that would actually predict. Same caveat as above.
# Reference only, for the same reason as the fit step above: GSTools'
# collocated cokriging predicts directly from the fitted model and the
# covariate's own values at the target locations, no separate
# "prediction dataset" the way gstat's predict() needs one built by
# hand -- a real structural difference from the R cell to the left,
# not just a syntax one.
6. Why Python takes a different approach here
R’s gstat (built on sp/sf) has no WebAssembly build of its closest Python equivalent, pykrige. That’s the same gap the kriging article already documents, and it’s true again here for the same reason: no pykrige wheel published for Pyodide. But cokriging specifically has a second, more interesting wrinkle worth being precise about instead of glossed over: Python’s own GSTools library does offer cokriging (Simple and Intrinsic Collocated Cokriging), but that’s a genuinely different technique from the fit.lmc() linear model of coregionalization the R cell above fits, not just a different implementation of the same idea. Collocated cokriging assumes the covariate is known at every location you’re predicting, and uses that one co-located value directly, without fitting a full cross-variogram between the two variables the way an LMC does. It asks less of the data and skips a real modeling step, which is reasonable when its own assumption holds but not a drop-in substitute for what this article and the standalone tool actually compute. No verified reference code for it sits on this page: writing some without running it against a real GSTools install would risk teaching a technique this project hasn’t actually checked.
7. Where to next
- Just needed the map? The standalone Raster Gap Filler: upload your own multi-band file, pick the two bands, done.
- Only have one band, no covariate? Ordinary kriging on the raster’s own valid pixels still works: the standalone tool falls back to it automatically when you leave the covariate on “None.” See the kriging article for how that half of the pipeline works on its own.
Second tool in the GeoStatistics family.