garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/notes/tunnels.md

raw


topic: roads triggers: [tunnels.rs, tunnel, entrance, build_structures, unmeasured_profiles, road_levels::structures, fill_to_land, land, cutting, cut_sheet, opening_digest, clipmap hole, basemap over a tunnel, building_passage, underground, drawn_street, street_shape, TunnelRoad, TunnelStructure, Tunnels, cut_ground, opening, floor_lift, layer, "the tunnel isn't rendering", "drive through the tunnel"] updated: 2026-09-28

Tunnels: what the tiles know, and everything they do not

Until 2026-09-15 a tunnel was a gap. vector_tiles::drawn_street refused any way the tiles flagged, the raster painter skipped it, and extract_street_labels withheld its name, so the southern ring road at Groningen — Weg der Verenigde Naties, eight of whose segments are flagged — was simply absent between its entrances: no carriageway, no markings, no name, nothing to walk into.

That refusal was right about one thing and silent about the rest. Painting a tunnel on the ground rules a pale band across every junction it passes under, which is a lie about what is up there. Nothing ever drew it anywhere else.

Where a tunnel is, and how deep, is decided on the road network (2026-09-28)

Read this section first; much of what follows is the history of a planner that no longer exists. From 2026-09-15 to 2026-09-28 tunnels.rs laid tunnels out from the tags alone — which ends were entrances, which surface way beyond a portal was its ramp (find_approaches), which untagged gap between two lids was a cutting (bridge_short_gaps) — at an invented depth, and then every consumer lifted the result back by whatever the survey had already dug (lift_for, stand_tunnel_on, convention_depth, cover_over, road_under_measured). The sections below record why each of those rules existed; the rules themselves are gone.

What replaced them is the model bridge-heights.md built for bridges, run the other way:

Four things the network needed that the planner had papered over:

The tiles carry one bit

Measured on crates/geo/tests/fixtures/groningen_z14.mvt, 2026-09-15, and pinned by tunnels::tests::the_tiles_say_which_streets_are_tunnels:

The second point has two consequences. A tunnel's depth cannot be read, so LEVEL_DEPTH_M (7 m) is a convention applied to every tunnel equally — the one number here a real source could replace. And vector_tiles::layer_of answers 0 for every street on this tileset, which makes the raster's "OSM layer first, so a flyover paints over what it crosses" sort a no-op. Its doc comment says Shortbread carries layer on streets; on this tileset it does not. That is a separate thing to fix and is not fixed here.

An arcade is flagged a tunnel too

OSM's tunnel also covers building_passage — a way through a building, at ground level, that people walk down. Over the Groningen ring box roughly a quarter of the flagged ways are those (12 of 44, Overpass, 2026-09-14). With no layer to separate them, sinking every flagged way seven metres puts a hole under a shopping street.

The tile answers it, because a passage runs through a building and a tunnel does not: tunnels::building_passages tests points along each flagged way against the tile's own buildings polygons and calls it a passage on a majority. Those stay on the ground sheet and are drawn as ordinary road — which is more than they had before, since the old rule dropped them with everything else.

It fails safely. A tunnel mistaken for a passage is drawn where it always was; a passage mistaken for a tunnel is the visible error, and needs the way to spend most of its length inside a building footprint to happen.

The entrance is invented, and it has to be

OSM tags the tunnel at full depth for its whole length, and the road that descends to the entrance is an ordinary surface way with nothing on it to say it is a ramp. Drawn literally that is a carriageway seven metres down meeting one at ground level with a vertical step between them — a hole in the road.

bridge-heights.md hits the same constraint from the other side and accepts the step: a deck with abrupt ends still reads as a deck. A tunnel cannot, because the step is the way in. So a ramp is built at MAX_GRADE (6 %), which puts a 7 m descent inside 120 m — on the road beyond the portal, not inside the tag (below).

An end is a entrance only if the line genuinely ends there: a run the tile border cut is held at full depth (so the two halves meet flush at the border), and an end another tunnel run also ends on is a tagging split, not a opening.

The ramp is outside the tag, because the tag is the lid

Settled 2026-09-23. Until then the ramp was built inside the tagged run, which assumed OSM tags the whole descent. It tags the covered part. Groningen's ring under the Oude Winschoterdiep is the case that showed it: the tagged way runs from the last lid to about 35 m past the canal, and in the world the road climbs out on roughly 300 m of open ramp beyond that, tagged as ordinary road. With the ramp inside the tag, the carriageway under the canal was 2 m down and roofless — the phone showed the ring crossing the water at grade.

