From ba444117bb07cc540d769e8f76e986a1232eb001 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Fri, 1 Oct 2021 11:47:27 -0600 Subject: [PATCH 1/2] Use nodata values in the GeoTIFF headers for fill value --- src/ol/source/GeoTIFF.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/ol/source/GeoTIFF.js b/src/ol/source/GeoTIFF.js index eadf679630..e94045f919 100644 --- a/src/ol/source/GeoTIFF.js +++ b/src/ol/source/GeoTIFF.js @@ -582,6 +582,7 @@ class GeoTIFFSource extends DataTile { const addAlpha = this.addAlpha_; const bandCount = this.bandCount; const samplesPerPixel = this.samplesPerPixel_; + const nodataValues = this.nodataValues_; const sourceInfo = this.sourceInfo_; for (let sourceIndex = 0; sourceIndex < sourceCount; ++sourceIndex) { const source = sourceInfo[sourceIndex]; @@ -599,12 +600,27 @@ class GeoTIFFSource extends DataTile { return bandNumber - 1; }); } + + /** @type {number|Array} */ + let fillValue; + if (!isNaN(source.nodata)) { + fillValue = source.nodata; + } else { + if (!samples) { + fillValue = nodataValues[sourceIndex]; + } else { + fillValue = samples.map(function (sampleIndex) { + return nodataValues[sourceIndex][sampleIndex]; + }); + } + } + requests[sourceIndex] = image[this.readMethod_]({ window: pixelBounds, width: size[0], height: size[1], samples: samples, - fillValue: source.nodata, + fillValue: fillValue, pool: getWorkerPool(), interleave: false, }); @@ -612,7 +628,6 @@ class GeoTIFFSource extends DataTile { const pixelCount = size[0] * size[1]; const dataLength = pixelCount * bandCount; - const nodataValues = this.nodataValues_; const normalize = this.normalize_; return Promise.all(requests).then(function (sourceSamples) { From 9d709cb3d9d9832b57bd5647bc82a204bd3ec32b Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Fri, 1 Oct 2021 13:48:35 -0600 Subject: [PATCH 2/2] Additional documentation on the nodata property --- src/ol/source/GeoTIFF.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ol/source/GeoTIFF.js b/src/ol/source/GeoTIFF.js index e94045f919..cacb5aad83 100644 --- a/src/ol/source/GeoTIFF.js +++ b/src/ol/source/GeoTIFF.js @@ -25,8 +25,10 @@ import {fromCode as unitsFromCode} from '../proj/Units.js'; * the configured min and max. * @property {number} [max] The maximum source data value. Rendered values are scaled from 0 to 1 based on * the configured min and max. - * @property {number} [nodata] Values to discard. When provided, an additional band (alpha) will be added - * to the data. + * @property {number} [nodata] Values to discard (overriding any nodata values in the metadata). + * When provided, an additional alpha band will be added to the data. Often the GeoTIFF metadata + * will include information about nodata values, so you should only need to set this property if + * you find that it is not already extracted from the metadata. * @property {Array} [bands] Band numbers to be read from (where the first band is `1`). If not provided, all bands will * be read. For example, if a GeoTIFF has blue (1), green (2), red (3), and near-infrared (4) bands, and you only need the * near-infrared band, configure `bands: [4]`.