Lay of the Land
All methods

Where water goes

Each terrain build also works out how water moves across the surface, with WhiteboxTools. The code is in services/gis-worker/gis_worker/terrain/hydrology.py. Thresholds here are judgments for an engineer to review, not published standards.

Two surfaces

Water can't be routed over a raw DEM, because every small dip traps it. So a second surface is made by cutting through what blocks flow, then filling what's left. That changes elevations, so it's wrong for anything else:

  • The original surface is the only one used for elevations, contours, profiles, slope and, later, tile depths and cover.
  • The conditioned surface is stored as its own file, and is only for working out where water goes.

Code can't mix them up. In the worker, ConditionedDem isn't a FieldDem, and anything needing real ground raises an error if given one. In the browser, DemReader<"conditioned"> won't compile where DemReader<"original"> is needed, and tests fail if that ever changes. "Conditioning changes" on the map shows what was altered.

Cutting first changed about half as many cells as filling alone on real ground at Moorhead, 13.9% against 24.3%. Cuts can reach up to 100 m, to get past a road.

WhiteboxTools 2.4.0's cutting tool can leave a low spot neither cut nor filled: one whose cheapest way out is past the grid's side, and one whose only way out is the grid's side or a cell with no height. Water in it then has nowhere to go, and the rain comparison kept its overflow as standing water. So from pipeline version 7, where any cell away from the terrain's edge is left with no direction, the cut surface is filled the way the fill depth is, and that fill is kept only in the stretches that hold such a cell. Ground the cut already drained gets no second pass and comes out exactly as before. surface-water.md, section 7, "What the cases found", fault 1, has the cases, the tool's code behind it and what it changes.

Flats

Some sources round their heights, so neighboring cells often match exactly. USGS's ND_3DEPProcessing_D22 DEM does: a tenth of the cells in a 160-acre field at Fargo sit on such flats, one of them about 15,000 cells, against under 0.5% in USGS's DEMs at Moorhead and Mankato. The least-cost search behind the cuts floods flats, and this was the slowest step of a whole build, by far.

So before cutting, cells on flats are raised by up to 0.5 mm, far below what lidar can measure. The amounts are random but seeded, so the same ground always gives the same answer. Only the conditioned surface gets them. Depths, elevations and everything else use the original.

Measured on real 160-acre fields, comparing water paths draining at least 1,000 m² with those from the original method:

Field Method Time Same cells Within 3 m
Fargo Original, 100 m cuts 28.7 s 1.00 1.00
Fargo Shorter, 50 m cuts 10.3 s 0.68 0.90
Fargo Flats raised up to 0.5 mm 0.7 s 0.84 0.95
Fargo Everything 1 cm of noise 0.3 s 0.39 0.86
Fargo 2 Original, 100 m cuts 26.1 s 1.00 1.00
Fargo 2 Shorter, 50 m cuts 6.0 s 0.65 0.89
Fargo 2 Flats raised up to 0.5 mm 0.6 s 0.91 0.98
Fargo 2 Everything 1 cm of noise 0.3 s 0.44 0.88
Moorhead Original, 100 m cuts 0.3 s 1.00 1.00
Moorhead Flats raised up to 0.5 mm 0.3 s 1.00 1.00
Mankato Original, 100 m cuts 0.3 s 1.00 1.00
Mankato Flats raised up to 0.5 mm 0.3 s 1.00 1.00

The noise rows are for scale: on very flat ground, 1 cm of noise moves individual paths more than this method does. The original method isn't a true answer either, only the one being compared with. Where a path runs on such flat ground, don't read its exact line too closely.

The same ground gives the same answer

WhiteboxTools' BreachDepressionsLeastCost isn't repeatable when it runs on several threads. Five runs on identical ground at Fargo gave five different surfaces. Only 0.03% of cells differed by over 1 mm between the worst pair, but by up to 3.6 m in places, and 2% of water path cells moved more than 3 m. On one thread, all five were identical, so hydrology.py passed max_procs=1.

2026-11-30 finding: --max_procs=1 was silently ignored without write permission

