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

overpass_api/statements/area_query.cc

635 of 700 instrumented lines run by the suite (90.7%) 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:
4314145 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)
6024645 q{
6124645 q Ranges< Uint32_Index > result;
6210045845 q for (std::set< Uint31_Index >::const_iterator it = area_blocks_req.begin(); it != area_blocks_req.end(); ++it)
6310021244 q result.push_back(Uint32_Index(it->val()), Uint32_Index((it->val()) + 0x100));
6424645 q result.sort();
6524645 q return result;
6624645 q}
69bool Area_Constraint::get_ranges
70 (Resource_Manager& rman, Ranges< Uint32_Index >& ranges)
7124645 q{
7224645 q std::set< Uint31_Index > area_blocks_req;
7324645 q if (area->areas_from_input())
7424645 q {
7524645 q const Set* input = rman.get_set(area->get_input());
7624645 q if (!input)
770 return true;
7924645 q area->get_ranges(input->ways, input->areas, area_blocks_req, rman);
8024645 q ranges = way_covered_indices(input->ways, input->attic_ways);
8124645 q }
820 else
830 {
840 area->get_ranges(area_blocks_req, rman);
850 ranges = Ranges< Uint32_Index >();
860 }
8824645 q ranges.union_(copy_discrete_to_area_ranges(area_blocks_req)).swap(ranges);
9024645 q return true;
9124645 q}
94bool Area_Constraint::get_ranges
95 (Resource_Manager& rman, Ranges< Uint31_Index >& ranges)
9616445 q{
9716445 q Ranges< Uint32_Index > node_ranges;
9816445 q this->get_ranges(rman, node_ranges);
9916445 q ranges = calc_parents(node_ranges);
10016445 q return true;
104void Area_Constraint::filter(Resource_Manager& rman, Set& into)
1064945 q Ranges< Uint31_Index > ranges;
1074945 q get_ranges(rman, ranges);
109 // pre-process ways to reduce the load of the expensive filter
1104945 q filter_ways_by_ranges(into.ways, ranges);
1114945 q if (!into.attic_ways.empty())
11288 q filter_ways_by_ranges(into.attic_ways, ranges);
114 // pre-filter relations
1154945 q filter_relations_by_ranges(into.relations, ranges);
1164945 q if (!into.attic_relations.empty())
11777 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)
12810945 q if (!potential_areas)
1290 return std::map< Uint32_Index, std::vector< Node_Skeleton > >();
13110945 q Tilewise_Const_Area_Iterator tai(potential_areas->ways, potential_areas->attic_ways, stmt, rman);
13210945 q std::map< Uint32_Index, std::vector< Node_Skeleton > > result;
13410945 q for (typename std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator iit = nodes.begin();
1353070445 q iit != nodes.end(); ++iit)
1363059527 q {
1373272227 q while (!tai.is_end() && tai.get_idx().val() < iit->first.val())
138212714 q tai.next();
1393059527 q if (tai.is_end() || iit->first.val() < tai.get_idx().val())
1402969527 q continue;
1429006 q std::vector< Node_Skeleton >& result_block = result[iit->first];
144327246 q for (typename std::vector< Node_Skeleton >::const_iterator it = iit->second.begin(); it != iit->second.end(); ++it)
145318246 q {
146318246 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';
148318246 q if ((accept_border && relpos != Tilewise_Area_Iterator::outside)
149318246 q || relpos == Tilewise_Area_Iterator::inside)
150131166 q result_block.push_back(*it);
151318246 q }
1529006 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// }
17810945 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)
1869845 q if (!potential_areas)
1870 return std::map< Uint31_Index, std::vector< Way_Skeleton > >();
1899845 q Tilewise_Const_Area_Iterator tai(potential_areas->ways, potential_areas->attic_ways, stmt, rman);
1909845 q std::map< Uint31_Index, std::vector< Way_Skeleton > > result;
1929845 q Tilewise_Way_Iterator twi(ways, std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >(), stmt, rman);
19333663845 q while (!twi.is_end())
19433654030 q {
19533826530 q while (!tai.is_end() && tai.get_idx() < twi.get_idx())
196172519 q tai.next();
19733654030 q if (tai.is_end() || twi.get_idx() < tai.get_idx())
19833584130 q {
19933584130 q twi.next();
20033584130 q continue;
20133584130 q }
20369913 q const std::map< Tilewise_Way_Iterator::Status_Ref< Way_Skeleton >*, Tilewise_Way_Iterator::Index_Block >& obj = twi.get_current_obj();
20469913 q for (std::map< Tilewise_Way_Iterator::Status_Ref< Way_Skeleton >*, Tilewise_Way_Iterator::Index_Block >::const_iterator it = obj.begin();
205519713 q it != obj.end(); ++it)
206449813 q {
207449813 q if (it->first->status == Tilewise_Area_Iterator::outside)
208336213 q {
209336213 q it->first->status = tai.rel_position(it->second.segments, true);
210336213 q if (it->first->status != Tilewise_Area_Iterator::outside)
21178912 q result[it->first->idx].push_back(*it->first->skel);
212336213 q }
213449813 q }
21569913 q twi.next();
21669913 q }
2189845 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)
2261512 q if (!potential_areas)
2270 return std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >();
2291512 q Tilewise_Const_Area_Iterator tai(potential_areas->ways, potential_areas->attic_ways, stmt, rman);
2301512 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > result;
2321512 q Tilewise_Way_Iterator twi(std::map< Uint31_Index, std::vector< Way_Skeleton > >(), ways, stmt, rman);
2339482512 q while (!twi.is_end())
234948109 q {
2351287439 q while (!tai.is_end() && tai.get_idx() < twi.get_idx())
236339336 q tai.next();
237948109 q if (tai.is_end() || twi.get_idx() < tai.get_idx())
238940619 q {
239940619 q twi.next();
240940619 q continue;
241940619 q }
2437494 q const std::map< Tilewise_Way_Iterator::Status_Ref< Attic< Way_Skeleton > >*, Tilewise_Way_Iterator::Index_Block >& obj = twi.get_attic_obj();
2447494 q for (std::map< Tilewise_Way_Iterator::Status_Ref< Attic< Way_Skeleton > >*, Tilewise_Way_Iterator::Index_Block >::const_iterator
24523944 q it = obj.begin(); it != obj.end(); ++it)
24616454 q {
24716454 q if (it->first->status == Tilewise_Area_Iterator::outside)
2484204 q {
2494204 q it->first->status = tai.rel_position(it->second.segments, true);
2504204 q if (it->first->status != Tilewise_Area_Iterator::outside)
2512954 q result[it->first->idx].push_back(*it->first->skel);
2524204 q }
25316454 q }
2557494 q twi.next();
2567494 q }
2581512 q return result;
262void Area_Constraint::filter(const Statement& query, Resource_Manager& rman, Set& into)
2644945 q std::set< Uint31_Index > area_blocks_req;
2654945 q const Set* input = rman.get_set(area->get_input());
2664945 q if (area->areas_from_input())
2684945 q if (input)
2694945 q area->get_ranges(input->ways, input->areas, area_blocks_req, rman);
2710 else
2720 area->get_ranges(area_blocks_req, rman);
274 //Process nodes
2764945 q std::map< Uint32_Index, std::vector< Node_Skeleton > > nodes_in_wr_areas
2774945 q = nodes_contained_in(input, true, query, rman, into.nodes);
2784945 q indexed_set_difference(into.nodes, nodes_in_wr_areas);
2794945 q area->collect_nodes(into.nodes, area_blocks_req, true, rman);
2804945 q indexed_set_union(into.nodes, nodes_in_wr_areas);
2814945 q }
283 //Process ways
2854945 q std::map< Uint31_Index, std::vector< Way_Skeleton > > ways_in_wr_areas
2864945 q = ways_contained_in(input, query, rman, into.ways);
2874945 q indexed_set_difference(into.ways, ways_in_wr_areas);
2884945 q area->collect_ways(Way_Geometry_Store(into.ways, query, rman),
2894945 q into.ways, area_blocks_req, false, query, rman);
2904945 q indexed_set_union(into.ways, ways_in_wr_areas);
293 //Process relations
2944945 q Request_Context context(&query, rman);
296 // Retrieve all nodes referred by the relations.
2974945 q Ranges< Uint32_Index > node_ranges;
2984945 q get_ranges(rman, node_ranges);
2994945 q Timeless< Uint32_Index, Node_Skeleton > node_members
3004945 q = relation_node_members(context, into.relations, {}, node_ranges, {}, true);
302 // filter for those nodes that are in one of the areas
3044945 q std::map< Uint32_Index, std::vector< Node_Skeleton > > nodes_in_wr_areas
3054945 q = nodes_contained_in(input, false, query, rman, node_members.current);
3064945 q indexed_set_difference(node_members.current, nodes_in_wr_areas);
3074945 q area->collect_nodes(node_members.current, area_blocks_req, false, rman);
3084945 q indexed_set_union(node_members.current, nodes_in_wr_areas);
3094945 q }
311 // Retrieve all ways referred by the relations.
3124945 q Ranges< Uint31_Index > way_ranges;
3134945 q get_ranges(rman, way_ranges);
3144945 q Timeless< Uint31_Index, Way_Skeleton > way_members_
3154945 q = relation_way_members(context, into.relations, {}, way_ranges, {}, true);
317 // Filter for those ways that are in one of the areas
3194945 q std::map< Uint31_Index, std::vector< Way_Skeleton > > ways_in_wr_areas
3204945 q = ways_contained_in(input, query, rman, way_members_.current);
3214945 q indexed_set_difference(way_members_.current, ways_in_wr_areas);
3224945 q area->collect_ways(Way_Geometry_Store(way_members_.current, query, rman),
3234945 q way_members_.current, area_blocks_req, false, query, rman);
3244945 q indexed_set_union(way_members_.current, ways_in_wr_areas);
3274945 q filter_relations_expensive(order_by_id(node_members.current, Order_By_Node_Id()),
3284945 q order_by_id(way_members_.current, Order_By_Way_Id()),
3294945 q into.relations);
331 //Process nodes
3324945 q if (!into.attic_nodes.empty())
33444 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > > nodes_in_wr_areas
33544 q = nodes_contained_in(input, true, query, rman, into.attic_nodes);
33644 q indexed_set_difference(into.attic_nodes, nodes_in_wr_areas);
33744 q area->collect_nodes(into.attic_nodes, area_blocks_req, true, rman);
33844 q indexed_set_union(into.attic_nodes, nodes_in_wr_areas);
33944 q }
341 //Process ways
3424945 q if (!into.attic_ways.empty())
34488 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > ways_in_wr_areas
34588 q = ways_contained_in(input, query, rman, into.attic_ways);
34688 q indexed_set_difference(into.attic_ways, ways_in_wr_areas);
34788 q area->collect_ways(Way_Geometry_Store(into.attic_ways, query, rman),
34888 q into.attic_ways, area_blocks_req, false, query, rman);
34988 q indexed_set_union(into.attic_ways, ways_in_wr_areas);
352 //Process relations
3534945 q if (!into.attic_relations.empty())
355 // Retrieve all nodes referred by the relations.
35677 q Ranges< Uint32_Index > node_ranges;
35777 q get_ranges(rman, node_ranges);
35877 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > > node_members
35977 q = relation_node_members(context, into.attic_relations, node_ranges);
361 // filter for those nodes that are in one of the areas
36377 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > > nodes_in_wr_areas
36477 q = nodes_contained_in(input, false, query, rman, node_members);
36577 q indexed_set_difference(node_members, nodes_in_wr_areas);
36677 q area->collect_nodes(node_members, area_blocks_req, false, rman);
36777 q indexed_set_union(node_members, nodes_in_wr_areas);
36877 q }
370 // Retrieve all ways referred by the relations.
37177 q Ranges< Uint31_Index > way_ranges;
37277 q get_ranges(rman, way_ranges);
37377 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > way_members_
37477 q = relation_way_members(context, into.attic_relations, way_ranges);
376 // Filter for those ways that are in one of the areas
37877 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > ways_in_wr_areas
37977 q = ways_contained_in(input, query, rman, way_members_);
38077 q indexed_set_difference(way_members_, ways_in_wr_areas);
38177 q area->collect_ways(Way_Geometry_Store(way_members_, query, rman),
38277 q way_members_, area_blocks_req, false, query, rman);
38377 q indexed_set_union(way_members_, ways_in_wr_areas);
38677 q filter_relations_expensive(order_attic_by_id(node_members, Order_By_Node_Id()),
38777 q order_attic_by_id(way_members_, Order_By_Way_Id()),
38877 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)
40818845 q Token_Node_Ptr tree_it = input_tree;
40918845 q uint line_nr = tree_it->line_col.first;
41018845 q std::string from = "_";
41118845 q std::string ref;
41318845 q if (tree_it->token == ":" && tree_it->rhs)
4140 {
4150 ref = tree_it.rhs()->token;
4160 tree_it = tree_it.lhs();
4170 }
41918845 q if (tree_it->token == "." && tree_it->rhs)
42018845 q from = tree_it.rhs()->token;
42218845 q std::map< std::string, std::string > attributes;
42318845 q attributes["from"] = from;
42418845 q attributes["into"] = into;
42518845 q attributes["ref"] = ref;
42618845 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)
43218845 q : Output_Statement(line_number_), area_blocks_req_filled(false)
43418845 q is_used_ = true;
43618845 q std::map< std::string, std::string > attributes;
43818845 q attributes["from"] = "_";
43918845 q attributes["into"] = "_";
44018845 q attributes["ref"] = "";
44218845 q eval_attributes_array(get_name(), attributes, input_attributes);
44418845 q input = attributes["from"];
44518845 q set_output(attributes["into"]);
44618845 q submitted_id = atoll(attributes["ref"].c_str());
44718845 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 }
45418845 q else if (submitted_id > 0)
4550 area_id.push_back(Area_Skeleton::Id_Type(submitted_id));
458Area_Query_Statement::~Area_Query_Statement()
46018845 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin();
46132945 q it != constraints.end(); ++it)
46214145 q delete *it;
466unsigned int Area_Query_Statement::count_ranges(Resource_Manager& rman)
4670{
4680 if (!area_blocks_req_filled)
4690 fill_ranges(rman);
4710 return area_blocks_req.size();
4720}
475void Area_Query_Statement::get_ranges(std::set< Uint31_Index >& area_blocks_req, Resource_Manager& rman)
4760{
4770 if (!area_blocks_req_filled)
4780 fill_ranges(rman);
4800 area_blocks_req = this->area_blocks_req;
4810}
484void Area_Query_Statement::fill_ranges(Resource_Manager& rman)
4850{
4860 Block_Backend< Uint31_Index, Area_Skeleton > area_locations_db
4870 (rman.get_area_transaction()->data_index(area_settings().AREAS));
4880 for (Block_Backend< Uint31_Index, Area_Skeleton >::Flat_Iterator
4890 it(area_locations_db.flat_begin());
4900 !(it == area_locations_db.flat_end()); ++it)
4910 {
4920 if (binary_search(area_id.begin(), area_id.end(), it.object().id))
4930 {
4940 for (std::vector< uint32 >::const_iterator it2(it.object().used_indices.begin());
4950 it2 != it.object().used_indices.end(); ++it2)
4960 area_blocks_req.insert(Uint31_Index(*it2));
4970 }
4980 }
4990 area_blocks_req_filled = true;
5000}
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)
50929545 q way_areas_id.clear();
51029545 q for (std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator it = input_ways.begin();
511114545 q it != input_ways.end(); ++it)
51285022 q {
513172522 q for (std::vector< Way_Skeleton >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
51487522 q {
51587522 q if (!it2->nds.empty() && it2->nds.front() == it2->nds.back())
51687522 q way_areas_id.push_back(it2->id);
51787522 q }
51885022 q }
51929545 q std::sort(way_areas_id.begin(), way_areas_id.end());
52129545 q area_id.clear();
52229545 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = input_areas.begin();
523498445 q it != input_areas.end(); ++it)
524468944 q {
5252820344 q for (std::vector< Area_Skeleton >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
5262351444 q {
5272351444 q area_id.push_back(it2->id);
5292351444 q for (std::vector< uint32 >::const_iterator it3(it2->used_indices.begin());
53025786544 q it3 != it2->used_indices.end(); ++it3)
53123435144 q area_blocks_req.insert(Uint31_Index(*it3));
5322351444 q }
533468944 q }
53429545 q std::sort(area_id.begin(), area_id.end());
538Query_Filter_Strategy Area_Constraint::delivers_data(Resource_Manager& rman)
5404945 q if (!area->areas_from_input())
5410 return (area->count_ranges(rman) < 12) ? prefer_ranges : ids_useful;
5424945 q else
5444945 q const Set* input = rman.get_set(area->get_input());
5454945 q if (!input)
5460 return prefer_ranges;
548 // Count the indicies of the input areas
5494945 q int counter = 0;
5514945 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = input->areas.begin();
55274145 q it != input->areas.end(); ++it)
55369244 q {
554407844 q for (std::vector< Area_Skeleton >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
555338644 q counter += it2->used_indices.size();
55669244 q }
5584945 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)
56910945 q Block_Backend< Uint31_Index, Area_Block, std::set< Uint31_Index >::const_iterator > area_blocks_db
57010945 q (rman.get_area_transaction()->data_index(area_settings().AREA_BLOCKS));
57110945 q Block_Backend< Uint31_Index, Area_Block, std::set< Uint31_Index >::const_iterator >::Discrete_Iterator
57210945 q area_it(area_blocks_db.discrete_begin(req.begin(), req.end()));
57410945 q typename std::map< Uint32_Index, std::vector< Node_Skeleton > >::iterator nodes_it = nodes.begin();
57610945 q uint32 loop_count = 0;
57710945 q uint32 current_idx(0);
5784606245 q while (!(area_it == area_blocks_db.discrete_end()))
5794595344 q {
5804595344 q current_idx = area_it.index().val();
5814595344 q if (loop_count > 1024*1024)
58322 q rman.health_check(*this);
58422 q loop_count = 0;
5874595344 q std::map< Area_Skeleton::Id_Type, std::vector< Area_Block > > areas;
588398074944 q while ((!(area_it == area_blocks_db.discrete_end())) &&
589398074944 q (area_it.index().val() == current_idx))
590393479644 q {
591393479644 q if (binary_search(area_id.begin(), area_id.end(), area_it.object().id))
59224685844 q areas[area_it.object().id].push_back(area_it.object());
593393479644 q ++area_it;
594393479644 q }
5965430644 q while (nodes_it != nodes.end() && nodes_it->first.val() < current_idx)
59783537 q {
59883537 q nodes_it->second.clear();
59983537 q ++nodes_it;
60083537 q }
6016813544 q while (nodes_it != nodes.end() &&
6026813544 q (nodes_it->first.val() & 0xffffff00) == current_idx)
6032218226 q {
6042218226 q std::vector< Node_Skeleton > into;
6052218226 q for (typename std::vector< Node_Skeleton >::const_iterator iit = nodes_it->second.begin();
606157711226 q iit != nodes_it->second.end(); ++iit)
607155493026 q {
608155493026 q uint32 ilat((::lat(nodes_it->first.val(), iit->ll_lower)
609155493026 q + 91.0)*10000000+0.5);
610155493026 q int32 ilon(::lon(nodes_it->first.val(), iit->ll_lower)*10000000
611155493026 q + (::lon(nodes_it->first.val(), iit->ll_lower) > 0 ? 0.5 : -0.5));
612155493026 q for (std::map< Area_Skeleton::Id_Type, std::vector< Area_Block > >::const_iterator it = areas.begin();
613318462326 q it != areas.end(); ++it)
614176351426 q {
615176351426 q int inside = 0;
6161117569226 q for (std::vector< Area_Block >::const_iterator it2 = it->second.begin(); it2 != it->second.end();
617941217826 q ++it2)
618942421926 q {
619942421926 q ++loop_count;
621942421926 q int check(Coord_Query_Statement::check_area_block(current_idx, *it2, ilat, ilon));
622942421926 q if (check == Coord_Query_Statement::HIT && add_border)
6231204114 q {
6241204114 q inside = 1;
6251204114 q break;
6261204114 q }
627941217826 q else if (check != 0)
62832207124 q inside ^= check;
629942421926 q }
630176351426 q if (inside)
63113382124 q {
63213382124 q into.push_back(*iit);
63313382124 q break;
63413382124 q }
635176351426 q }
636155493026 q }
6372218226 q nodes_it->second.swap(into);
6382218226 q ++nodes_it;
6392218226 q }
6404595344 q }
64116945 q while (nodes_it != nodes.end())
642604 q {
643604 q nodes_it->second.clear();
644604 q ++nodes_it;
645604 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)
656325109766 q{
657325109766 q double det = ((double(lat_a1) - lat_a0)*(double(lon_b1) - lon_b0) - (double(lat_b1) - lat_b0)*(double(lon_a1) - lon_a0));
658325109766 q if (det != 0)
659324573866 q {
660324573866 q double lon =
661324573866 q double(lat_b0 - lat_a0
662324573866 q + double(lon_a0)/(double(lon_a1) - lon_a0)*(double(lat_a1) - lat_a0)
663324573866 q - double(lon_b0)/(double(lon_b1) - lon_b0)*(double(lat_b1) - lat_b0))
664324573866 q /det*(double(lon_a1) - lon_a0)*(double(lon_b1) - lon_b0);
665324573866 q if (lon_a0 < lon && lon < lon_a1 && lon_b0 <= lon && lon <= lon_b1)
666183057 q return (((lat_a0 != lat_b0 || lon_a0 != lon_b0) && (lat_a1 != lat_b1 || lon_a1 != lon_b1)) ?
667176057 q INTERSECT : 0);
668324573866 q }
669535935 q else if ((fabs(lat_a0 - (double(lon_a0) - lon_b0)/(double(lon_b1) - lon_b0)
670535935 q *(double(lat_b1) - lat_b0) - lat_b0) <= 1)
671535935 q || (fabs(lat_a1 - (double(lon_a1) - lon_b0)/(double(lon_b1) - lon_b0)
67282525 q *(double(lat_b1) - lat_b0) - lat_b0) <= 1))
673453423 q return HIT;
675324473366 q return 0;
676325109766 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)
68231167535269 q{
68331167535269 q if (lon_b0 < lon_b1)
68416203734369 q {
68516203734369 q if (lon_a0 < lon_b1 && lon_b0 < lon_a1)
686193201366 q return ordered_intersects_inner(lat_a0, lon_a0, lat_a1, lon_a1, lat_b0, lon_b0, lat_b1, lon_b1);
68716203734369 q }
68814963800966 q else if (lon_b1 < lon_b0)
68914924657466 q {
69014924657466 q if (lon_a0 < lon_b0 && lon_b1 < lon_a1)
691131908463 q return ordered_intersects_inner(lat_a0, lon_a0, lat_a1, lon_a1, lat_b1, lon_b1, lat_b0, lon_b0);
69214924657466 q }
69339143511 q else // lon_b0 == lon_b1
69439143511 q {
69539143511 q if (lon_a0 < lon_b0 && lon_b0 < lon_a1)
696117810 q {
697117810 q double lat = (double(lon_b0) - lon_a0)/(double(lon_a1) - lon_a0)*(double(lat_a1) - lat_a0) + lat_a0;
698117810 q return (((lat_b0 < lat && lat < lat_b1) || (lat_b1 < lat && lat < lat_b0)) ?
699117810 q INTERSECT : 0);
700117810 q }
70139143511 q }
70330842307769 q return 0;
70431167535269 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)
71062854456 q{
71162854456 q if (lon_b0 < lon_b1)
71232669956 q {
71332669956 q if (lon_a < lon_b1 && lon_b0 < lon_a)
714486348 q {
715486348 q double lat = (double(lon_a) - lon_b0)/(double(lon_b1) - lon_b0)*(double(lat_b1) - lat_b0) + lat_b0;
716486348 q return (((lat_a0 < lat && lat < lat_a1) || (lat_a1 < lat && lat < lat_a0)) ? INTERSECT : 0);
717486348 q }
71832669956 q }
71930184553 q else if (lon_b1 < lon_b0)
72030133753 q {
72130133753 q if (lon_a < lon_b0 && lon_b1 < lon_a)
722280346 q {
723280346 q double lat = (double(lon_a) - lon_b0)/(double(lon_b1) - lon_b0)*(double(lat_b1) - lat_b0) + lat_b0;
724280346 q return (((lat_a0 < lat && lat < lat_a1) || (lat_a1 < lat && lat < lat_a0)) ? INTERSECT : 0);
725280346 q }
72630133753 q }
7275089 q else // lon_b0 == lon_b1
7285089 q {
7295089 q if (lon_a != lon_b0)
7304869 q return 0;
731223 q if (lat_a0 < lat_a1)
732102 q {
733102 q if (lat_b0 < lat_b1)
73452 q return ((lat_a0 < lat_b1 && lat_b0 < lat_a1) ? HIT : 0);
73551 q else
73651 q return ((lat_a0 < lat_b0 && lat_b1 < lat_a1) ? HIT : 0);
737102 q }
738122 q else
739122 q {
740122 q if (lat_b0 < lat_b1)
74151 q return ((lat_a1 < lat_b1 && lat_b0 < lat_a0) ? HIT : 0);
74272 q else
74372 q return ((lat_a1 < lat_b0 && lat_b1 < lat_a0) ? HIT : 0);
744122 q }
745223 q }
74762037056 q return 0;
74862854456 q}
751int intersects_inner(const Area_Block& string_a, const Area_Block& string_b)
752233233269 q{
753233233269 q std::vector< std::pair< uint32, uint32 > > coords_a;
7543204381469 q for (std::vector< uint64 >::const_iterator it = string_a.coors.begin(); it != string_a.coors.end(); ++it)
7552971148269 q coords_a.push_back(std::make_pair(::ilat(uint32((*it>>32)&0xff), uint32(*it & 0xffffffffull)),
7562971148269 q ::ilon(uint32((*it>>32)&0xff), uint32(*it & 0xffffffffull))));
758233233269 q std::vector< std::pair< uint32, uint32 > > coords_b;
7592645398869 q for (std::vector< uint64 >::const_iterator it = string_b.coors.begin(); it != string_b.coors.end(); ++it)
7602412165669 q coords_b.push_back(std::make_pair(::ilat(uint32((*it>>32)&0xff), uint32(*it & 0xffffffffull)),
7612412165669 q ::ilon(uint32((*it>>32)&0xff), uint32(*it & 0xffffffffull))));
7632962540169 q for (std::vector< std::pair< uint32, uint32 > >::size_type i = 0; i < coords_a.size()-1; ++i)
7642729939469 q {
7652729939469 q if (coords_a[i].second < coords_a[i+1].second)
7661421833069 q {
76717456881769 q for (std::vector< std::pair< uint32, uint32 > >::size_type j = 0; j < coords_b.size()-1; ++j)
76816035459569 q {
76916035459569 q int result = ordered_a_intersects_inner
77016035459569 q (coords_a[i].first, coords_a[i].second, coords_a[i+1].first, coords_a[i+1].second,
77116035459569 q coords_b[j].first, coords_b[j].second, coords_b[j+1].first, coords_b[j+1].second);
77216035459569 q if (result)
773410860 q return result;
77416035459569 q }
7751421833069 q }
7761308106469 q else if (coords_a[i+1].second < coords_a[i].second)
7771300393469 q {
77816432250569 q for (std::vector< std::pair< uint32, uint32 > >::size_type j = 0; j < coords_b.size()-1; ++j)
77915132075769 q {
78015132075769 q int result = ordered_a_intersects_inner
78115132075769 q (coords_a[i+1].first, coords_a[i+1].second, coords_a[i].first, coords_a[i].second,
78215132075769 q coords_b[j].first, coords_b[j].second, coords_b[j+1].first, coords_b[j+1].second);
78315132075769 q if (result)
784218658 q return result;
78515132075769 q }
7861300393469 q }
7877713056 q else
78870564356 q for (std::vector< std::pair< uint32, uint32 > >::size_type j = 0; j < coords_b.size()-1; ++j)
78962854456 q {
79062854456 q int result = longitude_a_intersects_inner
79162854456 q (coords_a[i].first, coords_a[i].second, coords_a[i+1].first,
79262854456 q coords_b[j].first, coords_b[j].second, coords_b[j+1].first, coords_b[j+1].second);
79362854456 q if (result)
7943114 q return result;
79562854456 q }
7962729939469 q }
798232600769 q return 0;
799233233269 q}
802void has_inner_points(const Area_Block& string_a, const Area_Block& string_b, int& inside)
803232600769 q{
804232600769 q std::vector< std::pair< uint32, uint32 > > coords_a;
8053190344169 q for (std::vector< uint64 >::const_iterator it = string_a.coors.begin(); it != string_a.coors.end(); ++it)
8062957743469 q coords_a.push_back(std::make_pair(::ilat(uint32((*it>>32)&0xff), uint32(*it & 0xffffffffull)),
8072957743469 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
8112957743469 q for (std::vector< std::pair< uint32, uint32 > >::size_type i = 0; i < coords_a.size()-1; ++i)
8122725142769 q {
8132725142769 q uint32 ilat = (coords_a[i].first + coords_a[i+1].first)/2;
8142725142769 q uint32 ilon = (coords_a[i].second + coords_a[i+1].second)/2 + 0x80000000u;
8152725142769 q int check = Coord_Query_Statement::check_area_block(0, string_b, ilat, ilon);
8162725142769 q if (check & Coord_Query_Statement::HIT)
81773 q inside = check;
8182725142069 q else if (check)
819102632865 q inside ^= check;
8202725142769 q }
821232600769 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)
83111345 q Block_Backend< Uint31_Index, Area_Block, std::set< Uint31_Index >::const_iterator > area_blocks_db
83211345 q (rman.get_area_transaction()->data_index(area_settings().AREA_BLOCKS));
83311345 q Block_Backend< Uint31_Index, Area_Block, std::set< Uint31_Index >::const_iterator >::Discrete_Iterator
83411345 q area_it(area_blocks_db.discrete_begin(req.begin(), req.end()));
83611345 q std::map< Way::Id_Type, bool > ways_inside;
83811345 q std::map< Uint31_Index, std::vector< Area_Block > > way_segments;
8396210845 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); it != ways.end(); ++it)
8406199535 q {
84130472235 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
84224272733 q add_way_to_area_blocks(way_geometries.get_geometry(*it2), it2->id.val(), way_segments);
8436199535 q }
84511345 q std::map< uint32, std::vector< std::pair< uint32, Way::Id_Type > > > way_coords_to_id;
8466210845 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); it != ways.end(); ++it)
8476199535 q {
84830472235 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
84924272733 q {
85024272733 q std::vector< Quad_Coord > coords = way_geometries.get_geometry(*it2);
851697213533 q for (std::vector< Quad_Coord >::const_iterator it3 = coords.begin(); it3 != coords.end(); ++it3)
852672940833 q way_coords_to_id[it3->ll_upper].push_back(std::make_pair(it3->ll_lower, it2->id));
85324272733 q }
8546199535 q }
85611345 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
85911345 q uint32 loop_count = 0;
86011345 q uint32 current_idx(0);
8614865545 q while (!(area_it == area_blocks_db.discrete_end()))
8624854244 q {
8634854244 q current_idx = area_it.index().val();
8644854244 q if (loop_count > 64*1024)
86512319 q {
86612319 q rman.health_check(*this);
86712319 q loop_count = 0;
86812319 q }
8704854244 q std::map< Area_Skeleton::Id_Type, std::vector< Area_Block > > areas;
871420870444 q while ((!(area_it == area_blocks_db.discrete_end())) &&
872420870444 q (area_it.index().val() == current_idx))
873416016244 q {
874416016244 q if (binary_search(area_id.begin(), area_id.end(), area_it.object().id))
87525499344 q areas[area_it.object().id].push_back(area_it.object());
876416016244 q ++area_it;
877416016244 q }
879 // check nodes
88017953244 q while (nodes_it != way_coords_to_id.end() && nodes_it->first < current_idx)
88113099028 q ++nodes_it;
88215988044 q while (nodes_it != way_coords_to_id.end() &&
88315988044 q (nodes_it->first & 0xffffff00) == current_idx)
88411133831 q {
88511133831 q std::vector< std::pair< uint32, Way::Id_Type > > into;
88611133831 q for (std::vector< std::pair< uint32, Way::Id_Type > >::const_iterator iit = nodes_it->second.begin();
887389341031 q iit != nodes_it->second.end(); ++iit)
888378207231 q {
889378207231 q uint32 ilat = ::ilat(nodes_it->first, iit->first);
890378207231 q int32 ilon = ::ilon(nodes_it->first, iit->first);
891378207231 q for (std::map< Area_Skeleton::Id_Type, std::vector< Area_Block > >::const_iterator it = areas.begin();
892848313031 q it != areas.end(); ++it)
893470105831 q {
894470105831 q int inside = 0;
8952440829131 q for (std::vector< Area_Block >::const_iterator it2 = it->second.begin(); it2 != it->second.end();
8961970723331 q ++it2)
8971981529931 q {
8981981529931 q ++loop_count;
9001981529931 q int check(Coord_Query_Statement::check_area_block(current_idx, *it2, ilat, ilon));
9011981529931 q if (check == Coord_Query_Statement::HIT)
90210806623 q {
90310806623 q inside = Coord_Query_Statement::HIT;
90410806623 q break;
90510806623 q }
9061970723331 q else
9071970723331 q inside ^= check;
9081981529931 q }
909470105831 q if (inside & (Coord_Query_Statement::TOGGLE_EAST | Coord_Query_Statement::TOGGLE_WEST))
910187174926 q ways_inside[iit->second] = true;
911470105831 q }
912378207231 q }
91311133831 q ++nodes_it;
91411133831 q }
916 // check segments
9174854244 q for (std::vector< Area_Block >::const_iterator sit = way_segments[Uint31_Index(current_idx)].begin();
91830543444 q sit != way_segments[Uint31_Index(current_idx)].end(); ++sit)
91925689231 q {
92025689231 q std::map< Area::Id_Type, int > area_status;
92125689231 q for (std::map< Area_Skeleton::Id_Type, std::vector< Area_Block > >::const_iterator it = areas.begin();
92243874831 q it != areas.end(); ++it)
92327676431 q {
92427676431 q if (ways_inside[Way::Id_Type(sit->id)])
9259490826 q break;
926107663227 q for (std::vector< Area_Block >::const_iterator it2 = it->second.begin(); it2 != it->second.end();
92789477627 q ++it2)
92889936927 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
93489936927 q int intersect = intersects_inner(*sit, *it2);
93589936927 q if (intersect == Coord_Query_Statement::INTERSECT)
9375516 q ways_inside[Way::Id_Type(sit->id)] = true;
9385516 q break;
94089931427 q else if (intersect == Coord_Query_Statement::HIT)
941453823 q {
942453823 q if (add_border)
9430 ways_inside[Way::Id_Type(sit->id)] = true;
944453823 q else
945453823 q area_status[it2->id] = Coord_Query_Statement::HIT;
946453823 q break;
947453823 q }
94889477627 q has_inner_points(*sit, *it2, area_status[it2->id]);
94989477627 q }
95018185627 q }
95143873731 q for (std::map< Area::Id_Type, int >::const_iterator it = area_status.begin(); it != area_status.end(); ++it)
95218184527 q {
95318184527 q if ((it->second && (!(it->second & Coord_Query_Statement::HIT))) ||
95418184527 q (it->second && add_border))
9557611 q ways_inside[Way::Id_Type(sit->id)] = true;
95618184527 q }
95725689231 q }
9584854244 q }
96011345 q std::map< Uint31_Index, std::vector< Way_Skeleton > > result;
962 // Mark ways as found that intersect the area border
96311345 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin();
9646210845 q it != ways.end(); ++it)
9656199535 q {
9666199535 q std::vector< Way_Skeleton > cur_result;
96730472235 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
96824272733 q {
96924272733 q if (ways_inside[it2->id])
9706326826 q {
9716326826 q cur_result.push_back(*it2);
9726326826 q it2->id = Way::Id_Type(0u);
9736326826 q }
97424272733 q }
9756199535 q result[it->first].swap(cur_result);
9766199535 q }
97811345 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()
100214145 q constraints.push_back(new Area_Constraint(*this));
100314145 q return constraints.back();