Overpass API regression suite coverage (Nova Scotia) / overpass_api/statements/polygon_query.cc

overpass_api/statements/polygon_query.cc

336 of 369 instrumented lines run by the suite (91.1%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#include <algorithm>
20#include <iterator>
21#include <sstream>
23#include "../../template_db/block_backend.h"
24#include "../../template_db/random_file.h"
25#include "../core/settings.h"
26#include "../data/abstract_processing.h"
27#include "../data/collect_members.h"
28#include "area_query.h"
29#include "coord_query.h"
30#include "make_area.h"
31#include "polygon_query.h"
32#include "recurse.h"
35//-----------------------------------------------------------------------------
37class Polygon_Constraint : public Query_Constraint
39 public:
40 Query_Filter_Strategy delivers_data(Resource_Manager& rman);
4215254 q Polygon_Constraint(Polygon_Query_Statement& polygon_) : polygon(&polygon_) {}
43 bool get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges);
44 bool get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges);
46 void filter(Resource_Manager& rman, Set& into);
47 void filter(const Statement& query, Resource_Manager& rman, Set& into);
480 virtual ~Polygon_Constraint() {}
50 private:
51 Polygon_Query_Statement* polygon;
52};
55Query_Filter_Strategy Polygon_Constraint::delivers_data(Resource_Manager& rman)
575749 q return (polygon && !polygon->covers_large_area()) ? prefer_ranges : ids_useful;
61bool Polygon_Constraint::get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges)
639354 q ranges = polygon->calc_ranges();
649354 q return true;
68bool Polygon_Constraint::get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges)
6919654 q{
7019654 q Ranges< Uint32_Index > node_ranges = polygon->calc_ranges();
7119654 q ranges = calc_parents(node_ranges);
7219654 q return true;
7319654 q}
76void Polygon_Constraint::filter(Resource_Manager& rman, Set& into)
786254 q polygon->collect_nodes(into.nodes, true);
796254 q if (!into.attic_nodes.empty())
801816 q polygon->collect_nodes(into.attic_nodes, true);
826254 q Ranges< Uint31_Index > ranges;
836254 q get_ranges(rman, ranges);
85 // pre-process ways to reduce the load of the expensive filter
866254 q filter_ways_by_ranges(into.ways, ranges);
876254 q if (!into.attic_ways.empty())
882421 q filter_ways_by_ranges(into.attic_ways, ranges);
90 // pre-filter relations
916254 q filter_relations_by_ranges(into.relations, ranges);
926254 q if (!into.attic_relations.empty())
931413 q filter_relations_by_ranges(into.attic_relations, ranges);
95 //TODO: filter areas
99void Polygon_Constraint::filter(const Statement& query, Resource_Manager& rman, Set& into)
101 //Process ways
1025749 q polygon->collect_ways(into.ways, Way_Geometry_Store(into.ways, query, rman), true, query, rman);
104 //Process relations
1055749 q Request_Context context(&query, rman);
107 // Retrieve all nodes referred by the relations.
1085749 q Ranges< Uint32_Index > node_ranges;
1095749 q get_ranges(rman, node_ranges);
1105749 q Timeless< Uint32_Index, Node_Skeleton > node_members
1115749 q = relation_node_members(context, into.relations, {}, node_ranges, {}, true);
113 // filter for those nodes that are in one of the areas
1145749 q polygon->collect_nodes(node_members.current, false);
116 // Retrieve all ways referred by the relations.
1175749 q Ranges< Uint31_Index > way_ranges;
1185749 q get_ranges(rman, way_ranges);
1195749 q Timeless< Uint31_Index, Way_Skeleton > way_members_
1205749 q = relation_way_members(context, into.relations, {}, way_ranges, {}, true);
1225749 q polygon->collect_ways(way_members_.current, Way_Geometry_Store(way_members_.current, query, rman), false, query, rman);
1245749 q filter_relations_expensive(order_by_id(node_members.current, Order_By_Node_Id()),
1255749 q order_by_id(way_members_.current, Order_By_Way_Id()),
1265749 q into.relations);
128 //Process ways
1295749 q if (!into.attic_ways.empty())
1302421 q polygon->collect_ways(into.attic_ways, Way_Geometry_Store(into.attic_ways, query, rman),
1312421 q true, query, rman);
133 //Process relations
1345749 q if (!into.attic_relations.empty())
136 // Retrieve all nodes referred by the relations.
1371413 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > > node_members
1381413 q = relation_node_members(context, into.attic_relations, node_ranges);
140 // filter for those nodes that are in one of the areas
1411413 q polygon->collect_nodes(node_members, false);
143 // Retrieve all ways referred by the relations.
1441413 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > way_members_
1451413 q = relation_way_members(context, into.attic_relations, way_ranges);
1471413 q polygon->collect_ways(way_members_, Way_Geometry_Store(way_members_, query, rman),
1481413 q false, query, rman);
1501413 q filter_relations_expensive(order_attic_by_id(node_members, Order_By_Node_Id()),
1511413 q order_attic_by_id(way_members_, Order_By_Way_Id()),
1521413 q into.attic_relations);
155 //TODO: filter areas
158//-----------------------------------------------------------------------------
161void add_segment_blocks(std::vector< Aligned_Segment >& segments)
16321654 q std::map< Uint31_Index, std::vector< Aligned_Segment > > by_upper;
164440454 q for (std::vector< Aligned_Segment >::const_iterator it = segments.begin(); it != segments.end(); ++it)
165418854 q by_upper[it->ll_upper_].push_back(*it);
167 /* We use that more northern segments always have bigger indices.
168 Thus we can collect each block's end points and add them, if they do not
169 cancel out, to the next block further northern.*/
17021654 q for (std::map< Uint31_Index, std::vector< Aligned_Segment > >::const_iterator
17163654 q it = by_upper.begin(); it != by_upper.end(); ++it)
17242054 q {
17342054 q std::set< int32 > lons;
17542054 q for (std::vector< Aligned_Segment >::const_iterator it2 = it->second.begin();
176472454 q it2 != it->second.end(); ++it2)
177430454 q {
178430454 q int32 lon_front(ilon(it->first.val() | (it2->ll_lower_a>>32), (it2->ll_lower_a) & 0xffffffffull));
179430454 q int32 lon_back(ilon(it->first.val() | (it2->ll_lower_b>>32), (it2->ll_lower_b) & 0xffffffffull));
180430454 q if (lons.find(lon_front) == lons.end())
181251254 q lons.insert(lon_front);
182179254 q else
183179254 q lons.erase(lon_front);
184430454 q if (lons.find(lon_back) == lons.end())
185190854 q lons.insert(lon_back);
186239654 q else
187239654 q lons.erase(lon_back);
188430454 q }
19042054 q if (lons.empty())
19131254 q continue;
193 // calc lat
19410821 q uint32 lat = ilat(it->first.val(), 0) + 16*65536;
19510821 q int32 lon = ilon(it->first.val(), 0);
19610821 q uint32 northern_ll_upper = ::ll_upper_(lat, lon);
198 // insert lons
19910821 q std::vector< Aligned_Segment >& northern_block(by_upper[northern_ll_upper]);
20022421 q for (std::set< int32 >::const_iterator it2 = lons.begin(); it2 != lons.end(); ++it2)
20111621 q {
20211621 q int32 from(*it2);
20311621 q ++it2;
20411621 q int32 to(*it2);
20511621 q Aligned_Segment segment;
20611621 q segment.ll_upper_ = northern_ll_upper;
20711621 q segment.ll_lower_a = (((uint64)(::ll_upper_(lat, from)) & 0xff)<<32) | ::ll_lower(lat, from);
20811621 q segment.ll_lower_b = (((uint64)(::ll_upper_(lat, to)) & 0xff)<<32) | ::ll_lower(lat, to);
21011621 q northern_block.push_back(segment);
21111621 q }
21210821 q }
21421654 q segments.clear();
21521654 q for (std::map< Uint31_Index, std::vector< Aligned_Segment > >::const_iterator it = by_upper.begin();
21663654 q it != by_upper.end(); ++it)
21742054 q {
218472454 q for (std::vector< Aligned_Segment >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
219430454 q segments.push_back(*it2);
22042054 q }
224Polygon_Query_Statement::Statement_Maker Polygon_Query_Statement::statement_maker;
225Polygon_Query_Statement::Criterion_Maker Polygon_Query_Statement::criterion_maker;
228Statement* Polygon_Query_Statement::Criterion_Maker::create_criterion(const Token_Node_Ptr& tree_it,
229 const std::string& type, const std::string& into,
230 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)
23221654 q uint line_nr = tree_it->line_col.first;
23421654 q if (tree_it->token == ":" && tree_it->rhs)
23521654 q {
23621654 q std::map< std::string, std::string > attributes;
23721654 q attributes["bounds"] = decode_json(tree_it.rhs()->token, error_output);
23821654 q attributes["into"] = into;
23921654 q return new Polygon_Query_Statement(line_nr, attributes, global_settings);
24021654 q }
2420 return 0;
246bool covers_large_area(const std::vector< std::pair< double, double > >& edges)
24821654 q double max_lat = -100.;
24921654 q double min_lat = 100.;
25021654 q double max_lon = -200.;
25121654 q double min_lon = 200.;
253393254 q for (std::vector< std::pair< double, double > >::const_iterator it = edges.begin(); it != edges.end(); ++it)
254371654 q {
255371654 q max_lat = std::max(max_lat, it->first);
256371654 q min_lat = std::min(min_lat, it->first);
257371654 q max_lon = std::max(max_lon, it->second);
258371654 q min_lon = std::min(min_lon, it->second);
259371654 q }
26121654 q if (max_lat < min_lat || max_lon < min_lon)
2620 return false;
26321654 q return (max_lat - min_lat) * (max_lon - min_lon) > 1.;
267Polygon_Query_Statement::Polygon_Query_Statement
268 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
26921654 q : Output_Statement(line_number_)
27121654 q std::map< std::string, std::string > attributes;
27321654 q attributes["into"] = "_";
27421654 q attributes["bounds"] = "";
27621654 q eval_attributes_array(get_name(), attributes, input_attributes);
27821654 q set_output(attributes["into"]);
280 //convert bounds
28121654 q std::istringstream in(attributes["bounds"]);
282393254 q while (in.good())
283371654 q {
284371654 q double lat, lon;
285371654 q in>>lat;
286371654 q if (!in.good())
2870 {
2880 add_static_error("For the attribute \"bounds\" of the element \"polygon-query\""
2890 " an even number of float values must be provided.");
2900 break;
2910 }
292371654 q in>>lon;
293371654 q edges.push_back(std::make_pair(lat, lon));
294371654 q }
29621654 q if (edges.size() < 3)
2970 add_static_error("For the attribute \"bounds\" of the element \"polygon-query\""
2980 " at least 3 lat/lon float value pairs must be provided.");
30021654 q for (std::vector< std::pair< double, double > >::const_iterator it = edges.begin();
301393254 q it != edges.end(); ++it)
302371654 q {
303371654 q if (it->first < -90.0 || it->first > 90.0)
3040 add_static_error("For the attribute \"bounds\" of the element \"polygon-query\""
3050 " the only allowed values for latitude are floats between -90.0 and 90.0.");
307371654 q if (it->second < -180.0 || it->second > 180.0)
3080 add_static_error("For the attribute \"bounds\" of the element \"polygon-query\""
3090 " the only allowed values for longitude are floats between -180.0 and 180.0.");
310371654 q }
31221654 q covers_large_area_ = ::covers_large_area(edges);
314371654 q for (unsigned int i = 1; i < edges.size(); ++i)
315350054 q Area::calc_aligned_segments(segments, edges[i-1].first, edges[i-1].second, edges[i].first, edges[i].second);
31621654 q if (!edges.empty())
31721654 q Area::calc_aligned_segments(
31821654 q segments, edges.back().first, edges.back().second, edges[0].first, edges[0].second);
31921654 q std::sort(segments.begin(), segments.end());
32121654 q add_segment_blocks(segments);
325Polygon_Query_Statement::~Polygon_Query_Statement()
32721654 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin();
32836354 q it != constraints.end(); ++it)
32914749 q delete *it;
333Ranges< Uint32_Index > Polygon_Query_Statement::calc_ranges()
33528954 q Ranges< Uint32_Index > result;
336564454 q for (std::vector< Aligned_Segment >::const_iterator it = segments.begin(); it != segments.end(); ++it)
337535554 q result.push_back(it->ll_upper_, it->ll_upper_ + 0x100);
33828954 q return result;
342template< typename Node_Skeleton >
343void Polygon_Query_Statement::collect_nodes(std::map< Uint32_Index, std::vector< Node_Skeleton > >& nodes,
344 bool add_border)
34615154 q std::vector< Aligned_Segment >::const_iterator area_it = segments.begin();
34715154 q typename std::map< Uint32_Index, std::vector< Node_Skeleton > >::iterator nodes_it = nodes.begin();
34915154 q uint32 current_idx(0);
35144354 q while (area_it != segments.end())
35229254 q {
35329254 q current_idx = area_it->ll_upper_;
35529254 q std::vector< Area_Block > areas;
356329154 q while (area_it != segments.end() && area_it->ll_upper_ == current_idx)
357299954 q {
358299954 q Area_Block block;
359299954 q block.coors.push_back(area_it->ll_lower_a);
360299954 q block.coors.push_back(area_it->ll_lower_b);
361299954 q areas.push_back(block);
362299954 q ++area_it;
363299954 q }
36529254 q while (nodes_it != nodes.end() && nodes_it->first.val() < current_idx)
3660 {
3670 nodes_it->second.clear();
3680 ++nodes_it;
3690 }
3702043754 q while (nodes_it != nodes.end() &&
3712043754 q (nodes_it->first.val() & 0xffffff00) == current_idx)
3722014538 q {
3732014538 q std::vector< Node_Skeleton > into;
3742014538 q for (typename std::vector< Node_Skeleton >::const_iterator iit = nodes_it->second.begin();
375136659838 q iit != nodes_it->second.end(); ++iit)
376134645338 q {
377134645338 q uint32 ilat((::lat(nodes_it->first.val(), iit->ll_lower)
378134645338 q + 91.0)*10000000+0.5);
379134645338 q int32 ilon(::lon(nodes_it->first.val(), iit->ll_lower)*10000000
380134645338 q + (::lon(nodes_it->first.val(), iit->ll_lower) > 0 ? 0.5 : -0.5));
382134645338 q int inside = 0;
383134645338 q for (std::vector< Area_Block >::const_iterator it = areas.begin();
3842034603438 q it != areas.end(); ++it)
3851899958238 q {
3861899958238 q int check(Coord_Query_Statement::check_area_block(current_idx, *it, ilat, ilon));
3871899958238 q if (check == Coord_Query_Statement::HIT && add_border)
38911 q inside = 1;
39011 q break;
3921899958138 q else if (check != 0)
39389476537 q inside ^= check;
3941899958238 q }
395134645338 q if (inside)
39614971035 q into.push_back(*iit);
397134645338 q }
3982014538 q nodes_it->second.swap(into);
3992014538 q ++nodes_it;
4002014538 q }
40129254 q }
40215154 q while (nodes_it != nodes.end())
4030 {
4040 nodes_it->second.clear();
4050 ++nodes_it;
4060 }
410template< typename Way_Skeleton >
411void Polygon_Query_Statement::collect_ways
412 (std::map< Uint31_Index, std::vector< Way_Skeleton > >& ways,
413 const Way_Geometry_Store& way_geometries,
414 bool add_border, const Statement& query, Resource_Manager& rman)
41615249 q std::map< uint32, std::vector< std::pair< uint32, Way::Id_Type > > > way_coords_to_id;
4172912649 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); it != ways.end(); ++it)
4182897444 q {
41918050744 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
42015153343 q {
42115153343 q std::vector< Quad_Coord > coords = way_geometries.get_geometry(*it2);
422304537643 q for (std::vector< Quad_Coord >::const_iterator it3 = coords.begin(); it3 != coords.end(); ++it3)
423289384343 q way_coords_to_id[it3->ll_upper].push_back(std::make_pair(it3->ll_lower, it2->id));
42415153343 q }
4252897444 q }
42715249 q std::map< uint32, std::vector< std::pair< uint32, Way::Id_Type > > >::const_iterator nodes_it = way_coords_to_id.begin();
42915249 q std::vector< Aligned_Segment >::const_iterator area_it = segments.begin();
43115249 q std::map< Uint31_Index, std::vector< Area_Block > > way_segments;
4322912649 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); it != ways.end(); ++it)
4332897444 q {
43418050744 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
43515153343 q add_way_to_area_blocks(way_geometries.get_geometry(*it2), it2->id.val(), way_segments);
4362897444 q }
43815249 q std::map< Way::Id_Type, bool > ways_inside;
440 // Fill node_status with the area related status of each node and segment
44115249 q uint32 current_idx(0);
44245449 q while (area_it != segments.end())
44330249 q {
44430249 q current_idx = area_it->ll_upper_;
44630249 q std::vector< Area_Block > areas;
447337749 q while (area_it != segments.end() && area_it->ll_upper_ == current_idx)
448307549 q {
449307549 q Area_Block block;
450307549 q block.coors.push_back(area_it->ll_lower_a);
451307549 q block.coors.push_back(area_it->ll_lower_b);
452307549 q areas.push_back(block);
453307549 q ++area_it;
454307549 q }
456 // check nodes
4575235149 q while (nodes_it != way_coords_to_id.end() && nodes_it->first < current_idx)
4585204943 q ++nodes_it;
4592565349 q while (nodes_it != way_coords_to_id.end() &&
4602565349 q (nodes_it->first & 0xffffff00) == current_idx)
4612535142 q {
4622535142 q std::vector< std::pair< uint32, Way::Id_Type > > into;
4632535142 q for (std::vector< std::pair< uint32, Way::Id_Type > >::const_iterator iit = nodes_it->second.begin();
464195825742 q iit != nodes_it->second.end(); ++iit)
465193290642 q {
466193290642 q uint32 ilat = ::ilat(nodes_it->first, iit->first);
467193290642 q int32 ilon = ::ilon(nodes_it->first, iit->first);
469193290642 q int inside = 0;
4701934721042 q for (std::vector< Area_Block >::const_iterator it2 = areas.begin(); it2 != areas.end();
4711741430442 q ++it2)
4721741430442 q {
4731741430442 q int check(Coord_Query_Statement::check_area_block(current_idx, *it2, ilat, ilon));
4741741430442 q if (check == Coord_Query_Statement::HIT)
4750 {
4760 inside = Coord_Query_Statement::HIT;
4770 break;
4780 }
4791741430442 q else
4801741430442 q inside ^= check;
4811741430442 q }
482193290642 q if (inside & (Coord_Query_Statement::TOGGLE_EAST | Coord_Query_Statement::TOGGLE_WEST))
48315883639 q ways_inside[iit->second] = true;
484193290642 q }
4852535142 q ++nodes_it;
4862535142 q }
488 // check segments
48930249 q for (std::vector< Area_Block >::const_iterator sit = way_segments[Uint31_Index(current_idx)].begin();
49016578449 q sit != way_segments[Uint31_Index(current_idx)].end(); ++sit)
49116548242 q {
49216548242 q int inside = 0;
49316548242 q for (std::vector< Area_Block >::const_iterator it = areas.begin();
494159671342 q it != areas.end(); ++it)
495143296342 q {
496 // If an area segment intersects this way segment in the inner of the way,
497 // the way is contained in the area.
498 // The endpoints are properly handled via the point-in-area test
499 // Check additionally the middle of the segment to also get segments
500 // that run through the area
501143296342 q int intersect = intersects_inner(*sit, *it);
502143296342 q if (intersect == Coord_Query_Statement::INTERSECT)
503173238 q {
504173238 q ways_inside[Way::Id_Type(sit->id)] = true;
505173238 q break;
506173238 q }
507143123142 q else if (intersect == Coord_Query_Statement::HIT)
5080 {
5090 if (add_border)
5100 ways_inside[Way::Id_Type(sit->id)] = true;
5110 else
5120 inside = 0;
5130 break;
5140 }
515143123142 q has_inner_points(*sit, *it, inside);
516143123142 q }
51716548242 q if ((inside && (!(inside & Coord_Query_Statement::HIT))) ||
51816548242 q (inside && add_border))
519655039 q ways_inside[Way::Id_Type(sit->id)] = true;
52016548242 q }
52130249 q }
52315249 q std::map< Uint31_Index, std::vector< Way_Skeleton > > result;
525 // Mark ways as found that intersect the area border
5262912649 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); it != ways.end(); ++it)
5272897444 q {
5282897444 q std::vector< Way_Skeleton > cur_result;
52918050744 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
53015153343 q {
53115153343 q if (ways_inside[it2->id])
5321670040 q {
5331670040 q cur_result.push_back(*it2);
5341670040 q it2->id = Way::Id_Type(0u);
5351670040 q }
53615153343 q }
5372897444 q result[it->first].swap(cur_result);
5382897444 q }
54015249 q result.swap(ways);
544void Polygon_Query_Statement::execute(Resource_Manager& rman)
54655 q Set into;
54855 q Polygon_Constraint constraint(*this);
54955 q Ranges< Uint32_Index > ranges;
55055 q constraint.get_ranges(rman, ranges);
55155 q get_elements_from_db< Uint32_Index, Node_Skeleton >(ranges, *this, rman)
55255 q .swap(into.nodes, into.attic_nodes);
55355 q constraint.filter(rman, into);
55455 q filter_attic_elements(rman, rman.get_desired_timestamp(), into.nodes, into.attic_nodes);
55655 q transfer_output(rman, into);
55755 q rman.health_check(*this);
561Query_Constraint* Polygon_Query_Statement::get_query_constraint()
56314749 q constraints.push_back(new Polygon_Constraint(*this));
56414749 q return constraints.back();