On 2026-11-30, three runs of services/gis-worker/scripts/real_build_checks.py on the same commit and machine (Apple M5, macOS, Docker image tilemapper-gis-worker:test run with --user "$(id -u):$(id -g)") produced byte-identical dem_original.tif (2,075,502 bytes, sha256: 9775cbb6808e0b13...), fill_depth.tif (629,082 bytes, d5e20f513a4d108b...), and outside_entry.tif (18,619 bytes, d0981fc463996574...), but three different conditioned DEMs and flow-direction grids:

  • dem_conditioned.tif: 2,108,457, 2,108,420, and 2,108,423 bytes. Out of 1,022,112 cells, Run A vs B differed at 497 cells (largest 0.080 m), B vs C at 349 cells (largest 0.067 m), and A vs C at 469 cells.
  • flow_dir.tif: 585,403, 585,406, and 585,461 bytes. Run A vs B differed at 1,007 cells, B vs C at 733 cells, and A vs C at 958 cells.

Step isolation on apps/web/e2e/fixtures/real-fargo-nd.tif (292 x 290 at 3.0 m, 8.88% flat cells) showed BreachDepressionsLeastCost is the first and only step that varies (9/45 pairs differed over 10 runs; D8Pointer, D8FlowAccumulation, and FillDepressionsWangAndLiu all had 0/45 differing pairs on fixed inputs).

The cause is how WhiteboxTools v2.4.0 applies --max_procs=1. CLI parsing writes settings.json beside the whitebox_tools binary (resolving symlinks via /proc/self/exe), and the tool re-reads settings.json from disk when it executes. In tilemapper-gis-worker:test, /opt/conda/bin is owned by mambauser:mambauser (mode 0755). When the container ran as the default mambauser user, /opt/conda/bin/settings.json was writable and max_procs=1 took effect. When run under any other UID (such as --user "$(id -u):$(id -g)") or with a read-only install directory, whitebox_tools printed Could not create output settings.json file. WBT is likely installed somewhere without write permission. to stderr, still exited 0, and fell back to max_procs=-1 (all available CPUs; /proc/<pid>/status peaked at 10 threads instead of 2).

What was tried on apps/web/e2e/fixtures/real-fargo-nd.tif in tilemapper-gis-worker:test under --user "$(id -u):$(id -g)":

Setting tried Runs Pairs Conditioned pairs differing Pointer pairs differing Notes
Baseline (--max_procs=1, uncompressed input) 20 190 99 (max 2 cells, 9.0 mm) 99 (max 6 cells) _write_grid already writes uncompressed GeoTIFF; settings.json write failed
RAYON_NUM_THREADS=1 + --max_procs=1 10 45 29 (max 6 cells, 9.0 mm) 29 (max 11 cells) Ignored by BreachDepressionsLeastCost (uses std::thread, not Rayon)
taskset -c 0 + --max_procs=1 10 45 0 0 Still spawned multi-threaded workers; triggered 62 "Error unwrapping" retries in 10 runs
--flat_increment=0.0001 + --max_procs=1 10 45 27 (max 12 cells, 9.3 mm) 27 (max 11 cells) Still ran multi-threaded
--min_dist + --max_procs=1 10 45 0 0 Changes algorithm; still multi-threaded (1 retry; 15/15 pairs differed on 3x3 tiled grid)
Compressed (DEFLATE) GeoTIFF input + --max_procs=1 10 45 21 (max 2 cells, 9.0 mm) 21 (max 6 cells) Still ran multi-threaded
Symlink to whitebox_tools in writable dir + --max_procs=1 10 45 24 (max 2 cells, 9.0 mm) 24 (max 6 cells) Binary resolves symlink back to /opt/conda/bin
Private binary copy in writable dir + --max_procs=1 20 190 0 0 0 retries; peaks at 2 OS threads (main + 1 worker); matches mambauser bit-for-bit

Fix and verified results

_run_whitebox now copies whitebox_tools once per temporary run folder into <folder>/.whitebox/whitebox_tools (~9 ms) so WhiteboxTools always has its own writable settings.json, and raises an error if stderr reports a settings.json write failure when max_procs is requested. This also avoids concurrent worker calls overwriting a shared settings.json.

