Ground distance on the quick look
Plan v4 item 525, PR 60. The quick look's tiles (apps/web/src/lib/terrain/quick-look.ts) arrive from the USGS elevation service in Web Mercator, EPSG:3857, whose cells are square on the map and not on the ground. A map meter is longer than a ground meter by a factor that grows with latitude, about 1.47 at 47° north. Every slope, length and grade worked from quick-look tiles, and the hillshade drawn from them, uses distance on the ground. This page gives the method, a case worked outside the engine, and what happens where the data runs out.
Method
Web Mercator takes WGS84 geodetic latitude φ and longitude λ and applies the spherical Mercator formulas with the ellipsoid's semi-major axis as the radius: x = aλ, y = a ln tan(π/4 + φ/2), with a = 6,378,137 m. Source: NGA, Implementation Practice: Web Mercator Map Projection, NGA.SIG.0011_1.0.0_WEBMERC, 2014, sections 2 and 5.
So dx/dλ = a and dy/dφ = a / cos φ. On the ellipsoid, a step dλ east is N cos φ dλ meters and a step dφ north is M dφ meters, where N = a / √(1 − e² sin² φ) is the radius of curvature in the prime vertical and M = a(1 − e²) / (1 − e² sin² φ)^{3/2} the meridional radius, e² = 0.00669437999014 for WGS84. Source: J. P. Snyder, Map Projections: A Working Manual, USGS Professional Paper 1395, 1987, equations 4-18 and 4-20.
Ground meters per map meter, then:
- east-west: N cos φ / a
- north-south: M cos φ / a
groundPerMapMeter(latDeg) returns both. They differ by about 0.3%, because Web Mercator isn't conformal on the ellipsoid, so the quick look keeps them apart.
Where they are used:
- A tile's cells.
decodeQuickLookTiletakes the scale at the tile's middle latitude and gives the tilecellSizeM(east-west) andcellSizeYM(north-south) in ground meters. A 512 px tile at level 15 spans 0.0075° of latitude at 47° north, over which cos φ changes by 0.014%, so one scale per tile is enough. - Slope and hillshade. Horn's 3 by 3 kernel, as
relief.mddescribes for built ground, with the east gradient overcellSizeMand the north gradient overcellSizeYM.shadeHillshadetakes the two spacings for a quick-look grid; for built ground they are one, and its output is unchanged bit for bit.QuickLookTileStore.slopeAtreads a slope the same way. - Lengths.
groundStepMmeasures a step between two map points with both scales taken at the step's middle;lineLengthMandQuickLookTileStore.profileadd up steps. A profile samples about one tile cell apart, and a line's length uses its own vertices, so no step is long enough for the scale to change across it by more than a few parts in a million.
A case worked outside the engine
apps/web/src/lib/terrain/quick-look-ground.test.ts. A plane is laid out in local east-north-up meters at 47° north, 96.8° west: it rises 2% to the east and falls 1% to the north, so its steepest slope is √5% = 2.2361%. Its heights are put into a level-15 quick-look tile, 512 px plus a border, at each cell center, by converting the cell's latitude and longitude to WGS84 earth-centered coordinates and then to east and north of the plane's origin. That path shares no formula with the app's Web Mercator scale. The tile is written as LERC, decoded by the app, and read back.
By hand, at 47°: sin² φ = 0.534878, √(1 − e² sin² φ) = 0.998208, N/a = 1/0.998208 = 1.001795, M/a = (1 − e²)/0.998208³ = 0.998665, cos φ = 0.681998. Ground per map meter east-west is 1.001795 × 0.681998 = 0.683223, north-south 0.998665 × 0.681998 = 0.681088. The test checks groundPerMapMeter(47) against these to six places.
| Read from the tiles | The plane's | Read | Difference | Tolerance |
|---|---|---|---|---|
| Steepest slope at the tile's middle | 2.23607% | 2.23609% | 0.001% | 0.1% |
| Length of a line across the tile | 710.5516 m | 710.5516 m | 0.0000002% | 0.05% |
| Grade along that line, from a profile | 2.1736% | 2.1754% | 0.08% | 1% |
| Hillshade at the middle, default light | analytic, 0–255 | within 1 gray level | 1 level |
The tolerances, and why:
- Slope, 0.1%. Heights are stored as 32-bit floats, about 3 × 10⁻⁵ m at 300 m, over a kernel 8 cells of 1.63 m wide: a gradient error near 2 × 10⁻⁶, 0.01% of a 2% slope. The scale changes 0.014% across the tile. 0.1% is several times both together and far below the 47% a slip back to map meters would make.
- Length, 0.05%. The only approximation is the scale at each step's middle. 0.05% is 36 cm in 711 m.
- Grade, 1%. A profile reads the cell holding each point, not an interpolated height, so each end can be off by half a cell times the slope: about 1.8 cm at each end over 711 m, 0.15% of this grade. 1% leaves room for steeper ground without hiding a scale error.
The same test computes the slope and the length in map meters and checks that they are wrong by a factor of 1.466 ± 0.005 (1 / cos 47° = 1.4663), so a change that dropped the correction fails. It also checks that the hillshade differs by more than 3 gray levels from a plane 1.466 times flatter, which is what shading in map meters would draw.
LERC from the service holds each height to 1 cm (quick-look-service.md). This test writes its plane uncompressed so the check is of distance alone; the 1 cm is noise on real tiles, not a bias.
Where the data runs out
A point with no loaded tile under it, or a cell the service sent no height for, has no reading: heightAt and slopeAt give null, a slope needs all nine cells of its kernel, and a profile marks each such sample with a null height and says it isn't complete. A line's length needs no ground and is always given. The test covers each: an empty store, and a tile with one cell removed under the line's start.
What this doesn't cover
Nothing here makes a quick-look figure a measurement. The rule under "May the quick look show numbers?" in docs/PLAN_V4.md says which figures may show and with what, and item 517 keeps quick-look heights out of everything that sizes, grades, checks, measures, saves, prints or downloads.