Skip to content

[Feature]: Evaluate Terrio as a COG writer dependency #663

Open

Description

@deniial00

Before you start

  • I searched the existing issues and the roadmap and did not find a duplicate request.

What problem does this solve?

In #629, @giswqs asked to keep Terrio out of the native COG writer PR and to experiment with it separately once Terrio supported narrower Cargo features. opengeos/terrio#154 added those features, so I evaluated Terrio's GeoTIFF/COG path without changing RustGIS's production writer or I/O.

Evidence of experiment branch: deniial00/RustGIS@terrio-cog-evaluation, pinned to commit ee149b8 on that branch:

The experiment is a standalone Cargo workspace with its own lockfile. It is not a RustGIS workspace member, so normal RustGIS builds do not resolve or compile Terrio. Terrio is pinned to ce3c4463f19864284d743720123c4d022784c497 with default-features = false, features = ["geotiff"].

Findings

Recommendation: keep the native writer for now. Terrio's minimal build works on native and wasm32-unknown-unknown, and its files pass GDAL's COG validator. But it does not produce equivalent overview pixels for several RustGIS export cases.

Both writers received identical inputs and settings: EPSG:32617, a north-up 30 m grid, 512×512 tiles, pixel interleaving, DEFLATE level 6, predictor 1 and automatic overviews.

Case Native Terrio Finding
RGB Pass Pass Exact pixels and overviews
Odd dimensions Pass Pass Edge dimensions and pixels match
BigTIFF Pass Pass 64-bit TIFF header and COG layout
Float32 + no-data Pass Fail 2,501 wrong second-level overview pixels
UInt16 + no-data Pass Fail 2,585 wrong second-level overview pixels: 2,501 no-data weighting errors (up to 2,977) plus 84 off-by-one rounding differences
Palette Pass Fail Overviews invent class indexes
Classes Pass Fail Overviews invent classes (9 where class 19 was expected)
Grey + alpha Pass Fail Transparent pixels darken colour overviews
RGBA Pass Fail Expected (200,100,50), got (100,50,25)

All 18 desktop outputs pass GDAL's full COG validator with no warnings or errors. Full-resolution pixels are identical between the two writers in every case, and georeferencing, EPSG, no-data values, palettes, UInt16 storage, alpha bands, odd dimensions and BigTIFF are all preserved. The failures are only in the overviews, which a viewer uses when zoomed out. The actual wasm exports show the same six failures.

The quickest way to see it is the classes case, whose first overview should only contain 1, 7 and 19 (run from a checkout of the branch above):

cargo run --release --manifest-path experiments/terrio-cog/Cargo.toml --features terrio -- export terrio classes 1025 769 terrio-classes.tif
gdal_translate -ovr 0 terrio-classes.tif terrio-classes-ov0.tif
gdalinfo -hist terrio-classes-ov0.tif   # also contains 4, 9, 10, 12, 13, ...

Main causes in the pinned Terrio revision:

  • COG export ignores overview_resampling, so a request for nearest overviews on classes still averages.
  • Colour overviews are not weighted by alpha.
  • Recursive overview averaging loses the original valid-pixel weights.

Cost measurements

These are small export programs, not the full RustGIS app or web bundle. Tests ran on an Apple M1 Pro with rustc 1.98.1 and GDAL 3.13.3.

Measurement Native With Terrio
Desktop clean build 136.8 s 239.3 s
Stripped desktop executable 1.58 MiB 8.13 MiB
Wasm clean build 104.1 s 194.8 s
Wasm, gzip -9 0.301 MiB 1.365 MiB
Wasm, Brotli 11 0.221 MiB 1.092 MiB

Desktop export, 4096×4096, median of 3 fresh processes; the times include writing and syncing the file:

Input Writer Export ms Peak memory MiB Output MiB
RGB Native 1254.8 202.3 21.88
RGB Terrio 326.5 323.1 20.64
Float32 + no-data Native 411.8 74.4 1.55
Float32 + no-data Terrio 84.8 194.8 3.01

Terrio is faster for these inputs but uses more peak memory. Its large Float32 output also fails the overview checks, so that speed is not for an equivalent file.

Proposed solution

Do not replace or add Terrio in the production writer yet. Keep the experiment as reproducible evidence and file the upstream Terrio issue(s) later. Re-evaluate after Terrio provides:

  1. COG overview resampling that honours nearest selection for categorical rasters.
  2. Alpha-weighted averaging for colour overviews.
  3. Valid-pixel-aware overviews, either by preserving weights recursively or by building from the full-resolution image.
  4. A public COG writer with bounded memory, for borrowed samples and a Write destination, on native and wasm.
  5. Optional JPEG XL/WebP and other unrelated codecs in the wbraster/wbgeotiff dependency chain.

After those fixes, rerun the same comparison before considering a production integration.

Estimated scope

Medium: a broader feature that is feasible in a reasonable timeframe

Alternatives considered

  • Keep RustGIS's native writer: no new dependency, and it passes this comparison.
  • Adopt Terrio now: rejected, because it gives different overview results for classes, alpha, and no-data rasters.
  • Patch Terrio's behaviour in an adapter: rejected, because precomputed overviews or local fixes would hide upstream gaps and duplicate writer logic.