So find_approaches looks, at each entrance, for the way that carries on from the portal (an end within SAME_POINT_M, leaving within JOIN_COS of the tunnel's heading), follows it through further ways while exactly one continues, and builds the ramp there as an open cutting keyed to that street. Four rules, each a failure it avoids:

a_road_under_water_has_a_lid_over_it pins it: no open, sunken stretch of deck may lie inside a water polygon, and the ring's must lie under one covered. What is still wrong: an approach that leaves the tile is cut at the border and gets only the ramp that fits this side of it; and a portal that sits exactly on the border, with its approach in the neighbour, has no tunnel in the neighbour's tile at all, so the neighbour draws that road at grade.

The roof and walls face in

Also 2026-09-23, from the same phone session. The tunnel material culls back faces, and the roof was wound like the deck — facing up into the earth — while both walls faced outward. From inside a lid there was no ceiling and no sides; with the ground's underside culled as well, the deck looked out on sky, the far city and the lawn's grass hanging overhead. The inner notch at a bend was also a real triangle wound backwards, not the "degenerate" one the code assumed. every_tunnel_face_is_wound_toward_its_normal checks every triangle against its declared normal.

The corridor is the chain, not the tagged run

Settled 2026-09-22, and it is the fault every capture of this feature had been photographing. Groningen's southern ring is built dicht-open-dicht-open — lid 174 m, open 109 m, lid 241 m, open 107 m, lid 233 m — and the tunnel flag marks only the covered parts. So the ring arrives as six lid runs with ordinary untagged road between them.

bridge_short_gaps already had to find those connecting ways: it is how it knows the lids either side are not portals (a gap shorter than 2 × ramp_m cannot reach the surface whatever the map says). Having found them it threw the geometry away. Nothing else looked at them, so tessellate_roads drew each one as an ordinary carriageway on the ground at y = 0 — between two decks seven metres below it.

Measured on groningen_ring_z14: 863 m of carriageway with six vertical seven-metre steps in it. From above, the road ran out of the city and stopped dead in a park; dump_the_fixture_bores printed the whole 239 m of the deepest run as -7.00 m from metre zero, with no ramp anywhere, because that run is a middle lid and both its ends are gap-joined.

Each gap is now absorbed into the corridor as an open cutting: the same deck, walls and ground hole a tunnel already builds, and never a lid, because the one thing the tile does say about that stretch is that it is not covered. Three things follow and each was a bug before it was a rule.

the_ring_road_never_steps_between_a_lid_and_the_road_beside_it is the guard, and it is local: at every end of every sunken carriageway either another corridor continues at the same depth, or the road has come back up, or the tile border cut it. A step is none of those, and the test names the place and the height.

The opening merge needed tightening to take it

parallel_and_near's proximity half is an overlap of bounding boxes grown by MEDIAN_M, which two long diagonal corridors satisfy a long way before the corridors themselves are near. With the cuttings present, a 46 m footpath and the ring's carriageway 88 m away passed it, were parallel to within a degree, and hulled a 130 × 102 m crater out of the park between them.

Tightening the distance does not settle it: the two rings come within 19 m at one corner, and a cutting legitimately touches the lid it joins. What actually distinguishes the case the merge exists for is that a dual carriageway's halves are the same road — same class, same width, mapped as two ways — so that is the key now, and a footpath can no longer be hulled into a trunk road however parallel it runs.

A tunnel is covered, and it took two wrong rules to get there

The roof rule went wrong twice, and both times the symptom was the same picture: a long open ditch where a tunnel should be.

First: roofing only under a building. The tiles do not say where a lid is, and they do say where a building is, so a way under a building was treated as covered and everything else as an open cut. Groningen's ring runs under a park, so nothing was ever covered. The observation that pushed me there — roofing by depth "buries the whole ring, with nothing to see" — is not a bug report. It is what a tunnel looks like from above. Depth decides now; the buildings layer only tells an arcade from a real tunnel.

Second: a dual carriageway could never surface. A road only opens where open_ends decides it genuinely ends, and that test asked "is there another tunnel's end within 25 m pointing the same way?" — meaning a way somebody had split in two. The two halves of a dual carriageway end side by side at the entrance, about eighteen metres apart, pointing the same way. Each saw the other, both were called splits, and neither got an entrance: measured over Groningen, every 7 m road tunnel in the streamed map was sealed from edge to edge, with no entrance anywhere. Beside is not beyond, and the test now requires the offset between the two ends to lie along the road. Same tile after the fix: four entrances on the deep roads, 6.0–6.7 m down, in opposite-heading pairs.

What the ring's portals taught, 2026-09-23

Bisected on the Deck with CARTO_DBG_HIDE (and !word to look at one layer alone), which is the instrument to reach for before guessing — four guesses about one pale block in a mouth were each disproved before it existed.

Still open at the east portal: a thin dark line along the median toward the canal in an oblique view, which the layer bisect puts at the ground meeting the tunnel rather than in the tunnel mesh, and pale bars across the canal that look like the quay walls' measured height stood up from the ground the same way.

The ground over a lid is filled back in

Settled 2026-09-16: where the two sources disagree, the map's tag wins. The height survey measures what it flew over, and Groningen's ring was an open trench when it was flown — it has a lid on it now. So the survey digs nine metres out of ground that is continuous, the lid stays at street level, and a covered road reads as a raised platform with a ditch either side.

Tunnel::covered_weight answers "how much is the land here ground over a lid", and map_geometry::stand_on_terrain lifts the ground back to the level of the land around it by that weight. Four properties, and the test pins all of them: full over the covered stretch and its own width, zero over the open ramp (the fill must never close the hole you drive through), zero away from any tunnel, and a smooth fade over TUNNEL_DATUM_M rather than a step, because a hard edge is a crease running the length of the road. The shading is flattened with it — a surface lifted out of a trench is level, and lighting it with the trench's slope would shade a flat lid as though it still dipped.

What it costs: where the survey is right and a road is genuinely in an open cut with a lid only over part of it, the open part keeps its measured trench and the covered part is filled — which is the correct picture, but it means the ground beside a covered stretch is no longer what AHN measured. That is the trade, taken deliberately: the tag is the newer fact.

Superseded: the lid floated over its own ground

The measured ground says that road is in a nine-metre open trench, because it was one when AHN flew it. The tag says tunnel, so it now gets a lid at street level. The ground beside it follows the measurement down and the lid does not, so the covered stretch reads as a raised platform rather than as ground you can walk over.

Two ways to settle it and it is a question about sources, not a bug:

Superseded: the deck stood four metres above the ground beside it

Reported 2026-09-17 from a phone over the ring, as a carriageway drawn on top of raised terrain instead of under it. Two unrelated faults with the same symptom.

The deck asked the depth buffer for an enormous bias. geo::tunnels filled its per-vertex rung attribute from road_rung, which has not been the depth ladder since 89125ef collapsed it to two rungs on 2026-08-26 — it survives as the order the road cut runs in and still returns 0..399. Tunnels were written three weeks later against a doc comment that still claimed the value reaches the graphics card. With bridge: true that is rung 391–398, about 25,900 depth units against a surface road's 512, and the shader spends it as a fraction of the fragment's own depth: 0.3 m nearer at 100 m, 3.1 m at a kilometre, and past about 2.3 km more than the whole LEVEL_DEPTH_M the tunnel is buried by. The walls and roof carried 0 throughout, which is why only the carriageway was wrong and why nothing photographed from close up ever showed it. Two tests now walk the tessellated meshes; both fail on the old value, reporting rung 391.

And the lift really did put it above ground. stand_on_terrain raises a tunnel by what the survey has already dug, capped so it cannot rise past its own depth — but the cap was the deepest vertex of whichever merged mesh was being displaced, i.e. a whole tile's tunnels, so everything shallower rose too far. Almost all of a tunnel is shallower: depth_at spends about 117 m ramping at each end and the ring's runs arrive 172–242 m long. Forty metres in, where the tessellator wants 2.4 m and the survey has dug 6.4, the deck was lifted the full 7 m and drawn four metres above the land beside it. The cap is now the convention depth under the vertex's own plan position (Tunnels::convention_depth) — asked by position rather than by the vertex's own height, so a wall top is lifted with the deck under it instead of being capped at zero and left behind. The drawn mesh, the walkable floor and the nearest-tunnel metric now share one lift_for; they had three versions of it, and the first two disagreeing is the avatar walking through the road.

The roof stops where it would surface, and that is the opening

There is no "how long is the opening" constant. The ceiling is drawn wherever it fits under the ground (CEILING_MAX_Y) and is absent where the rising deck would push it through — which is exactly the open approach. The plan corridor of that roofless stretch is handed back as a opening, and cut_ground subtracts it from the cell's ground meshes on the same worker, on road_stitch's integer millimetre lattice and for the same reason: a boundary produced by subtracting one shape from another is built from that shape's own vertices, so the hole's edge and the wall standing in it are one line rather than two opinions about one line.

Without the cut there is no tunnel, only a room with a lid over its own entrance.

You walk in because a tunnel floor is a negative storey

interiors::InteriorFloors already answered "how high is the ground here" for a building's upper storeys, and both movement paths add the answer (AvatarState::floor_lift) to the terrain. A deck is that with the sign flipped, and needs no new channel — interiors::support_level asks the tunnels first.

What keeps the two apart is the rule that was already there: a surface more than STEP_UP_M above you cannot pick you up. From the pavement over a tunnel the deck is unreachable, so you walk over the tunnel; from inside, the ground over your head is unreachable, so you walk along it. The only way between them is the ramp, which rises continuously out of the deck — exactly as the only way upstairs is the flight.

The walls have to be real collision. Without them, walking into one leaves the carriageway, finds no deck under the next step, and the floor returns to ground level — which reads as being fired seven metres up through the road. So a tunnel is a hollow collision::Footprint while the avatar is inside it (tunnels::inside), and no collider at all while it is not: on the surface it must not be something to walk into, and underground it must be the only thing that is. The two collision worlds are exclusive in both movement paths.

Verified, and not

The southern ring in open cut beside DUO

Captured over the Groningen ring on 2026-09-15 and again on 2026-09-16 with the basemap cut (--shot, lavapipe, 53.20548 6.57498 at 35 m, pitch −25): 41 tunnels standing in the streamed cells, deepest deck −7.0 m, 68 clipmap tiles carrying a hole, and the sunken dual carriageway of Weg der Verenigde Naties in the trench with its markings and its traffic on it, retaining edges either side, and the ground unbroken where a building covers it.

Aiming a camera at a tunnel is harder than building one, and two days of this work went on it. A shallow look along a tunnel — the angle that should see furthest into an open cut — is blocked by the trees standing along the corridor: at 18° above the horizon the frame is a wall of canopy. Deriving the camera from the way's own geometry (an Overpass query, the bearing across the midpoint, the camera placed back along the view direction) fixes the direction and does not fix this. What works is the picture above: close, and steep enough to look over the trees into the cut. docs/grade-shots.toml is the list, and its tunnel entries are the ones still worth re-aiming.

The floor rule is unit-tested in all four directions it can go wrong (interiors::tests): the pavement over a covered tunnel holds, the ground overhead cannot lift you off the deck, the opening drops you onto the ramp, and ordinary ground beside a tunnel is unchanged.

Walking through one has not been seen end to end. Standing the harness's avatar on a deck needs it to spawn on a specific 12 m strip of carriageway, and it spawns ~40 m from the --at point for a reason this work did not run down — avatar_floor_m in --dump-state reads 0 in every attempt, which is the honest report of an avatar on the surface, not of a floor that failed. The instrument is there for whoever aims it: avatar_floor_m is the tunnel deck's level when the avatar is on one.

The basemap needs the same hole, and every level of it

Cutting the vector ground is not the whole cut. Under those surfaces lies the raster clipmap — y = -0.03 for the finest level and 0.4 m lower per coarser one (map_stream::level_y) — painted with a picture of the same streets. All of it is above a seven-metre carriageway, so a cut that opens only the vector sheet opens the tunnel onto a copy of the map: the 2026-09-15 capture over the merged ring trench shows pale ground with the city's own cars drawn across the opening.

So tunnels::cut_sheet takes the same rings out of a clipmap tile's own mesh, in terrain::displaced_grid — the one place a tile's triangles exist. Three things follow:

Photographing one is harder than building one

Worth knowing before the next person spends a day on it. Across this work, eleven cameras aimed at a tunnel missed — by 28, 42, 56, 62, 64, 103, 111, 122, 136, 140 and 178 metres — landing on a park, a rail yard and the side of an office block, each costing minutes of software rendering to discover. Three causes, all now removed:

tunnel_ahead_m is the check: zero means the capture is pointed at a tunnel, and anything else means the picture is of something else. And the pictures are barely evidence anyway on a software rasteriser at handset quality — the profile printed by tunnels::tests::dump_the_fixture_bores says more in ten lines than any of them did.

Both halves of the aiming fix existed and neither could be reached

Found 2026-09-22, and between them they are most of why this feature was verified by hand-aimed one-off captures for a month.

docs/grade-shots.toml had never parsed. It sets time, fog, clouds and rain at the top — correctly, since a group is a set of views of one subject and a seven-metre trench reads only when the sun puts one wall in shadow — and those were not Manifest keys, so deny_unknown_fields refused the whole file. The only symptom was a one-line error from a command nothing in CI runs. The manifest built to check grade separation was therefore dead from the day it was written. shot_presets::every_checked_in_manifest_loads is the guard now.

And --look-at did not exist as a flag. ShotSpec::look_at is threaded through lerp, the MCP surface and aim_scripted_camera, which has computed the aim from where the camera really ends up since the day tunnels landed; nothing could set it. So the three ring cameras the manifest shipped with were aimed by pitch, and run on a real GPU they report tunnel_ahead_m of 82.7, 116.9 and 89.6 m — all three photograph a park, which is exactly what the section above predicts and what two sessions spent days rediscovering by eye. There is a --look-at LAT,LNG flag and a look_at preset key now, and the ring views are aimed at --dump-state's own tunnel_entrance_lat/lng.

Traffic drives the corridor, and its depth travels on the line

extract_drive_lines went through drawn_street, which takes every tunnel-flagged way off the ground sheet because the geometry draws it somewhere else. That is the right answer for a ribbon and the wrong one for a network: it cut the drive graph at every portal, so a car reaching the ring ran to the end of its line and was recycled, and 863 m of carriageway under the park carried no traffic at all. And drive_traffic placed every car at terrain.ground_at + ride height, so even the untagged cuttings would have had their cars driving through the air over the trench.

The depth is on the line (DriveLine::deck), never sampled from the world. An ordinary street crossing over the ring is at the same world XZ as the carriageway seven metres under it, so a depth read by position drops that street's traffic into the tunnel — which is DeckPlates::lift_at's first attempt seen in a mirror (grade-separation.md). The only thing that knows which of two roads at one point a car is on is the feature its line came from, so tunnels::SunkenWay keys each corridor to its streets feature and extract_drive_lines samples only its own. It covers the cuttings as well as the lids, and it has to: a cutting is an untagged way this module has decided is seven metres down, so a consumer reading the tunnel flag would put its cars on the ground over an open trench.

The lift a car gets is the lift its road got, through the same lift_for. Over an open cutting the survey has already dug the trench, so adding an invented seven metres to a measured nine would put the car nine metres under its own road.

A bridge is the same question upside down and is answered the same way, except that a deck's height is a measurement arriving some frames after the cell, so it cannot be baked on the worker: the line carries on_deck and DeckPlates is consulted only for the lines that have it.

What is not done

Everything vertical here is invented, and two sources we already stream could replace it

The tiles carry one bit. So the plan position of a tunnel is exact and every vertical fact in the picture is a convention: LEVEL_DEPTH_M (7 m), MAX_GRADE (6 %), CLEARANCE_M, and "roof it where a building is above". That is a plausible tunnel, not that tunnel, and it is why a capture of one is unconvincing however the camera is aimed.

Both halves of the real answer were checked on 2026-09-16 and both exist:

The survey publishes the footprint. tunneldeel is one of the BGT's 49 collections — the covered section as a polygon, with relatieve_hoogteligging giving the layer ordering the tiles lack. Seven live polygons over the Groningen ring box, all at level −1; one is 183 × 103 m centred on 53.20536, 6.57503, which is the DUO cut. That replaces under_a_building — a heuristic standing in for a surveyed fact — with the fact. overbruggingsdeel beside it is the same answer for bridge-heights.md. It needs a fetch in atlas's pipeline, i.e. another hand-synced format (where-the-map-data-is-built.md).

AHN measures the trench, at half a metre. dtm_05m over that same polygon, 54,291 valid samples in a 120 m box:

min -3.64   median 5.39   max 11.05  (m NAP)      range 14.69 m
cross-section every 4 m:  5.5  -0.9  -0.4  -0.1  0.9  2.4  2.6  3.4  3.9  4.8  5.3  5.5 …

The ramp is in the data: the ground falls from +5.5 m to −0.9 m over about forty metres and bottoms at −3.6 m. About nine metres of cut, against the seven that is hard-coded.

Which suggests deleting the constant rather than tuning it. In an open cut a tunnel road is not a special object — it is an ordinary road lying on ground that happens to be nine metres down. So the measured terrain should carry it and depth_at should not run there at all, leaving a convention only under the surveyed covered footprint, where the laser sees the roof and no instrument can see the floor. That also disposes of the double-counting this bullet used to warn about: today the terrain would dig the cut and the deck would be sunk into it. Two things are missing for it: the tunneldeel fetch above, and the fine half-metre AHN tier feeding geometry rather than only the shading normal it feeds today (systems::ground_detail).