1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 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. 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. 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/>. 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" 40class Area_Constraint : public Query_Constraint 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() {} 55 Area_Query_Statement* area; 59Ranges< Uint32_Index > copy_discrete_to_area_ranges(const std::set< Uint31_Index >& area_blocks_req) 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)); 69bool Area_Constraint::get_ranges 70 (Resource_Manager& rman, Ranges< Uint32_Index >& ranges) 7229855 q std::set< Uint31_Index > area_blocks_req; 7329855 q if (area->areas_from_input()) 7526850 q const Set* input = rman.get_set(area->get_input()); 7926850 q area->get_ranges(input->ways, input->areas, area_blocks_req, rman); 8026850 q ranges = way_covered_indices(input->ways, input->attic_ways); 84305 q area->get_ranges(area_blocks_req, rman); 85305 q ranges = Ranges< Uint32_Index >(); 8829855 q ranges.union_(copy_discrete_to_area_ranges(area_blocks_req)).swap(ranges); 94bool Area_Constraint::get_ranges 95 (Resource_Manager& rman, Ranges< Uint31_Index >& ranges) 9719855 q Ranges< Uint32_Index > node_ranges; 9819855 q this->get_ranges(rman, node_ranges); 9919855 q ranges = calc_parents(node_ranges); 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); 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) 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) 137365579431 q while (!tai.is_end() && tai.get_idx().val() < iit->first.val()) 1391850631 q if (tai.is_end() || iit->first.val() < tai.get_idx().val()) 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) 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); 154// while (!tai.is_end()) 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) 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<<')'; 168// for (uint i = 0; i < 16; ++i) 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)); 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) 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()) 19518874830 q while (!tai.is_end() && tai.get_idx() < twi.get_idx()) 19715274730 q if (tai.is_end() || twi.get_idx() < tai.get_idx()) 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) 2074201413 q if (it->first->status == Tilewise_Area_Iterator::outside) 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); 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) 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); 2357144066 q while (!tai.is_end() && tai.get_idx() < twi.get_idx()) 23774416 q if (tai.is_end() || twi.get_idx() < tai.get_idx()) 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) 24718482 q if (it->first->status == Tilewise_Area_Iterator::outside) 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); 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()) 2695950 q area->get_ranges(input->ways, input->areas, area_blocks_req, rman); 27265 q area->get_ranges(area_blocks_req, rman); 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); 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); 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); 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()), 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); 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); 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); 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); 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; 41324055 q if (tree_it->token == ":" && tree_it->rhs) 415205 q ref = tree_it.rhs()->token; 416205 q tree_it = tree_it.lhs(); 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) 43624055 q std::map< std::string, std::string > attributes; 43824055 q attributes["from"] = "_"; 43924055 q attributes["into"] = "_"; 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"] != "") 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()); 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) 466unsigned int Area_Query_Statement::count_ranges(Resource_Manager& rman) 46865 q if (!area_blocks_req_filled) 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) 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) 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)); 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) 51032750 q for (std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator it = input_ways.begin(); 511113950 q it != input_ways.end(); ++it) 513165126 q for (std::vector< Way_Skeleton >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) 51583926 q if (!it2->nds.empty() && it2->nds.front() == it2->nds.back()) 51683926 q way_areas_id.push_back(it2->id); 51932750 q std::sort(way_areas_id.begin(), way_areas_id.end()); 52232750 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = input_areas.begin(); 52398550 q it != input_areas.end(); ++it) 525170444 q for (std::vector< Area_Skeleton >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) 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)); 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; 5445950 q const Set* input = rman.get_set(area->get_input()); 5460 return prefer_ranges; 548 // Count the indicies of the input areas 5515950 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = input->areas.begin(); 55218350 q it != input->areas.end(); ++it) 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(); 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(); 578896955 q while (!(area_it == area_blocks_db.discrete_end())) 580883048 q current_idx = area_it.index().val(); 581883048 q if (loop_count > 1024*1024) 58394 q rman.health_check(*this); 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)) 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()); 596946248 q while (nodes_it != nodes.end() && nodes_it->first.val() < current_idx) 5986325 q nodes_it->second.clear(); 6012427348 q while (nodes_it != nodes.end() && 6022427348 q (nodes_it->first.val() & 0xffffff00) == current_idx) 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) 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) 6163115731829 q for (std::vector< Area_Block >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); 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) 6272603297129 q else if (check != 0) 63213189526 q into.push_back(*iit); 6371544330 q nodes_it->second.swap(into); 641358255 q while (nodes_it != nodes.end()) 64334434 q nodes_it->second.clear(); 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) 657628877156 q double det = ((double(lat_a1) - lat_a0)*(double(lon_b1) - lon_b0) - (double(lat_b1) - lat_b0)*(double(lon_a1) - lon_a0)); 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)) ? 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)) 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) 68378583212260 q if (lon_b0 < lon_b1) 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); 68840309412159 q else if (lon_b1 < lon_b0) 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); 69390836210 q else // lon_b0 == lon_b1 69590836210 q if (lon_a0 < lon_b0 && lon_b0 < lon_a1) 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)) ? 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) 711232393449 q if (lon_b0 < lon_b1) 713112641549 q if (lon_a < lon_b1 && lon_b0 < lon_a) 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); 719119751947 q else if (lon_b1 < lon_b0) 721119423147 q if (lon_a < lon_b0 && lon_b1 < lon_a) 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); 72732888 q else // lon_b0 == lon_b1 72932888 q if (lon_a != lon_b0) 73441 q return ((lat_a0 < lat_b1 && lat_b0 < lat_a1) ? HIT : 0); 7360 return ((lat_a0 < lat_b0 && lat_b1 < lat_a1) ? HIT : 0); 7410 return ((lat_a1 < lat_b1 && lat_b0 < lat_a0) ? HIT : 0); 74322 q return ((lat_a1 < lat_b0 && lat_b1 < lat_a0) ? HIT : 0); 751int intersects_inner(const Area_Block& string_a, const Area_Block& string_b) 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) 7659891300260 q if (coords_a[i].second < coords_a[i+1].second) 76744461373358 q for (std::vector< std::pair< uint32, uint32 > >::size_type j = 0; j < coords_b.size()-1; ++j) 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); 7764851331458 q else if (coords_a[i+1].second < coords_a[i].second) 77843982539958 q for (std::vector< std::pair< uint32, uint32 > >::size_type j = 0; j < coords_b.size()-1; ++j) 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); 788262518449 q for (std::vector< std::pair< uint32, uint32 > >::size_type j = 0; j < coords_b.size()-1; ++j) 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); 802void has_inner_points(const Area_Block& string_a, const Area_Block& string_b, int& inside) 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) 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) 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) 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); 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) 84874183438 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) 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)); 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 861846155 q while (!(area_it == area_blocks_db.discrete_end())) 863831548 q current_idx = area_it.index().val(); 864831548 q if (loop_count > 64*1024) 8668516 q rman.health_check(*this); 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)) 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()); 8808071648 q while (nodes_it != way_coords_to_id.end() && nodes_it->first < current_idx) 8825448848 q while (nodes_it != way_coords_to_id.end() && 8835448848 q (nodes_it->first & 0xffffff00) == current_idx) 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) 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) 8955179055629 q for (std::vector< Area_Block >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); 9004452980529 q int check(Coord_Query_Statement::check_area_block(current_idx, *it2, ilat, ilon)); 9014452980529 q if (check == Coord_Query_Statement::HIT) 90311171821 q inside = Coord_Query_Statement::HIT; 909737246929 q if (inside & (Coord_Query_Statement::TOGGLE_EAST | Coord_Query_Statement::TOGGLE_WEST)) 910116975026 q ways_inside[iit->second] = true; 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) 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) 92494274331 q if (ways_inside[Way::Id_Type(sit->id)]) 926539073729 q for (std::vector< Area_Block >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); 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) 937189 q ways_inside[Way::Id_Type(sit->id)] = true; 940456239029 q else if (intersect == Coord_Query_Statement::HIT) 9430 ways_inside[Way::Id_Type(sit->id)] = true; 945264519 q area_status[it2->id] = Coord_Query_Statement::HIT; 948455974529 q has_inner_points(*sit, *it2, area_status[it2->id]); 951147797231 q for (std::map< Area::Id_Type, int >::const_iterator it = area_status.begin(); it != area_status.end(); ++it) 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; 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) 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) 96969065834 q if (ways_inside[it2->id]) 9719936427 q cur_result.push_back(*it2); 9729936427 q it2->id = Way::Id_Type(0u); 9755117638 q result[it->first].swap(cur_result); 982void Area_Query_Statement::execute(Resource_Manager& rman) 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); 1000Query_Constraint* Area_Query_Statement::get_query_constraint() 100218055 q constraints.push_back(new Area_Constraint(*this));