Activity

  1. giswqs commented on Oct 21, 2026

    @giswqs
    Member

    @deniial00 Thank you very much for the thorough testing. This is very helpful. Let's keep experimenting and fixing things in terrio and revisit this later when terrio is more mature.

  2. deniial00 commented on Oct 22, 2026

    @deniial00
    MemberAuthor

    Update: the Terrio fixes are merged; parity now holds, and the application sizes are measured

    The overview problems reported in opengeos/terrio#158 were fixed upstream and merged: the overview fix as opengeos/terrio#165 and a row-averaging speed-up as opengeos/terrio#168. I re-ran the same comparison against the merged code and measured the real applications. Everything is on the fork branch, pinned to f53f0b4: parity and speed report, application sizes, integration patch.

    The tested commit was squash-merged, so it is not an ancestor of Terrio main, but drivers/gtiff_write.rs on main is byte-identical to the tested one and nothing has touched it since. main also has unrelated commits, so I did not measure its exact sizes.

    Parity with the native writer

    Same nine inputs and settings as before (EPSG:32617, 512 px tiles, Deflate 6, automatic overviews). Every sample of every overview is compared with an independent oracle, and each file is checked with GDAL's COG validator.

    Case Before (Terrio ce3c4463) Now, desktop Now, wasm
    RGB, odd dimensions, BigTIFF Pass Pass Pass
    Float32 + no-data Fail Pass Pass
    UInt16 + no-data Fail Pass Pass
    Palette Fail Pass Pass
    Classes Fail Pass Pass
    Grey + alpha Fail Pass Pass
    RGBA Fail Pass Pass

    All six earlier failures are fixed, and the 4096×4096 benchmark outputs pass too. One difference remains, in a supplemental 1027×771 test: the class and palette overviews differ from the native writer in 449 edge pixels. Both pick a real class, not an invented one. They differ in which pixel they choose in a partly filled edge block: the native writer takes the centre of the clipped block, Terrio the centre of the full-size block clamped to the image edge. That is a convention choice.

    Correction

    Earlier I wrote (in opengeos/terrio#158, now edited, and in my link comment here) that GDAL's NEAREST takes the centre pixel of a block. That was wrong. I checked GDAL 3.13.3 directly, and it starts from the top-left pixel (0,0). So "centre pixel" is RustGIS's own convention, not GDAL's, and the edge difference above should not be read as non-compliance.

    Speed and memory (4096×4096, median of 3 fresh processes, Apple M1 Pro)

    Input Writer Export Peak memory File
    RGB native 1183 ms 202 MiB 21.88 MiB
    RGB Terrio 262 ms 319 MiB 20.64 MiB
    Float32 + no-data native 405 ms 74 MiB 1.55 MiB
    Float32 + no-data Terrio 94 ms 192 MiB 3.17 MiB

    Terrio is about 4.5× and 4.3× faster here, but uses 1.6× and 2.6× the peak memory, and its Float32 file is about 2× larger. In the same run, the merged speed-up made Terrio 3.1× (RGB) and 2.7× (Float32) faster than the earlier fix. This is synthetic data, not a representative corpus.

    Actual application size

    Terrio's COG writer, with default-features = false, features = ["geotiff"], was wired into the real share.exportRaster COG path in a snapshot of this repo, then the apps were built with the release settings (the macOS app and DMG through packaging/package.sh, the web app through Trunk). One clean build per variant, macOS arm64.

    Without Terrio With Terrio replacing the native writer Change
    rustgis binary 66.78 MiB 74.36 MiB +7.58 MiB (+11.4%)
    .dmg download 28.73 MiB 32.05 MiB +3.32 MiB (+11.5%)
    rustgis-cli 41.96 MiB 49.09 MiB +7.13 MiB (+17.0%)
    web wasm, gzip 11.38 MiB 12.50 MiB +1.12 MiB (+9.8%)
    web wasm, Brotli 7.45 MiB 8.30 MiB +0.86 MiB (+11.5%)
    clean desktop build 371 s 461 s +91 s (+24.5%)
    clean web build 421 s 452 s +32 s (+7.5%)

    Keeping the native writer alongside Terrio adds only about 34 KB more, so the cost comes from Terrio's dependencies, not the writer. The shipped web app contains no Terrio today, so the web figures are a prospective integration: the web UI does not currently request a COG, so this code would not be reachable there unless a COG option is added. All 32 real exports through each variant's CLI passed the same checks, and both the desktop and web apps started and ran.

    Open points

    For whoever picks this up, the facts that remain:

    • Edge convention. The native writer and Terrio choose different pixels in partly filled edge blocks of categorical rasters (the 449-pixel difference above). Either could be considered correct.
    • Cost. About +12% on the download and the binaries, and 1.6–2.6× the peak memory, for roughly 4× the export speed on these inputs.
    • Memory. Terrio's COG writer still needs the whole image in memory; there is no public streaming COG writer.

    I'll leave it to the maintainers to decide how, or whether, to continue. I'm happy to help with whichever direction you prefer, and to re-run the comparison on request.

  3. added
    enhancement
    on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions