Skip to content

fix(data): say when raster values are rounded to 32-bit floats - #1286

Merged
Mavrick-F merged 9 commits into
opengeos:mainfrom
Mavrick-F:fix/raster-precision-notice
Nov 2, 2026
Merged

Mavrick-F merged 9 commits into
opengeos:mainfrom
Mavrick-F:fix/raster-precision-notice

Conversation

@Mavrick-F

@Mavrick-F Mavrick-F commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

Summary

RustGIS holds every raster band as 32-bit floats (f32). Whole numbers are exact only up to 16,777,216 (2^24), and decimals to about 7 significant digits. An Int32 or UInt32 band past that, Int64, or Float64 with more digits changes as it's read, and Export Raster writes the changed values. Until now nothing said so. Under the Never silent rule, this PR makes RustGIS say it.

On main at c0bc83c (the raster readers and share.exportRaster are unchanged up to a7e58ba), a 2×2 Int32 GeoTIFF holding 16777217, 16777219, 100000001, 7:

rustgis-cli run --cmd 'layer.addData={"path":"int32.tif","name":"x"}' \
  --cmd 'share.exportRaster={"layer":"x","path":"out.tif"}'
Input Value in Value out (Float32)
Int32 16777217 16777216
Int32 100000001 100000000
Float64 0.123456789012345 0.12345679104328156
Float64 1234567.891234 1234567.875
ASCII grid 1234567.891 1234567.875

The only notice was Exported out.tif. This matters for ID rasters (parcel or zone IDs above 16.7 million become other IDs), counts and high-precision elevation.

This PR doesn't widen the pixel type. That's a data-model change (memory, every tool, the renderer), so it's for maintainers to weigh as a follow-up. This PR makes the loss visible:

  • Counted while reading. Readers count the values that actually changed: a value whose f32 isn't the file's value. The count lives on Raster::rounded, by band. NaN and no-data samples aren't counted, by one rule for every reader (Uncounted). The no-data value is the file's when finite, else the sidecar's (.aux.xml, read first) when finite. Values are compared exactly as the file states them (a whole number stays whole past 2^53). Only a sample whose value in the file is that value is left out, even past f32's range (a Float64 no-data of −1.8e308). Any other sample is_valid masks is counted, even one f32 holds exactly. A valid sample that rounds onto the no-data value (an Int32 ID 16777217 with no-data 16777216) is counted, because it went from a value to no-data; the notice gives one count and doesn't tell such samples apart. Overflows to ±inf are counted.
  • Said on import. add_raster, where every opened raster passes, gives one notice per layer: "ids: 3 values were rounded as the file was read: pixels are held as 32-bit floats, exact for whole numbers up to 16,777,216 and to about 7 significant digits, so an export writes the rounded values". A multiband raster lists them by band: "8 values (band 1: 3, band 2: 5) were".
  • Said on export. share.exportRaster adds the count for the bands it writes, as each writer reports them (geotiff::written_bands, cogwrite::written_bands): "Exported out.tif; 3 values in it were rounded to 32-bit floats when ids was read". The mark is on the Raster, not a saved flag. A project that reopens re-reads the file and counts again, so the export note still appears after a reopen, where the import notice isn't given again. Tool outputs are new rasters, so they carry no mark. Georeferencing keeps the same pixels, so it keeps the mark.

What's checked:

Source How
GeoTIFF, read whole (uploads, the web app's parts reader, small files) Int32, UInt32, Int64, UInt64 and Float64 samples, by band (interleaved or stored apart). Other types skip the check.
GeoTIFF, read a chunk at a time (large files on several threads, overviews made while reading) Counted as each kept pixel is placed, so an overview made while reading counts only the pixels it keeps.
Rasters GDAL reads Through the GeoTIFF it converts to.
ASCII grid A value counts when its f32 is more than half a unit of its last written digit away: 123.4567 is held, 1234567.891 and 16777217 aren't. Overflow and underflow count too (1e39, 1e400, 1e-400); written infinities and zeros don't. The check is one pass over the token, with a whole-number comparison up to 15 digits.
Zarr (desktop) Int32/Int64/UInt64 past 2^24 (64-bit ones compared as integers, beyond 2^53 too) and Float64 digits. With scale_factor/add_offset, a packed whole number counts when unscaling its f32 doesn't give back its exact value, and a packed Float64 (or anything scaled by 0) when f32 doesn't hold the scaled value. Float32 or narrower storage states no more precision than f32, so scaling it is counted only when it leaves f32's range. Missing values become NaN before conversion; like the other readers, Zarr leaves out exact matches only. Ordinary packed data (0.01 steps on 273.15) counts nothing.

Cost: only Int32, UInt32, Int64, UInt64 and Float64 are checked; other types skip it. A large GeoTIFF is decoded a chunk at a time on several threads. Each thread marks a chunk's changed samples in a bitset, outside the shared lock, and placement counts the marked pixels it keeps. Opening a 7000×7000 file, best of 5–7 runs, same build profile, against main:

File main this PR
GeoTIFF Float32 (tiled) 1.14 s 1.17 s
GeoTIFF Float64 (tiled) 1.63 s 1.77 s
ASCII grid, 123.45 2.66 s 3.10 s
ASCII grid, 123.45678 3.12 s 4.54 s

Not covered, left for follow-ups:

  • A slice of a Zarr store the web app reads itself reaches the engine as a Float32 GeoTIFF, so its count is lost on the way. Carrying it would need a new layer.addZarr parameter.
  • A cloud-optimised GeoTIFF streamed tile by tile for display isn't read into a Raster, so it isn't counted. Added with "whole": true for tools, it goes through the GeoTIFF reader above.
  • The web app's Export Raster download doesn't give the export note. The import notice still applies.
  • Older than this PR: when an RGB raster's colour bands fit in bytes but a fourth band doesn't, the GeoTIFF export drops that band. The export note matches what's written, so it doesn't count rounded values there.
  • Whole numbers beyond ±2^53, from review:
    • A GeoTIFF Int64/UInt64 no-data value beyond ±2^53 rounds to f32 through f64, which can differ from the sample's direct f32 rounding, so the exact no-data sample can be counted. held_exactly should hold an exact match before the f32 mask check.
    • Missing inner chunks of an Int64 Zarr shard get the fill value as an f64, which rounds beyond ±2^53, so those cells don't match _FillValue. unshard should take the exact fill.
    • Exact::parse reads whole numbers beyond i128 (past ±1.7 × 10^38) as f64, so two such tokens can compare equal.
  • Clipping and editing pixels build a new raster, so, like tool outputs, they drop the count, and an export of the result gives no note. Carrying the whole raster's count would overcount a clip; counting again needs the file's values.
  • Keeping the file's type on export (Int32 stays Int32) or holding wider pixels. Both are pixel-type changes that need maintainer agreement.

The data formats page now says how pixels are held and that the notices appear.

Testing

  • cargo xtask ci: passed (3,178 tests, 20 ignored)
  • cargo build --locked --target wasm32-unknown-unknown -p rustgis-web: passed
  • pre-commit run --all-files: passed
  • New raster::geotiff::tests::values_f32_cannot_hold_are_counted: Int32 past 2^24 (2 of 4 counted, 16777216 itself held), a clean Int32, Float64 with a no-data value past f32's range and NaN (1 counted), exact Float64 (none), Int64 2^60 + 1, a sidecar no-data value taken off the count, and a 3-band UInt32 counted by band.
  • New raster::geotiff::tests::chunks_decoded_on_several_threads_count_rounded_values: 64 strips read on several threads count as one thread does.
  • New raster::geotiff::tests::a_reduced_read_counts_only_the_pixels_it_keeps: a 4×4 Int32 read at every second pixel counts only the 1 of 4 large values it keeps.
  • New raster::ascii::tests::values_f32_cannot_hold_are_counted: 7-digit decimals that write back the same, 2^24, the no-data value and zero runs aren't counted. 2^24 + 1, too many digits, overflow and a subnormal are.
  • New raster::zarr::tests::values_f32_cannot_hold_are_counted: Int64, UInt64 (past 2^53 too) and Float64 variables, lossy and clean, and scaled variables both ways.
  • New raster::{geotiff,ascii,zarr} tests no_data_*_are_left_out_of_the_count_by_one_rule: one table of no-data cases through all three readers.
  • New raster::cogwrite::tests::the_bands_said_written_are_the_bands_written: both writers' band lists match what they write.
  • New engine test rounded_raster_values_are_said_on_import_and_export: an ASCII grid with 3 rounded values gives the import notice and the export note, and the exported file holds 16777216. A clean grid gives neither.
  • CLI after, on the files above: the Int32 file gives "x: 3 values were rounded as the file was read: …" and "Exported out.tif; 3 values in it were rounded to 32-bit floats when int32 was read". Float64 and the ASCII grid give 2 each (12.3 is held). Clean Int32 and ASCII files say nothing, and a 49-million-pixel Float64 file read on several threads counts every value.

Related issue

None filed: this PR carries the report. The Never silent rule is in AGENTS.md.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Raster imports now report how many pixel values lose precision when stored as 32-bit floats, with counts by band for multiband rasters.
    • Raster exports report how many values are rounded in the written output.
    • Rounding counts exclude missing values and values preserved at the supported precision.
    • Exported color and alpha bands follow the raster format and band configuration.
  • Documentation
    • Updated raster format guidance to explain 32-bit float precision limits and when rounding notices appear.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant