Turns out Rust really might take over everything.

Project date Geospatial engineering · Raster tiling

I never really planned on becoming a Rust evangelist, yet here we are.

Some names first. When you upload imagery to Sarkoa, a processor converts it into a Cloud Optimized GeoTIFF (a COG: a big image file laid out so small pieces can be read on their own). Later, when someone opens the map, a tiler reads pieces of that COG and draws the small square images (tiles) the browser shows. The pieces inside a COG are compressed, and the code that squeezes and un-squeezes an image is called a codec.

A little while back I ran into an embarrassing bug: the default raster format coming out of my own upload processor was the exact format my tile server couldn’t decode. Within a week I’d replaced the entire TypeScript raster tiler with Rust compiled to WebAssembly (WASM), which is a way to run code written in a language like Rust in places that normally only run JavaScript. Day one wrapped libwebp; day two I ripped that out for a pure-Rust WebP decoder; day three the rest of the codecs went in; by the end of the week the old TypeScript tiler was dead.

One survey crop, one rendered map tile
USGS orthomosaicA white square on a wider aerial survey marks the patch containing two diagonal tracks.102.4 m survey crop · tile outlined
Rust renderer outputThe same two tracks cross the vegetation in the lossless output tile.512 × 512 pixels · lossless WebP
The white outline marks the footprint of zoom 19, column 152547, row 198669. Next to it is the 484,878-byte lossless WebP tile served by the Rust tiler from the August 5, 2025 aerial survey of Clearview Landfill in Pennsylvania. Download the original tile (WebP) · USGS survey · Explore the map.

How my own defaults broke my tiler

The old TypeScript tiler used a library called geotiff.js to read COGs. WebP is an image compression format, like JPEG but smaller. Whenever geotiff.js hits a WebP-compressed piece (GDAL compression code 50001), it doesn’t decode it itself. It asks the web browser to do it, through a browser feature called createImageBitmap. Problem is, my tiler doesn’t run in a browser. It runs on Cloudflare Workers, whose engine (workerd) is not Chrome - createImageBitmap doesn’t exist there, so decoding crashes immediately with Cannot decode WebImage as createImageBitmap is not available.

You can trigger the exact crash with this tiny 1.7 KB test COG: geotiff.js 2.1.3 dies before producing a single pixel, while the Rust tiler decodes it cleanly (pixel (40, 3) comes back as RGBA (80, 6, 43, 255) with zero-alpha masking intact) and handles the full aerial survey above without blinking.

This wasn’t some rare edge case either. My processor writes WebP COGs by default for normal 3- and 4-band imagery because lossy WebP is 25–34% smaller than comparable JPEG. In other words, almost every standard raster uploaded to Sarkoa was broken out of the box.

That left three choices:

  1. Stop writing WebP COGs. Easy fix, except now I’m paying to store 30% bigger files forever just to babysit a JS library.
  2. Polyfill createImageBitmap inside the Worker. Putting a hacked-up fake browser API right on my hottest code path sounded like a future nightmare.
  3. Write a reader that doesn’t depend on browser APIs at all.

Option 3 was the only one that didn’t feel like sweeping the problem under the rug.

Why Rust (and why pure Rust)

Cloudflare Workers run WebAssembly nicely, and Rust already has solid, pure-Rust image codecs. “Pure Rust” ended up being the key part. A crate is a Rust library. Some crates are thin wrappers around older libraries written in C (like libwebp-sys), and those drag in a C-to-WASM cross-compiler setup I really didn’t want to maintain. Pure-Rust crates just compile to WASM like normal code.

The whole tiler comes down to four steps:

  1. Parse the TIFF’s table of contents directly (its IFDs, or image file directories - no geotiff.js at all).
  2. Fetch only the blocks we need from R2 storage, with range requests (“send me bytes X to Y”).
  3. Decode them with pure-Rust crates: image-webp, flate2 (Rust backend), weezl (LZW), and jpeg-decoder.
  4. Encode the output tile as lossless WebP (VP8L).

Why lossless output? Lossy compression throws away detail you probably won’t notice to make a file smaller. Lossless compression gives back exactly the pixels that went in. The old tiler spit out lossy WebP at quality 70. On aerial photography you barely notice, but on a classified raster where exact RGB values map to legend categories, lossy compression subtly nudges pixel colours until they don’t match the legend anymore. Lossless tiles cost a bit more bandwidth, but every pixel that reaches the browser is bit-for-bit what the tiler rendered - alpha channel included.

A synthetic raster with flat colours and areas of partial transparency used to test exact pixel values.
This synthetic regression tile checks a property that's hard to verify by eye in aerial imagery. After decoding, the orange pixel at (128, 0) reads RGBA (200, 120, 40, 255), the black pixel at (128, 192) retains alpha 128, and all 18 recorded sample pixels match.

Pan around the Clearview survey above - every tile you see is coming straight out of that Rust Worker. Open the full map.

All posts