Measured after the fix:

  • Real Fargo 3 m fixture (real-fargo-nd.tif, 292 x 290): 20/20 runs (190/190 pairs) of analyze_hydrology produced identical conditioned, flow_dir, accumulation_m2, and fill_depth_m grids, matching default mambauser output bit-for-bit (0 cells changed).
  • Full 1 m Fargo field (1,008 x 1,014 = 1,022,112 cells): 5/5 runs of analyze_hydrology produced 0/10 differing pairs (against 10/10 differing pairs before the fix under --user), matching default mambauser output bit-for-bit (0 cells changed). Median BreachDepressionsLeastCost wall time moved from 0.270 s to 0.287 s (+0.017 s), and median 4-tool analyze_hydrology moved from 0.747 s to 0.836 s (+0.089 s, including the binary copy). Two full real_build_checks.py runs produced byte-identical dem_conditioned.tif (2,108,434 bytes) and flow_dir.tif (585,405 bytes); see real-build-checks.md.
  • Regression guard: services/gis-worker/tests/test_hydrology_repeatability.py runs analyze_hydrology twice, five times, and five times against a 0555 read-only whitebox_tools directory on real-fargo-nd.tif, requiring identical conditioned and pointer grids.

Water paths

Each cell drains to its steepest neighbor (D8), and the map draws cells draining at least 0.25 acre. Larger paths are thickened for drawing only. Single-direction routing can't split flow, so on broad flat ground the exact line is one of several the water could take.

Water from past the edge

The terrain stops at the fields plus 100 m, and watersheds don't. Until 2026-11-09 the land draining through a cell was only the land on the terrain, so a swale carrying 80 acres into a field showed as draining the few acres of it inside the edge, with nothing to say so. Low spots' drained areas, the water paths and any inlet's watershed were all low by whatever came from outside.

Now the ground around the terrain is read too, and land past the edge is traced on it:

  • What's read. USGS's 1/3 arc-second DEM for 2 km past the terrain on every side, put on a 10 m grid in the project's coordinate system. That's about 5 km across and 250,000 cells for a 160-acre field. The same read checks the build's heights (elevation-accuracy.md).
  • How it's routed. The ground around is conditioned and routed on its own grid, the same way as the terrain. Each of its cells past the terrain's edge counts for its own area. Where a path reaches the terrain, what it has gathered is handed to the terrain's cell there, and the path ends. From there the terrain's own, finer routing carries it. So land is counted once: past the edge on the 10 m grid, and inside it on the terrain's.
  • What it changes. Only areas: the water paths, and how much land drains through a low spot. Each low spot also says how much of its land is from past the edge.
  • What it doesn't change. No elevation crosses from one grid to the other. The original surface, the conditioned surface, low spots' depths and volumes, and every cell's own routing are exactly what they'd be without it, and a test holds them to that. In the worker the ground around is a ContextDem, which nothing that needs the field's real elevations will accept.

What's still a minimum

An area is shown as "at least" where not all the land draining there could be counted:

  • The watershed runs past the ground that was read, so the path that brings water in starts at the edge of those 2 km.
  • The water has already been through the terrain and left it. What it carried out isn't followed around on the 10 m grid, so below where it comes back in, the area is short by that much. A ditch that weaves along a field's edge does this.
  • There's no ground read past that edge, because the 1/3 arc-second DEM stops there or couldn't be read. If it can't be read at all, or the tool that routes it fails, every cell downstream of the terrain's edge is a minimum, and the build goes on. The terrain's details say whether it was traced.

Marking is deliberately wide. Where nothing is known past an edge, any cell on it may be taking in water, whichever way it drains, so it and everything downstream of it is marked.

Checked on a surface with a known answer

A valley 800 m wide between ridges, with a ridge across its head 600 m north of a 300 m field astride its swale. The land draining through any point of the swale can be worked out by hand.

Near the field's south edge Drains Shown as
By hand, the whole watershed 136.4 ac
By hand, the part inside the field 15.9 ac
The field's terrain alone 16.0 ac at least
With the ground around 136.5 ac, 120.5 ac of it from outside about

test_hydrology.py holds these to within 3%, which is what a 10 m grid can place a ridge to.

What it costs

2 km is a judgment: it's past the next section line on every side, which takes in the watershed of the swales and draws a tile design has to allow for, and it keeps the read small.

Measured on 2026-11-09 on a MacBook, with the worker in Docker, on a 160-acre field's 247 acres at 1 m near Fargo. The ground around was read from USGS over a home connection, from the two 1 degree tiles the area's 2 km reach into:

Step Time
Reading the ground around from USGS 4.8 s
Tracing it, on top of the hydrology step 0.2 to 1.2 s

