Skip to content

fix(geotiff): decode NaN pixels in LERC tiles as NaN - #790

Merged
kylebarron merged 3 commits into
mainfrom
kyle/lerc-nan-mask
Nov 3, 2026
Merged

kylebarron merged 3 commits into
mainfrom
kyle/lerc-nan-mask

Conversation

@kylebarron

Copy link
Copy Markdown
Member

Note

This PR was written by Claude (Claude Code), not by @kylebarron.

LERC stores NaN as an invalid pixel in its mask rather than as a value. lerc.ts returned the decoded pixels and dropped the mask, and the lerc package leaves invalid pixels at 0, so NaN pixels in float LERC tiles read as 0. libtiff fills invalid float pixels with NaN (tif_lerc.c), so GDAL reads NaN there. lerc.ts now sets those pixels to NaN.

Changes

  • For F32 and F64 LERC data, lerc.ts sets every pixel the LERC mask marks invalid to NaN, as libtiff does.
  • When a blob has one mask per band (bandMasks), each band uses its own mask. GDAL writes that when a pixel-interleaved pixel is NaN in only some samples. The combined mask would mark the pixel invalid in every band.
  • When one mask covers several values per pixel (depthCount > 1), every value of an invalid pixel becomes NaN.
  • This runs before decode() swaps samples from big endian files, as in libtiff, so big endian files match GDAL too. That includes the values GDAL loses in such files (GDAL loses some float values in big endian LERC files #707), which now read as the same byte-swapped NaN.
  • Integer data is unchanged. libtiff leaves invalid integer pixels at 0 too.

Tests

  • Six new tests in decode.test.ts decode tiny real LERC blobs embedded as base64: 4x4 strips written by GDAL 3.12.1 with COMPRESS=LERC and MAX_Z_ERROR=0. They cover float32 and float64, one mask shared by two samples, one mask per band, an all-NaN tile, and the big endian ordering. All six fail on main.
  • Existing LERC tests, including the rasterio integration fixtures, still pass.
  • Checked end to end against GDAL with the issue's 64x64 files (35 NaN pixels in tile (0, 0)), through GeoTIFF.fromArrayBuffer().fetchTile(): main differs from GDAL in 35 pixels, this branch in none, for little endian, big endian and 3-band files.

Notes

  • Pixel-interleaved multi-sample LERC tiles with one mask (or none) still decode to a single depth-major array instead of one array per band. That bug predates this change, and toPixelInterleaved throws Band count mismatch on them. Their NaN values are now correct; splitting them into bands is a separate fix.

Closes #706

🤖 Written by Claude Code

LERC stores NaN as an invalid pixel in its mask rather than as a value.
`lerc.ts` returned the decoded pixels and dropped the mask, and the
`lerc` package leaves invalid pixels at 0, so NaN pixels read as 0.
libtiff fills invalid float pixels with NaN, so GDAL reads NaN there.

For F32 and F64 data, set the pixels the mask marks invalid to NaN.
Use each band's own mask when the blob has one per band (GDAL writes
that when a pixel-interleaved pixel is NaN in only some samples), and
set every depth value of an invalid pixel. This runs before `decode()`
swaps samples from big endian files, as in libtiff, so big endian files
read the same as in GDAL too. Integer data is unchanged, as in libtiff,
which leaves invalid integer pixels at 0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the fix label Oct 23, 2026
@kylebarron kylebarron added this to the 0.10 milestone Oct 30, 2026

@kylebarron kylebarron left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅

@kylebarron
kylebarron enabled auto-merge (squash) November 3, 2026 00:00
@kylebarron
kylebarron disabled auto-merge November 3, 2026 00:01
@kylebarron
kylebarron merged commit e22c876 into main Nov 3, 2026
3 checks passed
@kylebarron
kylebarron deleted the kyle/lerc-nan-mask branch November 3, 2026 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NaN pixels in LERC tiles decode as 0

1 participant