Skip to content

Tiered distance - #27

Draft
mority wants to merge 4 commits into
motis-project:masterfrom
mority:local_radii_lut
Draft

mority wants to merge 4 commits into
motis-project:masterfrom
mority:local_radii_lut

Conversation

@mority

@mority mority commented Oct 12, 2026

Copy link
Copy Markdown
Member

geo::distance now returns the WGS84 geodesic distance instead of the haversine on a sphere. For each pair it uses the cheapest method that is accurate enough:

max(|lat a|, |lat b|)
 90 +-------------------------------------------------+
    |                     karney                      |
 80 +-------------+-------------------------+---------+
    | local radii |         andoyer         | karney  |
    |    ~7 ns    |         ~50 ns          |  ~1 us  |
  0 +-------------+-------------------------+---------+
    0           30 km                   15000 km  20004 km
                    distance (not to scale)
  • local radii: <= 0.004% error up to 30 km at any latitude below 80° (the
    1° table's interpolation error), but fails where meridians converge
    (poles) and grows with distance beyond (0.008% at 50 km and 75-80°);
    its own result picks the next tier
  • andoyer (first order in flattening): <= 0.00125%, but breaks down below
    ~10 m and degrades towards the antipode; its own result picks the next
  • karney: exact everywhere

Compared with the haversine:

  • Precision: the sphere is off by up to 0.56% (168 m on 30 km). This is within 0.004% of the exact value everywhere, and within about 1 m below 30 km.
  • Speed: almost all real distances are short, so it is faster: 6.8 ns instead of 23 ns per segment on 17.9 M Europe OSM way segments.

Compared with the approx_* functions:

  • Precision: approx_* uses the same sphere and a flat earth, with the longitude scale taken at the query point. It is off by up to 0.56% below 5 km, and the error grows with distance: 0.9% at 30 km, 2% at 100 km. distance() stays within 0.004%.
  • Speed: approx_* stays faster per pair: 3.6 ns instead of 6.8 ns, once the scale has been computed for the query (one haversine, ~23 ns). It remains the better choice for testing many candidates against one point.

The approx_* functions could switch to local radii without changing how they are used (scale computed once per query, two multiplications per pair). This is not part of this PR:

scales per query per pair max error below 60°
sphere (approx_* today) 21.5 ns 3.55 ns 0.56% up to 30 km
local radii 2.3 ns 3.54 ns 0.008% at 1 km, 0.05% at 10 km, 0.16% at 30 km

The cost per pair stays the same, the scale gets 10× cheaper and the sphere error disappears. The remaining error comes from using the query point's scale for a point at another latitude.

The spherical distance stays available as haversine_distance, and the approx_* functions are unchanged. box::extend now assumes 110,500 m per degree of latitude (WGS84 has 110,574 m at the equator). Otherwise point_rtree::in_radius would miss points at the edge of the radius.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant