From 0e71678773df45ff6bc5f323e1ea39d87995cf9a Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 22:13:54 +0000 Subject: [PATCH 1/2] around: index the input set's geometry with a coarse grid around compared every candidate point and segment against every point and segment of the input set, so the cost grew with (input set size) x (number of nearby candidates). An input set of 282 ways took 24 s. Index the input set's points and segments in a lat/lon grid. Each entry is filed under the cells of its reach box: its bounding box widened by its own length plus the radius. Every exact test (point near segment, segment near point, segments intersecting) can only pass when the two reach boxes overlap, so a candidate is tested only against entries in the cells its reach box touches. The exact tests are unchanged. Boxes near a pole, crossing the antimeridian or spanning more than 64 cells are never pruned: such entries are checked by every lookup, and such candidates are checked against every entry. Nova Scotia extract, after the fixed-size vector change: way(around.s0:100), 282 ways in .s0 9.9 s -> 1.2 s way(around.s0:100), ways in a city bbox >120 s -> 4.7 s way(around.s0:100), nw within 1 km 11.6 s -> 0.2 s Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT --- src/overpass_api/statements/around.cc | 145 +++++++++++++++++++++----- src/overpass_api/statements/around.h | 88 ++++++++++++++++ 2 files changed, 207 insertions(+), 26 deletions(-) diff --git a/src/overpass_api/statements/around.cc b/src/overpass_api/statements/around.cc index dcd7375cc..f3dce2808 100644 --- a/src/overpass_api/statements/around.cc +++ b/src/overpass_api/statements/around.cc @@ -695,6 +695,99 @@ Ranges< Uint32_Index > Around_Statement::calc_ranges(const Set& input, Resource_ } +//----------------------------------------------------------------------------- + +/* Why reach boxes are enough. Let reach(X) be the bounding box of X widened by + (length of X + radius), where a point has length 0. Every exact test only passes + when the two objects' reach boxes overlap: + + - Point P near segment S (is_inside(lat, lon)): the test requires + great_circle_dist(P, first point of S) <= sqrt(len(S)^2 + radius^2) + <= len(S) + radius, so P lies in reach(S). + - Segment C near point Q (is_inside(segment), first loop): the same test with the + roles swapped puts Q in reach(C). + - Segments S and C intersect (intersect()): the crossing point lies on both arcs, + so within len(S) of S's first point and within len(C) of C's first point; it is + in both reach boxes. + + The widening uses a slightly larger distance than needed and the latitude closest + to the pole for the longitude scale, so the boxes are conservative. Boxes too + close to a pole, crossing the antimeridian or spanning many cells are never pruned. */ + +Reach_Grid::Box Reach_Grid::reach_box(double lat1, double lon1, double lat2, double lon2, double reach) +{ + Box box; + // great_circle_dist uses 10,000 km per quarter meridian, so 1 degree of latitude is + // 1e7/90 m. A point within distance d is within d/(1e7/90) degrees of latitude. + double margin_deg = (reach * 1.02 + 1.0) / (10.0*1000*1000/90.0); + box.south = std::min(lat1, lat2) - margin_deg; + box.north = std::max(lat1, lat2) + margin_deg; + double poleward = std::max(std::abs(box.south), std::abs(box.north)); + double lon_margin_deg = poleward < 80.0 ? margin_deg / cos(poleward/90.0*acos(0)) : 360.0; + box.west = std::min(lon1, lon2) - lon_margin_deg; + box.east = std::max(lon1, lon2) + lon_margin_deg; + box.usable = poleward < 80.0 && margin_deg < 5.0 && std::abs(lon1 - lon2) < 180.0 + && box.west >= -180.0 && box.east <= 180.0; + return box; +} + + +bool Reach_Grid::cell_range(const Box& box, int64& lat0, int64& lat1, int64& lon0, int64& lon1) +{ + if (!box.usable) + return false; + lat0 = (int64)floor(box.south / cell_size); + lat1 = (int64)floor(box.north / cell_size); + lon0 = (int64)floor(box.west / cell_size); + lon1 = (int64)floor(box.east / cell_size); + return (lat1 - lat0 + 1) * (lon1 - lon0 + 1) <= max_cells; +} + + +void Reach_Grid::clear() +{ + cells.clear(); + always.clear(); + total = 0; +} + + +void Reach_Grid::add(const Box& box, uint32 id) +{ + ++total; + int64 lat0, lat1, lon0, lon1; + if (!cell_range(box, lat0, lat1, lon0, lon1)) + { + always.push_back(id); + return; + } + for (int64 i = lat0; i <= lat1; ++i) + { + for (int64 j = lon0; j <= lon1; ++j) + cells[key(i, j)].push_back(id); + } +} + + +void Around_Statement::build_grids() +{ + point_grid.clear(); + for (uint32 i = 0; i < simple_lat_lons.size(); ++i) + { + const Prepared_Point& p = simple_lat_lons[i]; + point_grid.add(Reach_Grid::reach_box(p.lat, p.lon, p.lat, p.lon, radius), i); + } + segment_grid.clear(); + for (uint32 i = 0; i < simple_segments.size(); ++i) + { + const Prepared_Segment& s = simple_segments[i]; + double length = great_circle_dist(s.first_lat, s.first_lon, s.second_lat, s.second_lon); + segment_grid.add(Reach_Grid::reach_box( + s.first_lat, s.first_lon, s.second_lat, s.second_lon, length + radius), i); + } +} + + void add_coord(double lat, double lon, double radius, std::map< Uint32_Index, std::vector< Point_Double > >& radius_lat_lons, std::vector< Prepared_Point >& simple_lat_lons) @@ -833,11 +926,13 @@ void Around_Statement::calc_lat_lons(const Set& input, Statement& query, Resourc if (points.size() == 1) { add_coord(points[0].lat, points[0].lon, radius, radius_lat_lons, simple_lat_lons); + build_grids(); return; } else if (points.size() > 1) { add_way(points, radius, radius_lat_lons, simple_lat_lons, simple_segments); + build_grids(); return; } Request_Context context(&query, rman); @@ -867,6 +962,7 @@ void Around_Statement::calc_lat_lons(const Set& input, Statement& query, Resourc = relation_way_members(context, input.attic_relations, Ranges< Uint31_Index >::global()); add_ways(way_members, Way_Geometry_Store(way_members, query, rman)); } + build_grids(); } @@ -886,9 +982,9 @@ bool Around_Statement::is_inside(double lat, double lon) const } Vec3 coord_cartesian = cartesian(lat, lon); - for (std::vector< Prepared_Segment >::const_iterator - it = simple_segments.begin(); it != simple_segments.end(); ++it) + return segment_grid.any(Reach_Grid::reach_box(lat, lon, lat, lon, radius), [&](uint32 id) { + const Prepared_Segment* it = &simple_segments[id]; if (great_circle_line_dist(*it, coord_cartesian) <= radius) { double gcdist = great_circle_dist @@ -898,37 +994,34 @@ bool Around_Statement::is_inside(double lat, double lon) const great_circle_dist(lat, lon, it->second_lat, it->second_lon) <= limit) return true; } - } - - return false; + return false; + }); } bool Around_Statement::is_inside (double first_lat, double first_lon, double second_lat, double second_lon) const { Prepared_Segment segment(first_lat, first_lon, second_lat, second_lon); + double length = great_circle_dist(first_lat, first_lon, second_lat, second_lon); + Reach_Grid::Box box = Reach_Grid::reach_box(first_lat, first_lon, second_lat, second_lon, + length + radius); - for (std::vector< Prepared_Point >::const_iterator cit = simple_lat_lons.begin(); - cit != simple_lat_lons.end(); ++cit) - { - if (great_circle_line_dist(segment, cit->cartesian) <= radius) - { - double gcdist = great_circle_dist(first_lat, first_lon, second_lat, second_lon); - double limit = sqrt(gcdist*gcdist + radius*radius); - if (great_circle_dist(cit->lat, cit->lon, first_lat, first_lon) <= limit && - great_circle_dist(cit->lat, cit->lon, second_lat, second_lon) <= limit) - return true; - } - } - - for (std::vector< Prepared_Segment >::const_iterator - cit = simple_segments.begin(); cit != simple_segments.end(); ++cit) - { - if (intersect(*cit, segment)) - return true; - } - - return false; + if (point_grid.any(box, [&](uint32 id) + { + const Prepared_Point* cit = &simple_lat_lons[id]; + if (great_circle_line_dist(segment, cit->cartesian) <= radius) + { + double gcdist = great_circle_dist(first_lat, first_lon, second_lat, second_lon); + double limit = sqrt(gcdist*gcdist + radius*radius); + if (great_circle_dist(cit->lat, cit->lon, first_lat, first_lon) <= limit && + great_circle_dist(cit->lat, cit->lon, second_lat, second_lon) <= limit) + return true; + } + return false; + })) + return true; + + return segment_grid.any(box, [&](uint32 id) { return intersect(simple_segments[id], segment); }); } diff --git a/src/overpass_api/statements/around.h b/src/overpass_api/statements/around.h index 1b6380aab..0eefb4e24 100644 --- a/src/overpass_api/statements/around.h +++ b/src/overpass_api/statements/around.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "../data/collect_members.h" #include "../data/utils.h" @@ -59,6 +60,89 @@ struct Prepared_Point }; +/* A coarse lat/lon grid over the points and segments of an around statement's input + set. It only narrows down which entries the exact tests need to look at; it never + decides a match itself. + + Each entry is filed under every cell its "reach box" covers: its bounding box widened + by its own length plus the radius. A point or segment can only pass any of the exact + tests against an entry if their reach boxes overlap (see around.cc), so looking up the + cells of a candidate's reach box finds every entry that could match it. Entries whose + reach box is too large to file, or lies near a pole or the antimeridian, are kept in + a list that every lookup includes. */ +class Reach_Grid +{ +public: + struct Box + { + double south, west, north, east; + bool usable; // false: too large, polar or crossing the antimeridian + }; + + static Box reach_box(double lat1, double lon1, double lat2, double lon2, double reach); + + void clear(); + void add(const Box& box, uint32 id); + + // Calls f(id) for every entry whose cells the box touches, plus the always-checked + // entries. Stops and returns true as soon as f returns true. An id can be passed + // more than once. + template< typename F > + bool any(const Box& box, F f) const; + +private: + static const int max_cells = 64; + static constexpr double cell_size = 0.01; + + static bool cell_range(const Box& box, int64& lat0, int64& lat1, int64& lon0, int64& lon1); + static int64 key(int64 lat_cell, int64 lon_cell) { return (lat_cell << 32) ^ (lon_cell & 0xffffffff); } + + std::unordered_map< int64, std::vector< uint32 > > cells; + std::vector< uint32 > always; + uint32 total = 0; +}; + + +template< typename F > +bool Reach_Grid::any(const Box& box, F f) const +{ + for (uint32 id : always) + { + if (f(id)) + return true; + } + int64 lat0, lat1, lon0, lon1; + if (!cell_range(box, lat0, lat1, lon0, lon1)) + { + // The box is too large to look up cell by cell: check every entry. + for (const auto& cell : cells) + { + for (uint32 id : cell.second) + { + if (f(id)) + return true; + } + } + return false; + } + for (int64 i = lat0; i <= lat1; ++i) + { + for (int64 j = lon0; j <= lon1; ++j) + { + auto it = cells.find(key(i, j)); + if (it == cells.end()) + continue; + for (uint32 id : it->second) + { + if (f(id)) + return true; + } + } + } + return false; +} + + class Around_Statement : public Output_Statement { public: @@ -146,6 +230,10 @@ class Around_Statement : public Output_Statement std::map< Uint32_Index, std::vector< Point_Double > > radius_lat_lons; std::vector< Prepared_Point > simple_lat_lons; std::vector< Prepared_Segment > simple_segments; + Reach_Grid point_grid; // ids are positions in simple_lat_lons + Reach_Grid segment_grid; // ids are positions in simple_segments + + void build_grids(); std::vector< Query_Constraint* > constraints; }; From 4fad46a8a252f271ad0c514f1cab051f84cc58bc Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 07:10:51 +0000 Subject: [PATCH 2/2] around grid: test each entry once per lookup, tighter reach for segments Three changes to the reach grid that narrows down which input points and segments around's exact tests look at: - An entry filed under several cells was passed to the exact test once per cell the lookup box shared with it. Each lookup now passes it once, using a stamp per entry. - When a lookup box was too large to look up cell by cell (a long candidate segment), every cell's list was scanned, so entries were tested once per cell they were filed in, up to 64 times. Such lookups now scan a list of the filed entries once. - A segment's reach box was widened by its own length plus the radius. It is now widened by the radius plus twice the most a great-circle arc of that length can bulge out of its endpoints' bounding box (L^2 tan(latitude) / 8R, a few metres for segments of some km), plus one metre. Beyond 50 km or 80 degrees of latitude the old bound is kept. The comment in around.cc gives the argument that each exact test still only passes when the two reach boxes overlap. The exact tests are unchanged, so results are the same. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT --- src/overpass_api/statements/around.cc | 58 ++++++++++++++++++++++----- src/overpass_api/statements/around.h | 38 ++++++++++++------ 2 files changed, 73 insertions(+), 23 deletions(-) diff --git a/src/overpass_api/statements/around.cc b/src/overpass_api/statements/around.cc index f3dce2808..ccba219eb 100644 --- a/src/overpass_api/statements/around.cc +++ b/src/overpass_api/statements/around.cc @@ -697,23 +697,44 @@ Ranges< Uint32_Index > Around_Statement::calc_ranges(const Set& input, Resource_ //----------------------------------------------------------------------------- -/* Why reach boxes are enough. Let reach(X) be the bounding box of X widened by - (length of X + radius), where a point has length 0. Every exact test only passes - when the two objects' reach boxes overlap: +/* Why reach boxes are enough. Let reach(X) be the bounding box of X's endpoints + widened by segment_reach(len(X), radius): the radius plus twice the most a + great-circle arc of that length can bulge out of its endpoints' bounding box, plus + one metre. A point has length 0. Every exact test only passes when the two objects' + reach boxes overlap: - Point P near segment S (is_inside(lat, lon)): the test requires - great_circle_dist(P, first point of S) <= sqrt(len(S)^2 + radius^2) - <= len(S) + radius, so P lies in reach(S). + great_circle_line_dist(S, P) <= radius, and P within sqrt(len(S)^2 + radius^2) of + both endpoints of S. The second condition keeps P's foot on S's great circle + within sqrt(len(S)^2 + radius^2) - len(S) <= radius beyond either endpoint. So P + is within about 2 * radius of the arc S, hence within the arc's bounding box, + which lies within S's endpoint box widened by the bulge, widened by 2 * radius. + Overlap needs only one of the two boxes to be widened by that much; each is + widened by radius, so together they cover it. - Segment C near point Q (is_inside(segment), first loop): the same test with the - roles swapped puts Q in reach(C). + roles swapped. - Segments S and C intersect (intersect()): the crossing point lies on both arcs, - so within len(S) of S's first point and within len(C) of C's first point; it is - in both reach boxes. + so within each arc's bounding box, which lies within its endpoint box widened by + the bulge. The widening uses a slightly larger distance than needed and the latitude closest to the pole for the longitude scale, so the boxes are conservative. Boxes too close to a pole, crossing the antimeridian or spanning many cells are never pruned. */ +double Reach_Grid::segment_reach(double length, double radius, double lat1, double lat2) +{ + // A great-circle arc of length L deviates in latitude from its endpoints' bounding + // box by at most about L^2 * tan(latitude) / (8 * R). Beyond 50 km, or near a pole, + // keep the much wider bound of the arc's own length. + double poleward = std::max(std::abs(lat1), std::abs(lat2)); + if (length > 50000.0 || poleward >= 80.0) + return length + radius; + const double earth_radius = 10.0*1000*1000/acos(0); // as in great_circle_dist + double bulge = length * length * tan(poleward/90.0*acos(0)) / (8.0 * earth_radius); + return radius + 2.0 * bulge + 1.0; +} + + Reach_Grid::Box Reach_Grid::reach_box(double lat1, double lon1, double lat2, double lon2, double reach) { Box box; @@ -748,6 +769,9 @@ void Reach_Grid::clear() { cells.clear(); always.clear(); + filed.clear(); + seen.clear(); + lookup = 0; total = 0; } @@ -766,6 +790,18 @@ void Reach_Grid::add(const Box& box, uint32 id) for (int64 j = lon0; j <= lon1; ++j) cells[key(i, j)].push_back(id); } + filed.push_back(id); + if (seen.size() <= id) + seen.resize(id + 1, 0); +} + + +bool Reach_Grid::first_visit(uint32 id) const +{ + if (seen[id] == lookup) + return false; + seen[id] = lookup; + return true; } @@ -782,8 +818,8 @@ void Around_Statement::build_grids() { const Prepared_Segment& s = simple_segments[i]; double length = great_circle_dist(s.first_lat, s.first_lon, s.second_lat, s.second_lon); - segment_grid.add(Reach_Grid::reach_box( - s.first_lat, s.first_lon, s.second_lat, s.second_lon, length + radius), i); + segment_grid.add(Reach_Grid::reach_box(s.first_lat, s.first_lon, s.second_lat, s.second_lon, + Reach_Grid::segment_reach(length, radius, s.first_lat, s.second_lat)), i); } } @@ -1004,7 +1040,7 @@ bool Around_Statement::is_inside Prepared_Segment segment(first_lat, first_lon, second_lat, second_lon); double length = great_circle_dist(first_lat, first_lon, second_lat, second_lon); Reach_Grid::Box box = Reach_Grid::reach_box(first_lat, first_lon, second_lat, second_lon, - length + radius); + Reach_Grid::segment_reach(length, radius, first_lat, second_lat)); if (point_grid.any(box, [&](uint32 id) { diff --git a/src/overpass_api/statements/around.h b/src/overpass_api/statements/around.h index 0eefb4e24..6b8713159 100644 --- a/src/overpass_api/statements/around.h +++ b/src/overpass_api/statements/around.h @@ -19,6 +19,7 @@ #ifndef DE__OSM3S___OVERPASS_API__STATEMENTS__AROUND_H #define DE__OSM3S___OVERPASS_API__STATEMENTS__AROUND_H +#include #include #include #include @@ -65,7 +66,8 @@ struct Prepared_Point decides a match itself. Each entry is filed under every cell its "reach box" covers: its bounding box widened - by its own length plus the radius. A point or segment can only pass any of the exact + by the radius plus a small margin for the bulge of a great-circle arc (see + segment_reach). A point or segment can only pass any of the exact tests against an entry if their reach boxes overlap (see around.cc), so looking up the cells of a candidate's reach box finds every entry that could match it. Entries whose reach box is too large to file, or lies near a pole or the antimeridian, are kept in @@ -84,9 +86,12 @@ class Reach_Grid void clear(); void add(const Box& box, uint32 id); - // Calls f(id) for every entry whose cells the box touches, plus the always-checked - // entries. Stops and returns true as soon as f returns true. An id can be passed - // more than once. + // The reach to use for a segment of the given length: the radius plus a margin for + // how far a great-circle arc can bulge out of its endpoints' bounding box. + static double segment_reach(double length, double radius, double lat1, double lat2); + + // Calls f(id) once for every entry whose cells the box touches, plus the + // always-checked entries. Stops and returns true as soon as f returns true. template< typename F > bool any(const Box& box, F f) const; @@ -99,7 +104,14 @@ class Reach_Grid std::unordered_map< int64, std::vector< uint32 > > cells; std::vector< uint32 > always; + std::vector< uint32 > filed; // every id filed in cells, once each uint32 total = 0; + + // An entry filed in several cells is passed to f only once per lookup: seen[id] holds + // the number of the last lookup that passed it. + mutable std::vector< uint32 > seen; + mutable uint32 lookup = 0; + bool first_visit(uint32 id) const; }; @@ -114,17 +126,19 @@ bool Reach_Grid::any(const Box& box, F f) const int64 lat0, lat1, lon0, lon1; if (!cell_range(box, lat0, lat1, lon0, lon1)) { - // The box is too large to look up cell by cell: check every entry. - for (const auto& cell : cells) + // The box is too large to look up cell by cell: check every filed entry once. + for (uint32 id : filed) { - for (uint32 id : cell.second) - { - if (f(id)) - return true; - } + if (f(id)) + return true; } return false; } + if (++lookup == 0) + { + std::fill(seen.begin(), seen.end(), 0); + lookup = 1; + } for (int64 i = lat0; i <= lat1; ++i) { for (int64 j = lon0; j <= lon1; ++j) @@ -134,7 +148,7 @@ bool Reach_Grid::any(const Box& box, F f) const continue; for (uint32 id : it->second) { - if (f(id)) + if (first_visit(id) && f(id)) return true; } }