Handle NaN nodata

This commit is contained in:
Tim Schaub
2022-05-11 19:02:38 -06:00
parent 6341ed3a64
commit 25b2007636
9 changed files with 72 additions and 6 deletions

View File

@@ -476,8 +476,7 @@ class GeoTIFFSource extends DataTile {
const image = images[imageIndex]; const image = images[imageIndex];
const nodataValue = image.getGDALNoData(); const nodataValue = image.getGDALNoData();
metadata[sourceIndex][imageIndex] = image.getGDALMetadata(0); metadata[sourceIndex][imageIndex] = image.getGDALMetadata(0);
nodataValues[sourceIndex][imageIndex] = nodataValues[sourceIndex][imageIndex] = nodataValue;
nodataValue === null ? NaN : nodataValue;
const wantedSamples = this.sourceInfo_[sourceIndex].bands; const wantedSamples = this.sourceInfo_[sourceIndex].bands;
samplesPerPixel[sourceIndex] = wantedSamples samplesPerPixel[sourceIndex] = wantedSamples
@@ -607,7 +606,7 @@ class GeoTIFFSource extends DataTile {
const bands = this.sourceInfo_[sourceIndex].bands; const bands = this.sourceInfo_[sourceIndex].bands;
if (bands) { if (bands) {
for (let i = 0; i < bands.length; ++i) { for (let i = 0; i < bands.length; ++i) {
if (!isNaN(values[bands[i] - 1])) { if (values[bands[i] - 1] !== null) {
this.addAlpha_ = true; this.addAlpha_ = true;
break outer; break outer;
} }
@@ -617,7 +616,7 @@ class GeoTIFFSource extends DataTile {
// option 3: check image metadata for all bands // option 3: check image metadata for all bands
for (let imageIndex = 0; imageIndex < values.length; ++imageIndex) { for (let imageIndex = 0; imageIndex < values.length; ++imageIndex) {
if (!isNaN(values[imageIndex])) { if (values[imageIndex] !== null) {
this.addAlpha_ = true; this.addAlpha_ = true;
break outer; break outer;
} }
@@ -681,7 +680,7 @@ class GeoTIFFSource extends DataTile {
/** @type {number|Array<number>} */ /** @type {number|Array<number>} */
let fillValue; let fillValue;
if (!isNaN(source.nodata)) { if ('nodata' in source && source.nodata !== null) {
fillValue = source.nodata; fillValue = source.nodata;
} else { } else {
if (!samples) { if (!samples) {
@@ -778,7 +777,11 @@ class GeoTIFFSource extends DataTile {
nodata = nodataValues[sourceIndex][bandIndex]; nodata = nodataValues[sourceIndex][bandIndex];
} }
if (sourceValue !== nodata) { const nodataIsNaN = isNaN(nodata);
if (
(!nodataIsNaN && sourceValue !== nodata) ||
(nodataIsNaN && !isNaN(sourceValue))
) {
transparent = false; transparent = false;
data[dataIndex] = value; data[dataIndex] = value;
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

View File

@@ -0,0 +1,21 @@
import GeoTIFF from '../../../../src/ol/source/GeoTIFF.js';
import Map from '../../../../src/ol/Map.js';
import TileLayer from '../../../../src/ol/layer/WebGLTile.js';
const source = new GeoTIFF({
sources: [{url: '/data/raster/elevation-f32.tif', nodata: NaN}],
});
new Map({
layers: [
new TileLayer({
source: source,
}),
],
target: 'map',
view: source.getView(),
});
render({
message: 'normalize i16 data with nan nodata based on GDAL stats',
});

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

View File

@@ -0,0 +1,21 @@
import GeoTIFF from '../../../../src/ol/source/GeoTIFF.js';
import Map from '../../../../src/ol/Map.js';
import TileLayer from '../../../../src/ol/layer/WebGLTile.js';
const source = new GeoTIFF({
sources: [{url: '/data/raster/elevation-f32.tif'}],
});
new Map({
layers: [
new TileLayer({
source: source,
}),
],
target: 'map',
view: source.getView(),
});
render({
message: 'normalize i16 data with nan nodata based on GDAL stats',
});

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

View File

@@ -0,0 +1,21 @@
import GeoTIFF from '../../../../src/ol/source/GeoTIFF.js';
import Map from '../../../../src/ol/Map.js';
import TileLayer from '../../../../src/ol/layer/WebGLTile.js';
const source = new GeoTIFF({
sources: [{url: '/data/raster/elevation-i16.tif'}],
});
new Map({
layers: [
new TileLayer({
source: source,
}),
],
target: 'map',
view: source.getView(),
});
render({
message: 'normalize i16 data with -9999 nodata based on GDAL stats',
});

Binary file not shown.

Binary file not shown.