RasterSource
Quick answer
RasterSource is ol.source.Raster, a source that resamples several image sources onto one grid and runs a per-pixel or whole-image operation over them. Supply sources plus an operation(inputs, data) function, choose operationType pixel or image, and set threads, lib and projection. Display the result as an image layer for band math, indices or classification.
new ol.source.Raster( options )
Key points
- inputs align with the sources order and hold Uint8ClampedArray RGBA pixels of equal length.
- The operation must return arrays of the same length; multiple arrays become output bands.
- beforeoperations and afteroperations events expose inputs, resolutions and returned data.
Example
var red = new ol.source.XYZ({ url: '/tiles/red/{z}/{x}/{y}.png' });
var nir = new ol.source.XYZ({ url: '/tiles/nir/{z}/{x}/{y}.png' });
var ndvi = new ol.source.Raster({
sources: [red, nir],
operationType: 'pixel',
threads: 2,
operation: function (inputs, data) {
var r = inputs[0];
var n = inputs[1];
var out = new Uint8ClampedArray(r.length);
for (var i = 0; i < r.length; i += 4) {
var redValue = r[i];
var nirValue = n[i];
var index = (nirValue - redValue) / (nirValue + redValue + 1e-6);
var v = Math.round(((index + 1) / 2) * 255);
out[i] = v;
out[i + 1] = 255 - v;
out[i + 2] = 90;
out[i + 3] = r[i + 3];
}
data.max = 1;
return [out];
}
});
var layer = new ol.layer.Image({ source: ndvi });
Notes
- Unapproved cross-origin source images taint the canvas and turn the whole result blank; set crossOrigin anonymous.
- The operation is serialized into workers, so closure variables fail; put helpers in lib and constants in data.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). RasterSource. https://www.jingweizhi.com/en/openlayers/api/raster-source.html (accessed 2026-11-03).