Repository navigation
fix(data): say when raster values are rounded to 32-bit floats - #1286
Merged
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.exportRasterare unchanged up to a7e58ba), a 2×2 Int32 GeoTIFF holding16777217, 16777219, 100000001, 7: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:
f32isn't the file's value. The count lives onRaster::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 pastf32's range (a Float64 no-data of −1.8e308). Any other sampleis_validmasks is counted, even onef32holds 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.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".share.exportRasteradds 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 theRaster, 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:
f32is more than half a unit of its last written digit away:123.4567is held,1234567.891and16777217aren'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.scale_factor/add_offset, a packed whole number counts when unscaling itsf32doesn't give back its exact value, and a packed Float64 (or anything scaled by 0) whenf32doesn't hold the scaled value. Float32 or narrower storage states no more precision thanf32, so scaling it is counted only when it leavesf32'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:
123.45123.45678Not covered, left for follow-ups:
layer.addZarrparameter.Raster, so it isn't counted. Added with"whole": truefor tools, it goes through the GeoTIFF reader above.f32throughf64, which can differ from the sample's directf32rounding, so the exact no-data sample can be counted.held_exactlyshould hold an exact match before thef32mask check.f64, which rounds beyond ±2^53, so those cells don't match_FillValue.unshardshould take the exact fill.Exact::parsereads whole numbers beyond i128 (past ±1.7 × 10^38) asf64, so two such tokens can compare equal.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: passedpre-commit run --all-files: passedraster::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 pastf32'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.raster::geotiff::tests::chunks_decoded_on_several_threads_count_rounded_values: 64 strips read on several threads count as one thread does.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.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.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.raster::{geotiff,ascii,zarr}testsno_data_*_are_left_out_of_the_count_by_one_rule: one table of no-data cases through all three readers.raster::cogwrite::tests::the_bands_said_written_are_the_bands_written: both writers' band lists match what they write.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.12.3is 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