Skip to content

Commit b152725

Browse files
committed
Replace NaN values
1 parent a9bf5b9 commit b152725

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎src/tile.js‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Tile {
1515
coordinates,
1616
bands,
1717
initializeBuffer,
18+
needsNaNPreprocessing = false,
19+
fillValue = null,
1820
}) {
1921
this.key = key
2022
this.tileCoordinates = keyToTile(key)
@@ -31,6 +33,9 @@ class Tile {
3133
this._loading = {}
3234
this._ready = {}
3335

36+
this.needsNaNPreprocessing = needsNaNPreprocessing
37+
this.fillValue = fillValue
38+
3439
bands.forEach((k) => {
3540
this._buffers[k] = initializeBuffer()
3641
})
@@ -133,7 +138,26 @@ class Tile {
133138
`Unexpected data dimensions for band: ${band}. Found ${bandData.dimension}, expected 2. Check the selector value.`
134139
)
135140
}
136-
this._buffers[band](bandData)
141+
142+
if (this.needsNaNPreprocessing && bandData.data) {
143+
const processedData = new Float32Array(bandData.data)
144+
let hasNaN = false
145+
for (let i = 0; i < processedData.length; i++) {
146+
if (isNaN(processedData[i])) {
147+
processedData[i] = this.fillValue
148+
hasNaN = true
149+
}
150+
}
151+
if (hasNaN) {
152+
const processedBandData = bandData.pick()
153+
processedBandData.data = processedData
154+
this._buffers[band](processedBandData)
155+
} else {
156+
this._buffers[band](bandData)
157+
}
158+
} else {
159+
this._buffers[band](bandData)
160+
}
137161
})
138162

139163
this._bufferCache = getSelectorHash(selector)

‎src/tiles.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@ export const createTiles = (regl, opts) => {
161161
this.dimensions = dimensions
162162
this.shape = shape
163163
this.chunks = chunks
164-
this.fillValue = fillValue ?? fill_value ?? DEFAULT_FILL_VALUES[dtype]
164+
const originalFillValue =
165+
fillValue ?? fill_value ?? DEFAULT_FILL_VALUES[dtype]
166+
this.fillValue = isNaN(originalFillValue)
167+
? DEFAULT_FILL_VALUES['<f4']
168+
: originalFillValue
169+
this.needsNaNPreprocessing = isNaN(originalFillValue)
165170

166171
if (mode === 'texture') {
167172
const emptyTexture = ndarray(
@@ -334,6 +339,8 @@ export const createTiles = (regl, opts) => {
334339
coordinates: this.coordinates,
335340
bands: this.bands,
336341
initializeBuffer: initialize,
342+
needsNaNPreprocessing: this.needsNaNPreprocessing,
343+
fillValue: this.fillValue,
337344
})
338345
}
339346

0 commit comments

Comments
 (0)