Overpass API regression suite coverage (Saxony) / overpass_api/statements/area_query.cc

overpass_api/statements/area_query.cc

673 of 700 instrumented lines run by the suite (96.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 <cctype>
20#include <fstream>
21#include <iostream>
22#include <map>
23#include <set>
24#include <sstream>
25#include <string>
26#include <math.h>
27#include <stdlib.h>
28#include <vector>
30#include "../../template_db/block_backend.h"
31#include "../core/index_computations.h"
32#include "../data/collect_members.h"
33#include "../data/tilewise_geometry.h"
34#include "area_query.h"
35#include "coord_query.h"
36#include "make_area.h"
37#include "recurse.h"
40class Area_Constraint : public Query_Constraint
42 public:
4318055 q Area_Constraint(Area_Query_Statement& area_) : area(&area_) {}
45 Query_Filter_Strategy delivers_data(Resource_Manager& rman);
47 bool get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges);
48 bool get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges);
50 void filter(Resource_Manager& rman, Set& into);
51 void filter(const Statement& query, Resource_Manager& rman, Set& into);
520 virtual ~Area_Constraint() {}
54 private:
55 Area_Query_Statement* area;
56};
59Ranges< Uint32_Index > copy_discrete_to_area_ranges(const std::set< Uint31_Index >& area_blocks_req)
6029855 q{
6129855 q Ranges< Uint32_Index > result;
621717255 q for (std::set< Uint31_Index >::const_iterator it = area_blocks_req.begin(); it != area_blocks_req.end(); ++it)
631687448 q result.push_back(Uint32_Index(it->val()), Uint32_Index((it->val()) + 0x100));
6429855 q result.sort();
6529855 q return result;
6629855 q}
69bool Area_Constraint::get_ranges
70 (Resource_Manager& rman, Ranges< Uint32_Index >& ranges)
7129855 q{
7229855 q std::set< Uint31_Index > area_blocks_req;
7329855 q if (area->areas_from_input())
7426850 q {
7526850 q const Set* input = rman.get_set(area->get_input());
7626850 q if (!input)
770 return true;
7926850 q area->get_ranges(input->ways, input->areas, area_blocks_req, rman);
8026850 q ranges = way_covered_indices(input->ways, input->attic_ways);
8126850 q }
82305 q else
83305 q {
84305 q area->get_ranges(area_blocks_req, rman);
85305 q ranges = Ranges< Uint32_Index >();
86305 q }
8829855 q ranges.union_(copy_discrete_to_area_ranges(area_blocks_req)).swap(ranges);
9029855 q return true;
9129855 q}
94bool Area_Constraint::get_ranges
95 (Resource_Manager& rman, Ranges< Uint31_Index >& ranges)
9619855 q{
9719855 q Ranges< Uint32_Index > node_ranges;
9819855 q this->get_ranges(rman, node_ranges);
9919855 q ranges = calc_parents(node_ranges);
10019855 q return true;
104void Area_Constraint::filter(Resource_Manager& rman, Set& into)
1066555 q Ranges< Uint31_Index > ranges;
1076555 q get_ranges(rman, ranges);
109 // pre-process ways to reduce the load of the expensive filter
1106555 q filter_ways_by_ranges(into.ways, ranges);
1116555 q if (!into.attic_ways.empty())
1121311 q filter_ways_by_ranges(into.attic_ways, ranges);
114 // pre-filter relations
1156555 q filter_relations_by_ranges(into.relations, ranges);
1166555 q if (!into.attic_relations.empty())
11744 q filter_relations_by_ranges(into.attic_relations, ranges);
119 //TODO: filter areas
123template< typename Node_Skeleton >
124std::map< Uint32_Index, std::vector< Node_Skeleton > > nodes_contained_in(
125 const Set* potential_areas, bool accept_border, const Statement& stmt, Resource_Manager& rman,
126 const std::map< Uint32_Index, std::vector< Node_Skeleton > >& nodes)
12813955 q if (!potential_areas)
129124 q return std::map< Uint32_Index, std::vector< Node_Skeleton > >();
13112751 q Tilewise_Const_Area_Iterator tai(potential_areas->ways, potential_areas->attic_ways, stmt, rman);
13212751 q std::map< Uint32_Index, std::vector< Node_Skeleton > > result;
13412751 q for (typename std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator iit = nodes.begin();
1351863351 q iit != nodes.end(); ++iit)
1361850631 q {
137365579431 q while (!tai.is_end() && tai.get_idx().val() < iit->first.val())
138363728816 q tai.next();
1391850631 q if (tai.is_end() || iit->first.val() < tai.get_idx().val())
1401757130 q continue;
14293512 q std::vector< Node_Skeleton >& result_block = result[iit->first];
14429520012 q for (typename std::vector< Node_Skeleton >::const_iterator it = iit->second.begin(); it != iit->second.end(); ++it)
14529426511 q {
14629426511 q Tilewise_Area_Iterator::Relative_Position relpos = tai.rel_position(iit->first.val(), it->ll_lower, true);
147// std::cout<<"Id "<<it->id.val()<<' '<<relpos<<'\n';
14829426511 q if ((accept_border && relpos != Tilewise_Area_Iterator::outside)
14929426511 q || relpos == Tilewise_Area_Iterator::inside)
150143339 q result_block.push_back(*it);
15129426511 q }
15293512 q }
154// while (!tai.is_end())
155// {
156// const std::map< const Way_Skeleton*, Tilewise_Area_Iterator::Index_Block >& way_blocks = tai.get_obj();
157// for (std::map< const Way_Skeleton*, Tilewise_Area_Iterator::Index_Block >::const_iterator bit = way_blocks.begin();
158// bit != way_blocks.end(); ++bit)
159// {
160// std::cout<<"Index "<<std::hex<<tai.get_idx().val()
161// <<" ("<<std::dec<<lat(tai.get_idx().val(), 0u)<<' '<<lon(tai.get_idx().val(), 0u)<<") "
162// <<std::dec<<bit->first->id.val()<<": "<<bit->second.sw_is_inside;
163// for (std::vector< Tilewise_Area_Iterator::Entry >::const_iterator it = bit->second.segments.begin();
164// it != bit->second.segments.end(); ++it)
165// std::cout<<" ("<<it->ilat_west<<' '<<it->ilon_west<<' '<<it->ilat_east<<' '<<it->ilon_east<<')';
166// std::cout<<'\n';
167// }
168// for (uint i = 0; i < 16; ++i)
169// {
170// std::cout<<" ";
171// for (uint j = 0; j < 16; ++j)
172// std::cout<<tai.rel_position(tai.get_idx().val(), ll_lower(0xf000 - i*0x1000, j*0x1000));
173// std::cout<<'\n';
174// }
175// tai.next();
176// }
17812751 q return result;
182std::map< Uint31_Index, std::vector< Way_Skeleton > > ways_contained_in(
183 const Set* potential_areas, const Statement& stmt, Resource_Manager& rman,
184 const std::map< Uint31_Index, std::vector< Way_Skeleton > >& ways)
18613055 q if (!potential_areas)
187104 q return std::map< Uint31_Index, std::vector< Way_Skeleton > >();
18912051 q Tilewise_Const_Area_Iterator tai(potential_areas->ways, potential_areas->attic_ways, stmt, rman);
19012051 q std::map< Uint31_Index, std::vector< Way_Skeleton > > result;
19212051 q Tilewise_Way_Iterator twi(ways, std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >(), stmt, rman);
19315286751 q while (!twi.is_end())
19415274730 q {
19518874830 q while (!tai.is_end() && tai.get_idx() < twi.get_idx())
1963600117 q tai.next();
19715274730 q if (tai.is_end() || twi.get_idx() < tai.get_idx())
19815029730 q {
19915029730 q twi.next();
20015029730 q continue;
20115029730 q }
203245013 q const std::map< Tilewise_Way_Iterator::Status_Ref< Way_Skeleton >*, Tilewise_Way_Iterator::Index_Block >& obj = twi.get_current_obj();
204245013 q for (std::map< Tilewise_Way_Iterator::Status_Ref< Way_Skeleton >*, Tilewise_Way_Iterator::Index_Block >::const_iterator it = obj.begin();
2054446413 q it != obj.end(); ++it)
2064201413 q {
2074201413 q if (it->first->status == Tilewise_Area_Iterator::outside)
2083781113 q {
2093781113 q it->first->status = tai.rel_position(it->second.segments, true);
2103781113 q if (it->first->status != Tilewise_Area_Iterator::outside)
211738610 q result[it->first->idx].push_back(*it->first->skel);
2123781113 q }
2134201413 q }
215245013 q twi.next();
216245013 q }
21812051 q return result;
222std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > ways_contained_in(
223 const Set* potential_areas, const Statement& stmt, Resource_Manager& rman,
224 const std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >& ways)
2261612 q if (!potential_areas)
22732 q return std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >();
2291310 q Tilewise_Const_Area_Iterator tai(potential_areas->ways, potential_areas->attic_ways, stmt, rman);
2301310 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > result;
2321310 q Tilewise_Way_Iterator twi(std::map< Uint31_Index, std::vector< Way_Skeleton > >(), ways, stmt, rman);
233745410 q while (!twi.is_end())
23474416 q {
2357144066 q while (!tai.is_end() && tai.get_idx() < twi.get_idx())
2367069652 q tai.next();
23774416 q if (tai.is_end() || twi.get_idx() < tai.get_idx())
23874116 q {
23974116 q twi.next();
24074116 q continue;
24174116 q }
243302 q const std::map< Tilewise_Way_Iterator::Status_Ref< Attic< Way_Skeleton > >*, Tilewise_Way_Iterator::Index_Block >& obj = twi.get_attic_obj();
244302 q for (std::map< Tilewise_Way_Iterator::Status_Ref< Attic< Way_Skeleton > >*, Tilewise_Way_Iterator::Index_Block >::const_iterator
24518782 q it = obj.begin(); it != obj.end(); ++it)
24618482 q {
24718482 q if (it->first->status == Tilewise_Area_Iterator::outside)
24816562 q {
24916562 q it->first->status = tai.rel_position(it->second.segments, true);
25016562 q if (it->first->status != Tilewise_Area_Iterator::outside)
25111942 q result[it->first->idx].push_back(*it->first->skel);
25216562 q }
25318482 q }
255302 q twi.next();
256302 q }
2581310 q return result;
262void Area_Constraint::filter(const Statement& query, Resource_Manager& rman, Set& into)
2646555 q std::set< Uint31_Index > area_blocks_req;
2656555 q const Set* input = rman.get_set(area->get_input());
2666555 q if (area->areas_from_input())
2685950 q if (input)
2695950 q area->get_ranges(input->ways, input->areas, area_blocks_req, rman);
27165 q else
27265 q area->get_ranges(area_blocks_req, rman);
274 //Process nodes
2766555 q std::map< Uint32_Index, std::vector< Node_Skeleton > > nodes_in_wr_areas
2776555 q = nodes_contained_in(input, true, query, rman, into.nodes);
2786555 q indexed_set_difference(into.nodes, nodes_in_wr_areas);
2796555 q area->collect_nodes(into.nodes, area_blocks_req, true, rman);
2806555 q indexed_set_union(into.nodes, nodes_in_wr_areas);
2816555 q }
283 //Process ways
2856555 q std::map< Uint31_Index, std::vector< Way_Skeleton > > ways_in_wr_areas
2866555 q = ways_contained_in(input, query, rman, into.ways);
2876555 q indexed_set_difference(into.ways, ways_in_wr_areas);
2886555 q area->collect_ways(Way_Geometry_Store(into.ways, query, rman),
2896555 q into.ways, area_blocks_req, false, query, rman);
2906555 q indexed_set_union(into.ways, ways_in_wr_areas);
293 //Process relations
2946555 q Request_Context context(&query, rman);
296 // Retrieve all nodes referred by the relations.
2976555 q Ranges< Uint32_Index > node_ranges;
2986555 q get_ranges(rman, node_ranges);
2996555 q Timeless< Uint32_Index, Node_Skeleton > node_members
3006555 q = relation_node_members(context, into.relations, {}, node_ranges, {}, true);
302 // filter for those nodes that are in one of the areas
3046555 q std::map< Uint32_Index, std::vector< Node_Skeleton > > nodes_in_wr_areas
3056555 q = nodes_contained_in(input, false, query, rman, node_members.current);
3066555 q indexed_set_difference(node_members.current, nodes_in_wr_areas);
3076555 q area->collect_nodes(node_members.current, area_blocks_req, false, rman);
3086555 q indexed_set_union(node_members.current, nodes_in_wr_areas);
3096555 q }
311 // Retrieve all ways referred by the relations.
3126555 q Ranges< Uint31_Index > way_ranges;
3136555 q get_ranges(rman, way_ranges);
3146555 q Timeless< Uint31_Index, Way_Skeleton > way_members_
3156555 q = relation_way_members(context, into.relations, {}, way_ranges, {}, true);
317 // Filter for those ways that are in one of the areas
3196555 q std::map< Uint31_Index, std::vector< Way_Skeleton > > ways_in_wr_areas
3206555 q = ways_contained_in(input, query, rman, way_members_.current);
3216555 q indexed_set_difference(way_members_.current, ways_in_wr_areas);
3226555 q area->collect_ways(Way_Geometry_Store(way_members_.current, query, rman),
3236555 q way_members_.current, area_blocks_req, false, query, rman);
3246555 q indexed_set_union(way_members_.current, ways_in_wr_areas);
3276555 q filter_relations_expensive(order_by_id(node_members.current, Order_By_Node_Id()),
3286555 q order_by_id(way_members_.current, Order_By_Way_Id()),
3296555 q into.relations);
331 //Process nodes
3326555 q if (!into.attic_nodes.empty())
33455 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > > nodes_in_wr_areas
33555 q = nodes_contained_in(input, true, query, rman, into.attic_nodes);
33655 q indexed_set_difference(into.attic_nodes, nodes_in_wr_areas);
33755 q area->collect_nodes(into.attic_nodes, area_blocks_req, true, rman);
33855 q indexed_set_union(into.attic_nodes, nodes_in_wr_areas);
33955 q }
341 //Process ways
3426555 q if (!into.attic_ways.empty())
3441210 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > ways_in_wr_areas
3451210 q = ways_contained_in(input, query, rman, into.attic_ways);
3461210 q indexed_set_difference(into.attic_ways, ways_in_wr_areas);
3471210 q area->collect_ways(Way_Geometry_Store(into.attic_ways, query, rman),
3481210 q into.attic_ways, area_blocks_req, false, query, rman);
3491210 q indexed_set_union(into.attic_ways, ways_in_wr_areas);
352 //Process relations
3536555 q if (!into.attic_relations.empty())
355 // Retrieve all nodes referred by the relations.
35644 q Ranges< Uint32_Index > node_ranges;
35744 q get_ranges(rman, node_ranges);
35844 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > > node_members
35944 q = relation_node_members(context, into.attic_relations, node_ranges);
361 // filter for those nodes that are in one of the areas
36344 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > > nodes_in_wr_areas
36444 q = nodes_contained_in(input, false, query, rman, node_members);
36544 q indexed_set_difference(node_members, nodes_in_wr_areas);
36644 q area->collect_nodes(node_members, area_blocks_req, false, rman);
36744 q indexed_set_union(node_members, nodes_in_wr_areas);
36844 q }
370 // Retrieve all ways referred by the relations.
37144 q Ranges< Uint31_Index > way_ranges;
37244 q get_ranges(rman, way_ranges);
37344 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > way_members_
37444 q = relation_way_members(context, into.attic_relations, way_ranges);
376 // Filter for those ways that are in one of the areas
37844 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > ways_in_wr_areas
37944 q = ways_contained_in(input, query, rman, way_members_);
38044 q indexed_set_difference(way_members_, ways_in_wr_areas);
38144 q area->collect_ways(Way_Geometry_Store(way_members_, query, rman),
38244 q way_members_, area_blocks_req, false, query, rman);
38344 q indexed_set_union(way_members_, ways_in_wr_areas);
38644 q filter_relations_expensive(order_attic_by_id(node_members, Order_By_Node_Id()),
38744 q order_attic_by_id(way_members_, Order_By_Way_Id()),
38844 q into.attic_relations);
391 //TODO: filter areas
395//-----------------------------------------------------------------------------
397bool Area_Query_Statement::is_used_ = false;
400Area_Query_Statement::Statement_Maker Area_Query_Statement::statement_maker;
401Area_Query_Statement::Criterion_Maker Area_Query_Statement::criterion_maker;
404Statement* Area_Query_Statement::Criterion_Maker::create_criterion(const Token_Node_Ptr& input_tree,
405 const std::string& type, const std::string& into,
406 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)
40824055 q Token_Node_Ptr tree_it = input_tree;
40924055 q uint line_nr = tree_it->line_col.first;
41024055 q std::string from = "_";
41124055 q std::string ref;
41324055 q if (tree_it->token == ":" && tree_it->rhs)
414205 q {
415205 q ref = tree_it.rhs()->token;
416205 q tree_it = tree_it.lhs();
417205 q }
41924055 q if (tree_it->token == "." && tree_it->rhs)
42022050 q from = tree_it.rhs()->token;
42224055 q std::map< std::string, std::string > attributes;
42324055 q attributes["from"] = from;
42424055 q attributes["into"] = into;
42524055 q attributes["ref"] = ref;
42624055 q return new Area_Query_Statement(line_nr, attributes, global_settings);
430Area_Query_Statement::Area_Query_Statement
431 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
43224055 q : Output_Statement(line_number_), area_blocks_req_filled(false)
43424055 q is_used_ = true;
43624055 q std::map< std::string, std::string > attributes;
43824055 q attributes["from"] = "_";
43924055 q attributes["into"] = "_";
44024055 q attributes["ref"] = "";
44224055 q eval_attributes_array(get_name(), attributes, input_attributes);
44424055 q input = attributes["from"];
44524055 q set_output(attributes["into"]);
44624055 q submitted_id = atoll(attributes["ref"].c_str());
44724055 q if (submitted_id <= 0 && attributes["ref"] != "")
4480 {
4490 std::ostringstream temp;
4500 temp<<"For the attribute \"ref\" of the element \"area-query\""
4510 <<" the only allowed values are positive integers.";
4520 add_static_error(temp.str());
4530 }
45424055 q else if (submitted_id > 0)
455205 q area_id.push_back(Area_Skeleton::Id_Type(submitted_id));
458Area_Query_Statement::~Area_Query_Statement()
46024055 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin();
46142055 q it != constraints.end(); ++it)
46218055 q delete *it;
466unsigned int Area_Query_Statement::count_ranges(Resource_Manager& rman)
46865 q if (!area_blocks_req_filled)
46955 q fill_ranges(rman);
47165 q return area_blocks_req.size();
475void Area_Query_Statement::get_ranges(std::set< Uint31_Index >& area_blocks_req, Resource_Manager& rman)
477365 q if (!area_blocks_req_filled)
4780 fill_ranges(rman);
480365 q area_blocks_req = this->area_blocks_req;
484void Area_Query_Statement::fill_ranges(Resource_Manager& rman)
48655 q Block_Backend< Uint31_Index, Area_Skeleton > area_locations_db
48755 q (rman.get_area_transaction()->data_index(area_settings().AREAS));
48855 q for (Block_Backend< Uint31_Index, Area_Skeleton >::Flat_Iterator
48955 q it(area_locations_db.flat_begin());
490351205 q !(it == area_locations_db.flat_end()); ++it)
491351155 q {
492351155 q if (binary_search(area_id.begin(), area_id.end(), it.object().id))
49444 q for (std::vector< uint32 >::const_iterator it2(it.object().used_indices.begin());
49594 q it2 != it.object().used_indices.end(); ++it2)
49654 q area_blocks_req.insert(Uint31_Index(*it2));
498351155 q }
49955 q area_blocks_req_filled = true;
503void Area_Query_Statement::get_ranges
504 (const std::map< Uint31_Index, std::vector< Way_Skeleton > >& input_ways,
505 const std::map< Uint31_Index, std::vector< Area_Skeleton > >& input_areas,
506 std::set< Uint31_Index >& area_blocks_req,
507 Resource_Manager& rman)
50932750 q way_areas_id.clear();
51032750 q for (std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator it = input_ways.begin();
511113950 q it != input_ways.end(); ++it)
51281226 q {
513165126 q for (std::vector< Way_Skeleton >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
51483926 q {
51583926 q if (!it2->nds.empty() && it2->nds.front() == it2->nds.back())
51683926 q way_areas_id.push_back(it2->id);
51783926 q }
51881226 q }
51932750 q std::sort(way_areas_id.begin(), way_areas_id.end());
52132750 q area_id.clear();
52232750 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = input_areas.begin();
52398550 q it != input_areas.end(); ++it)
52465844 q {
525170444 q for (std::vector< Area_Skeleton >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
526104644 q {
527104644 q area_id.push_back(it2->id);
529104644 q for (std::vector< uint32 >::const_iterator it3(it2->used_indices.begin());
5304116644 q it3 != it2->used_indices.end(); ++it3)
5314012044 q area_blocks_req.insert(Uint31_Index(*it3));
532104644 q }
53365844 q }
53432750 q std::sort(area_id.begin(), area_id.end());
538Query_Filter_Strategy Area_Constraint::delivers_data(Resource_Manager& rman)
5406555 q if (!area->areas_from_input())
54165 q return (area->count_ranges(rman) < 12) ? prefer_ranges : ids_useful;
5425950 q else
5445950 q const Set* input = rman.get_set(area->get_input());
5455950 q if (!input)
5460 return prefer_ranges;
548 // Count the indicies of the input areas
5495950 q int counter = 0;
5515950 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = input->areas.begin();
55218350 q it != input->areas.end(); ++it)
55312444 q {
55432244 q for (std::vector< Area_Skeleton >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
55519844 q counter += it2->used_indices.size();
55612444 q }
5585950 q return (counter <= 12) ? prefer_ranges : ids_useful;
563template< typename Node_Skeleton >
564void Area_Query_Statement::collect_nodes
565 (std::map< Uint32_Index, std::vector< Node_Skeleton > >& nodes,
566 const std::set< Uint31_Index >& req, bool add_border,
567 Resource_Manager& rman)
56913955 q Block_Backend< Uint31_Index, Area_Block, std::set< Uint31_Index >::const_iterator > area_blocks_db
57013955 q (rman.get_area_transaction()->data_index(area_settings().AREA_BLOCKS));
57113955 q Block_Backend< Uint31_Index, Area_Block, std::set< Uint31_Index >::const_iterator >::Discrete_Iterator
57213955 q area_it(area_blocks_db.discrete_begin(req.begin(), req.end()));
57413955 q typename std::map< Uint32_Index, std::vector< Node_Skeleton > >::iterator nodes_it = nodes.begin();
57613955 q uint32 loop_count = 0;
57713955 q uint32 current_idx(0);
578896955 q while (!(area_it == area_blocks_db.discrete_end()))
579883048 q {
580883048 q current_idx = area_it.index().val();
581883048 q if (loop_count > 1024*1024)
58394 q rman.health_check(*this);
58494 q loop_count = 0;
587883048 q std::map< Area_Skeleton::Id_Type, std::vector< Area_Block > > areas;
588332291448 q while ((!(area_it == area_blocks_db.discrete_end())) &&
589332291448 q (area_it.index().val() == current_idx))
590331408448 q {
591331408448 q if (binary_search(area_id.begin(), area_id.end(), area_it.object().id))
59210644348 q areas[area_it.object().id].push_back(area_it.object());
593331408448 q ++area_it;
594331408448 q }
596946248 q while (nodes_it != nodes.end() && nodes_it->first.val() < current_idx)
5976325 q {
5986325 q nodes_it->second.clear();
5996325 q ++nodes_it;
6006325 q }
6012427348 q while (nodes_it != nodes.end() &&
6022427348 q (nodes_it->first.val() & 0xffffff00) == current_idx)
6031544330 q {
6041544330 q std::vector< Node_Skeleton > into;
6051544330 q for (typename std::vector< Node_Skeleton >::const_iterator iit = nodes_it->second.begin();
606509045830 q iit != nodes_it->second.end(); ++iit)
607507501529 q {
608507501529 q uint32 ilat((::lat(nodes_it->first.val(), iit->ll_lower)
609507501529 q + 91.0)*10000000+0.5);
610507501529 q int32 ilon(::lon(nodes_it->first.val(), iit->ll_lower)*10000000
611507501529 q + (::lon(nodes_it->first.val(), iit->ll_lower) > 0 ? 0.5 : -0.5));
612507501529 q for (std::map< Area_Skeleton::Id_Type, std::vector< Area_Block > >::const_iterator it = areas.begin();
6131006746729 q it != areas.end(); ++it)
614512434729 q {
615512434729 q int inside = 0;
6163115731829 q for (std::vector< Area_Block >::const_iterator it2 = it->second.begin(); it2 != it->second.end();
6172603297129 q ++it2)
6182603811329 q {
6192603811329 q ++loop_count;
6212603811329 q int check(Coord_Query_Statement::check_area_block(current_idx, *it2, ilat, ilon));
6222603811329 q if (check == Coord_Query_Statement::HIT && add_border)
623514212 q {
624514212 q inside = 1;
625514212 q break;
626514212 q }
6272603297129 q else if (check != 0)
62836038226 q inside ^= check;
6292603811329 q }
630512434729 q if (inside)
63113189526 q {
63213189526 q into.push_back(*iit);
63313189526 q break;
63413189526 q }
635512434729 q }
636507501529 q }
6371544330 q nodes_it->second.swap(into);
6381544330 q ++nodes_it;
6391544330 q }
640883048 q }
641358255 q while (nodes_it != nodes.end())
64234434 q {
64334434 q nodes_it->second.clear();
64434434 q ++nodes_it;
64534434 q }
649const int HIT = 1;
650const int INTERSECT = 8;
653inline int ordered_intersects_inner
654 (uint32 lat_a0, uint32 lon_a0, uint32 lat_a1, uint32 lon_a1,
655 uint32 lat_b0, uint32 lon_b0, uint32 lat_b1, uint32 lon_b1)
656628877156 q{
657628877156 q double det = ((double(lat_a1) - lat_a0)*(double(lon_b1) - lon_b0) - (double(lat_b1) - lat_b0)*(double(lon_a1) - lon_a0));
658628877156 q if (det != 0)
659628568956 q {
660628568956 q double lon =
661628568956 q double(lat_b0 - lat_a0
662628568956 q + double(lon_a0)/(double(lon_a1) - lon_a0)*(double(lat_a1) - lat_a0)
663628568956 q - double(lon_b0)/(double(lon_b1) - lon_b0)*(double(lat_b1) - lat_b0))
664628568956 q /det*(double(lon_a1) - lon_a0)*(double(lon_b1) - lon_b0);
665628568956 q if (lon_a0 < lon && lon < lon_a1 && lon_b0 <= lon && lon <= lon_b1)
666218742 q return (((lat_a0 != lat_b0 || lon_a0 != lon_b0) && (lat_a1 != lat_b1 || lon_a1 != lon_b1)) ?
667209242 q INTERSECT : 0);
668628568956 q }
669308225 q else if ((fabs(lat_a0 - (double(lon_a0) - lon_b0)/(double(lon_b1) - lon_b0)
670308225 q *(double(lat_b1) - lat_b0) - lat_b0) <= 1)
671308225 q || (fabs(lat_a1 - (double(lon_a1) - lon_b0)/(double(lon_b1) - lon_b0)
67244116 q *(double(lat_b1) - lat_b0) - lat_b0) <= 1))
673264119 q return HIT;
675628394356 q return 0;
676628877156 q}
679inline int ordered_a_intersects_inner
680 (uint32 lat_a0, uint32 lon_a0, uint32 lat_a1, uint32 lon_a1,
681 uint32 lat_b0, uint32 lon_b0, uint32 lat_b1, uint32 lon_b1)
68278583212260 q{
68378583212260 q if (lon_b0 < lon_b1)
68438273800160 q {
68538273800160 q if (lon_a0 < lon_b1 && lon_b0 < lon_a1)
686329898656 q return ordered_intersects_inner(lat_a0, lon_a0, lat_a1, lon_a1, lat_b0, lon_b0, lat_b1, lon_b1);
68738273800160 q }
68840309412159 q else if (lon_b1 < lon_b0)
68940218575959 q {
69040218575959 q if (lon_a0 < lon_b0 && lon_b1 < lon_a1)
691298978555 q return ordered_intersects_inner(lat_a0, lon_a0, lat_a1, lon_a1, lat_b1, lon_b1, lat_b0, lon_b0);
69240218575959 q }
69390836210 q else // lon_b0 == lon_b1
69490836210 q {
69590836210 q if (lon_a0 < lon_b0 && lon_b0 < lon_a1)
696183610 q {
697183610 q double lat = (double(lon_b0) - lon_a0)/(double(lon_a1) - lon_a0)*(double(lat_a1) - lat_a0) + lat_a0;
698183610 q return (((lat_b0 < lat && lat < lat_b1) || (lat_b1 < lat && lat < lat_b0)) ?
699183510 q INTERSECT : 0);
700183610 q }
70190836210 q }
70377954151560 q return 0;
70478583212260 q}
707inline int longitude_a_intersects_inner
708 (uint32 lat_a0, uint32 lon_a, uint32 lat_a1,
709 uint32 lat_b0, uint32 lon_b0, uint32 lat_b1, uint32 lon_b1)
710232393449 q{
711232393449 q if (lon_b0 < lon_b1)
712112641549 q {
713112641549 q if (lon_a < lon_b1 && lon_b0 < lon_a)
714701043 q {
715701043 q double lat = (double(lon_a) - lon_b0)/(double(lon_b1) - lon_b0)*(double(lat_b1) - lat_b0) + lat_b0;
716701043 q return (((lat_a0 < lat && lat < lat_a1) || (lat_a1 < lat && lat < lat_a0)) ? INTERSECT : 0);
717701043 q }
718112641549 q }
719119751947 q else if (lon_b1 < lon_b0)
720119423147 q {
721119423147 q if (lon_a < lon_b0 && lon_b1 < lon_a)
722718740 q {
723718740 q double lat = (double(lon_a) - lon_b0)/(double(lon_b1) - lon_b0)*(double(lat_b1) - lat_b0) + lat_b0;
724718740 q return (((lat_a0 < lat && lat < lat_a1) || (lat_a1 < lat && lat < lat_a0)) ? INTERSECT : 0);
725718740 q }
726119423147 q }
72732888 q else // lon_b0 == lon_b1
72832888 q {
72932888 q if (lon_a != lon_b0)
73032828 q return 0;
73162 q if (lat_a0 < lat_a1)
73341 q if (lat_b0 < lat_b1)
73441 q return ((lat_a0 < lat_b1 && lat_b0 < lat_a1) ? HIT : 0);
7350 else
7360 return ((lat_a0 < lat_b0 && lat_b1 < lat_a1) ? HIT : 0);
73822 q else
74022 q if (lat_b0 < lat_b1)
7410 return ((lat_a1 < lat_b1 && lat_b0 < lat_a0) ? HIT : 0);
74222 q else
74322 q return ((lat_a1 < lat_b0 && lat_b1 < lat_a0) ? HIT : 0);
747230644949 q return 0;
748232393449 q}
751int intersects_inner(const Area_Block& string_a, const Area_Block& string_b)
7521434456560 q{
7531434456560 q std::vector< std::pair< uint32, uint32 > > coords_a;
75412772779060 q for (std::vector< uint64 >::const_iterator it = string_a.coors.begin(); it != string_a.coors.end(); ++it)
75511338322560 q coords_a.push_back(std::make_pair(::ilat(uint32((*it>>32)&0xff), uint32(*it & 0xffffffffull)),
75611338322560 q ::ilon(uint32((*it>>32)&0xff), uint32(*it & 0xffffffffull))));
7581434456560 q std::vector< std::pair< uint32, uint32 > > coords_b;
75912207983560 q for (std::vector< uint64 >::const_iterator it = string_b.coors.begin(); it != string_b.coors.end(); ++it)
76010773527060 q coords_b.push_back(std::make_pair(::ilat(uint32((*it>>32)&0xff), uint32(*it & 0xffffffffull)),
76110773527060 q ::ilon(uint32((*it>>32)&0xff), uint32(*it & 0xffffffffull))));
76311325282560 q for (std::vector< std::pair< uint32, uint32 > >::size_type i = 0; i < coords_a.size()-1; ++i)
7649891300260 q {
7659891300260 q if (coords_a[i].second < coords_a[i+1].second)
7665039968858 q {
76744461373358 q for (std::vector< std::pair< uint32, uint32 > >::size_type j = 0; j < coords_b.size()-1; ++j)
76839421649058 q {
76939421649058 q int result = ordered_a_intersects_inner
77039421649058 q (coords_a[i].first, coords_a[i].second, coords_a[i+1].first, coords_a[i+1].second,
77139421649058 q coords_b[j].first, coords_b[j].second, coords_b[j+1].first, coords_b[j+1].second);
77239421649058 q if (result)
773244543 q return result;
77439421649058 q }
7755039968858 q }
7764851331458 q else if (coords_a[i+1].second < coords_a[i].second)
7774821205658 q {
77843982539958 q for (std::vector< std::pair< uint32, uint32 > >::size_type j = 0; j < coords_b.size()-1; ++j)
77939161563258 q {
78039161563258 q int result = ordered_a_intersects_inner
78139161563258 q (coords_a[i+1].first, coords_a[i+1].second, coords_a[i].first, coords_a[i].second,
78239161563258 q coords_b[j].first, coords_b[j].second, coords_b[j+1].first, coords_b[j+1].second);
78339161563258 q if (result)
784228944 q return result;
78539161563258 q }
7864821205658 q }
78730125849 q else
788262518449 q for (std::vector< std::pair< uint32, uint32 > >::size_type j = 0; j < coords_b.size()-1; ++j)
789232393449 q {
790232393449 q int result = longitude_a_intersects_inner
791232393449 q (coords_a[i].first, coords_a[i].second, coords_a[i+1].first,
792232393449 q coords_b[j].first, coords_b[j].second, coords_b[j+1].first, coords_b[j+1].second);
793232393449 q if (result)
79484 q return result;
795232393449 q }
7969891300260 q }
7981433982360 q return 0;
7991434456560 q}
802void has_inner_points(const Area_Block& string_a, const Area_Block& string_b, int& inside)
8031433982360 q{
8041433982360 q std::vector< std::pair< uint32, uint32 > > coords_a;
80512756541660 q for (std::vector< uint64 >::const_iterator it = string_a.coors.begin(); it != string_a.coors.end(); ++it)
80611322559360 q coords_a.push_back(std::make_pair(::ilat(uint32((*it>>32)&0xff), uint32(*it & 0xffffffffull)),
80711322559360 q ::ilon(uint32((*it>>32)&0xff), uint32(*it & 0xffffffffull))));
809 // Check additionally the middle of the segment to also get segments
810 // that run through the area
81111322559360 q for (std::vector< std::pair< uint32, uint32 > >::size_type i = 0; i < coords_a.size()-1; ++i)
8129888577060 q {
8139888577060 q uint32 ilat = (coords_a[i].first + coords_a[i+1].first)/2;
8149888577060 q uint32 ilon = (coords_a[i].second + coords_a[i+1].second)/2 + 0x80000000u;
8159888577060 q int check = Coord_Query_Statement::check_area_block(0, string_b, ilat, ilon);
8169888577060 q if (check & Coord_Query_Statement::HIT)
817126 q inside = check;
8189888575860 q else if (check)
819171652954 q inside ^= check;
8209888577060 q }
8211433982360 q}
824template< typename Way_Skeleton >
825void Area_Query_Statement::collect_ways
826 (const Way_Geometry_Store& way_geometries,
827 std::map< Uint31_Index, std::vector< Way_Skeleton > >& ways,
828 const std::set< Uint31_Index >& req, bool add_border,
829 const Statement& query, Resource_Manager& rman)
83114655 q Block_Backend< Uint31_Index, Area_Block, std::set< Uint31_Index >::const_iterator > area_blocks_db
83214655 q (rman.get_area_transaction()->data_index(area_settings().AREA_BLOCKS));
83314655 q Block_Backend< Uint31_Index, Area_Block, std::set< Uint31_Index >::const_iterator >::Discrete_Iterator
83414655 q area_it(area_blocks_db.discrete_begin(req.begin(), req.end()));
83614655 q std::map< Way::Id_Type, bool > ways_inside;
83814655 q std::map< Uint31_Index, std::vector< Area_Block > > way_segments;
8395132255 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); it != ways.end(); ++it)
8405117638 q {
84174183438 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
84269065834 q add_way_to_area_blocks(way_geometries.get_geometry(*it2), it2->id.val(), way_segments);
8435117638 q }
84514655 q std::map< uint32, std::vector< std::pair< uint32, Way::Id_Type > > > way_coords_to_id;
8465132255 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); it != ways.end(); ++it)
8475117638 q {
84874183438 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
84969065834 q {
85069065834 q std::vector< Quad_Coord > coords = way_geometries.get_geometry(*it2);
851783923934 q for (std::vector< Quad_Coord >::const_iterator it3 = coords.begin(); it3 != coords.end(); ++it3)
852714858134 q way_coords_to_id[it3->ll_upper].push_back(std::make_pair(it3->ll_lower, it2->id));
85369065834 q }
8545117638 q }
85614655 q std::map< uint32, std::vector< std::pair< uint32, Way::Id_Type > > >::const_iterator nodes_it = way_coords_to_id.begin();
858 // Fill node_status with the area related status of each node and segment
85914655 q uint32 loop_count = 0;
86014655 q uint32 current_idx(0);
861846155 q while (!(area_it == area_blocks_db.discrete_end()))
862831548 q {
863831548 q current_idx = area_it.index().val();
864831548 q if (loop_count > 64*1024)
8668516 q rman.health_check(*this);
8678516 q loop_count = 0;
870831548 q std::map< Area_Skeleton::Id_Type, std::vector< Area_Block > > areas;
871313878848 q while ((!(area_it == area_blocks_db.discrete_end())) &&
872313878848 q (area_it.index().val() == current_idx))
873313047348 q {
874313047348 q if (binary_search(area_id.begin(), area_id.end(), area_it.object().id))
87510119948 q areas[area_it.object().id].push_back(area_it.object());
876313047348 q ++area_it;
877313047348 q }
879 // check nodes
8808071648 q while (nodes_it != way_coords_to_id.end() && nodes_it->first < current_idx)
8817240128 q ++nodes_it;
8825448848 q while (nodes_it != way_coords_to_id.end() &&
8835448848 q (nodes_it->first & 0xffffff00) == current_idx)
8844617329 q {
8854617329 q std::vector< std::pair< uint32, Way::Id_Type > > into;
8864617329 q for (std::vector< std::pair< uint32, Way::Id_Type > >::const_iterator iit = nodes_it->second.begin();
887528659629 q iit != nodes_it->second.end(); ++iit)
888524042329 q {
889524042329 q uint32 ilat = ::ilat(nodes_it->first, iit->first);
890524042329 q int32 ilon = ::ilon(nodes_it->first, iit->first);
891524042329 q for (std::map< Area_Skeleton::Id_Type, std::vector< Area_Block > >::const_iterator it = areas.begin();
8921261289229 q it != areas.end(); ++it)
893737246929 q {
894737246929 q int inside = 0;
8955179055629 q for (std::vector< Area_Block >::const_iterator it2 = it->second.begin(); it2 != it->second.end();
8964441808729 q ++it2)
8974452980529 q {
8984452980529 q ++loop_count;
9004452980529 q int check(Coord_Query_Statement::check_area_block(current_idx, *it2, ilat, ilon));
9014452980529 q if (check == Coord_Query_Statement::HIT)
90211171821 q {
90311171821 q inside = Coord_Query_Statement::HIT;
90411171821 q break;
90511171821 q }
9064441808729 q else
9074441808729 q inside ^= check;
9084452980529 q }
909737246929 q if (inside & (Coord_Query_Statement::TOGGLE_EAST | Coord_Query_Statement::TOGGLE_WEST))
910116975026 q ways_inside[iit->second] = true;
911737246929 q }
912524042329 q }
9134617329 q ++nodes_it;
9144617329 q }
916 // check segments
917831548 q for (std::vector< Area_Block >::const_iterator sit = way_segments[Uint31_Index(current_idx)].begin();
91865529748 q sit != way_segments[Uint31_Index(current_idx)].end(); ++sit)
91964698231 q {
92064698231 q std::map< Area::Id_Type, int > area_status;
92164698231 q for (std::map< Area_Skeleton::Id_Type, std::vector< Area_Block > >::const_iterator it = areas.begin();
922147797431 q it != areas.end(); ++it)
92394274331 q {
92494274331 q if (ways_inside[Way::Id_Type(sit->id)])
92511175126 q break;
926539073729 q for (std::vector< Area_Block >::const_iterator it2 = it->second.begin(); it2 != it->second.end();
927455974529 q ++it2)
928456240829 q {
929 // If an area segment intersects this way segment in the inner of the way,
930 // the way is contained in the area.
931 // The endpoints are properly handled via the point-in-area test
932 // Check additionally the middle of the segment to also get segments
933 // that run through the area
934456240829 q int intersect = intersects_inner(*sit, *it2);
935456240829 q if (intersect == Coord_Query_Statement::INTERSECT)
936189 q {
937189 q ways_inside[Way::Id_Type(sit->id)] = true;
938189 q break;
939189 q }
940456239029 q else if (intersect == Coord_Query_Statement::HIT)
941264519 q {
942264519 q if (add_border)
9430 ways_inside[Way::Id_Type(sit->id)] = true;
944264519 q else
945264519 q area_status[it2->id] = Coord_Query_Statement::HIT;
946264519 q break;
947264519 q }
948455974529 q has_inner_points(*sit, *it2, area_status[it2->id]);
949455974529 q }
95083099229 q }
951147797231 q for (std::map< Area::Id_Type, int >::const_iterator it = area_status.begin(); it != area_status.end(); ++it)
95283099029 q {
95383099029 q if ((it->second && (!(it->second & Coord_Query_Statement::HIT))) ||
95483099029 q (it->second && add_border))
955239 q ways_inside[Way::Id_Type(sit->id)] = true;
95683099029 q }
95764698231 q }
958831548 q }
96014655 q std::map< Uint31_Index, std::vector< Way_Skeleton > > result;
962 // Mark ways as found that intersect the area border
96314655 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin();
9645132255 q it != ways.end(); ++it)
9655117638 q {
9665117638 q std::vector< Way_Skeleton > cur_result;
96774183438 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
96869065834 q {
96969065834 q if (ways_inside[it2->id])
9709936427 q {
9719936427 q cur_result.push_back(*it2);
9729936427 q it2->id = Way::Id_Type(0u);
9739936427 q }
97469065834 q }
9755117638 q result[it->first].swap(cur_result);
9765117638 q }
97814655 q result.swap(ways);
982void Area_Query_Statement::execute(Resource_Manager& rman)
9830{
9840 Set into;
9860 Area_Constraint constraint(*this);
9870 Ranges< Uint32_Index > ranges;
9880 constraint.get_ranges(rman, ranges);
9890 get_elements_from_db< Uint32_Index, Node_Skeleton >(ranges, *this, rman)
9900 .swap(into.nodes, into.attic_nodes);
9910 constraint.filter(rman, into);
9920 filter_attic_elements(rman, rman.get_desired_timestamp(), into.nodes, into.attic_nodes);
9930 constraint.filter(*this, rman, into);
9950 transfer_output(rman, into);
9960 rman.health_check(*this);
9970}
1000Query_Constraint* Area_Query_Statement::get_query_constraint()
100218055 q constraints.push_back(new Area_Constraint(*this));
100318055 q return constraints.back();