That's about 5 to 6 s added to a local build. The read was timed once on a quiet machine. Two more, while the same machine ran a test suite, took 5.3 s and 13.1 s, so it varies.

It hasn't been measured on production, where builds took 9 to 12 s and whole runs 28 to 42 s against a 60 s target (terrain-benchmark.md). Reads from USGS's storage are quicker there and processing is slower, so it should add a few seconds and stay inside the target, but that's an estimate until the benchmark is run again. A request that gets no answer is given up after 10 s, and the build goes on without the ground around.

What this can't see

  • The 10 m DEM is coarser than the terrain, and where the two disagree about which way water runs near the edge, the terrain's routing is what's followed from the edge inward. Water the 10 m DEM brings to the edge at a spot the terrain drains outward from is lost.
  • On very flat ground a path is one of several the water could take, on either grid. A large area arriving by one path is a reason to look at the ground, not a measurement.
  • Culverts and tile, as everywhere here.

Low spots

A low spot is where water would stand on the original surface before spilling. To count, it must be at least 10 cm deep and 100 m² in area. On real ground at Moorhead, that took 39,077 depressions down to 39, which still held 86% of the volume, so it mostly removes lidar noise.

Each low spot also says how much land drains through it: the most area accumulating at any of its cells, on the conditioned surface. That includes what comes through other low spots upstream and what was traced from past the terrain's edge, and it's a starting point for a surface inlet's watershed. Where it couldn't all be counted, the list and the inlet's panel say "at least", and the button that takes the figure for an inlet says it's a minimum.

Stored hydrology grids (pipeline version 6)

Alongside the original and conditioned DEMs and the D8 flow-direction grid (flow_dir, uint8 COG), each build from pipeline version 6 onward stores two more single-band Cloud-Optimized GeoTIFFs on the same grid (256 × 256 tiles, DEFLATE compression):

  • Fill depth (fill_depth): how deep water would stand on the original ground before spilling (FillDepressionsWangAndLiu minus original DEM, clamped to 0 on dry and missing cells). Stored as uint16 in millimeters (scale = 0.001 m, predictor=1, cap 65.535 m). One-millimeter precision is 100 times finer than QL2 lidar's 0.10 m vertical accuracy and 100 times finer than LOW_SPOT_MIN_DEPTH_M (0.10 m), while using a third of the storage and half the browser memory of float32.
  • Outside run-on entries (outside_entry): each cell's own share of land entering from outside the terrain edge, in square meters (inflow_m2 from trace_run_on, zero everywhere when the ground around couldn't be read or on missing cells). Stored as entries at the cells where external flow crosses in — not as the carried downstream running total outside_m2 — so any browser watershed sums each outside entry once and stays valid when a culvert changes the downstream route. Stored as float32 (predictor=3) with no quantization loss; because almost every cell is zero, it compresses to a few kilobytes.

Measured storage sizes on fixtures (not yet measured on the three live USGS test fields):

Grid E2E fixture (512 × 512, 1 m, 38% wet) Valley fixture (300 × 300, 1 m, 88 entries) Synthetic prairie (1,000 × 1,000, 1 m, 31% wet, 100 entries)
Original DEM (float32, pred=3) 591 KB 18 KB 2,261 KB
flow_dir (uint8) 27 KB 2 KB 554 KB
fill_depth (uint16 mm, pred=1) 178 KB — 676 KB
fill_depth (float32, pred=3, rejected) 466 KB — 2,238 KB
outside_entry (float32, pred=3) 2 KB 5 KB 14 KB

Terrains built before pipeline version 6 lack fill_depth and outside_entry. Tools that rely on them (raindrop path and watershed) refuse to silently fall back and prompt the user to rebuild the terrain under Elevation data.

What this can't see

  • Culverts. Lidar sees a road, not the pipe under it. A cut through a road is a guess at where water gets through.
  • Existing tile, and how fast soil takes up water. A low spot is where water could stand, not proof that it does.
  • Very low relief. A depth is only as good as the surface, and survey marks put typical error at 13 to 21 cm RMSE. See elevation-accuracy.md. The shallowest low spots are near that size.

A tool problem to remember

WhiteboxTools 2.4.0's FillDepressions crashes now and then with "Error unwrapping 'output'". It failed 1 of 60 runs, and more under load. FillDepressionsWangAndLiu had no failures in 60 runs, so that's what's used.