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 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() {} 55 Area_Query_Statement* area; 59Ranges< Uint32_Index > copy_discrete_to_area_ranges(const std::set< Uint31_Index >& area_blocks_req) 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)); 69bool Area_Constraint::get_ranges 70 (Resource_Manager& rman, Ranges< Uint32_Index >& ranges) 7224645 q std::set< Uint31_Index > area_blocks_req; 7324645 q if (area->areas_from_input()) 7524645 q const Set* input = rman.get_set(area->get_input()); 7924645 q area->get_ranges(input->ways, input->areas, area_blocks_req, rman); 8024645 q ranges = way_covered_indices(input->ways, input->attic_ways); 840 area->get_ranges(area_blocks_req, rman); 850 ranges = Ranges< Uint32_Index >(); 8824645 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) 9716445 q Ranges< Uint32_Index > node_ranges; 9816445 q this->get_ranges(rman, node_ranges); 9916445 q ranges = calc_parents(node_ranges); 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); 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) 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) 1373272227 q while (!tai.is_end() && tai.get_idx().val() < iit->first.val()) 1393059527 q if (tai.is_end() || iit->first.val() < tai.get_idx().val()) 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) 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); 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) 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()) 19533826530 q while (!tai.is_end() && tai.get_idx() < twi.get_idx()) 19733654030 q if (tai.is_end() || twi.get_idx() < tai.get_idx()) 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(); 207449813 q if (it->first->status == Tilewise_Area_Iterator::outside) 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); 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) 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); 2351287439 q while (!tai.is_end() && tai.get_idx() < twi.get_idx()) 237948109 q if (tai.is_end() || twi.get_idx() < tai.get_idx()) 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) 24716454 q if (it->first->status == Tilewise_Area_Iterator::outside) 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); 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()) 2694945 q area->get_ranges(input->ways, input->areas, area_blocks_req, rman); 2720 area->get_ranges(area_blocks_req, rman); 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); 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); 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); 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()), 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); 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); 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); 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); 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; 41318845 q if (tree_it->token == ":" && tree_it->rhs) 4150 ref = tree_it.rhs()->token; 4160 tree_it = tree_it.lhs(); 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) 43618845 q std::map< std::string, std::string > attributes; 43818845 q attributes["from"] = "_"; 43918845 q attributes["into"] = "_"; 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"] != "") 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()); 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) 466unsigned int Area_Query_Statement::count_ranges(Resource_Manager& rman) 4680 if (!area_blocks_req_filled) 4710 return area_blocks_req.size(); 475void Area_Query_Statement::get_ranges(std::set< Uint31_Index >& area_blocks_req, Resource_Manager& rman) 4770 if (!area_blocks_req_filled) 4800 area_blocks_req = this->area_blocks_req; 484void Area_Query_Statement::fill_ranges(Resource_Manager& rman) 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) 4920 if (binary_search(area_id.begin(), area_id.end(), it.object().id)) 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)); 4990 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) 51029545 q for (std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator it = input_ways.begin(); 511114545 q it != input_ways.end(); ++it) 513172522 q for (std::vector< Way_Skeleton >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) 51587522 q if (!it2->nds.empty() && it2->nds.front() == it2->nds.back()) 51687522 q way_areas_id.push_back(it2->id); 51929545 q std::sort(way_areas_id.begin(), way_areas_id.end()); 52229545 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = input_areas.begin(); 523498445 q it != input_areas.end(); ++it) 5252820344 q for (std::vector< Area_Skeleton >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) 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)); 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; 5444945 q const Set* input = rman.get_set(area->get_input()); 5460 return prefer_ranges; 548 // Count the indicies of the input areas 5514945 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = input->areas.begin(); 55274145 q it != input->areas.end(); ++it) 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(); 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(); 5784606245 q while (!(area_it == area_blocks_db.discrete_end())) 5804595344 q current_idx = area_it.index().val(); 5814595344 q if (loop_count > 1024*1024) 58322 q rman.health_check(*this); 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)) 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()); 5965430644 q while (nodes_it != nodes.end() && nodes_it->first.val() < current_idx) 59883537 q nodes_it->second.clear(); 6016813544 q while (nodes_it != nodes.end() && 6026813544 q (nodes_it->first.val() & 0xffffff00) == current_idx) 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) 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) 6161117569226 q for (std::vector< Area_Block >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); 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) 627941217826 q else if (check != 0) 63213382124 q into.push_back(*iit); 6372218226 q nodes_it->second.swap(into); 64116945 q while (nodes_it != nodes.end()) 643604 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) 657325109766 q double det = ((double(lat_a1) - lat_a0)*(double(lon_b1) - lon_b0) - (double(lat_b1) - lat_b0)*(double(lon_a1) - lon_a0)); 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)) ? 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)) 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) 68331167535269 q if (lon_b0 < lon_b1) 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); 68814963800966 q else if (lon_b1 < lon_b0) 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); 69339143511 q else // lon_b0 == lon_b1 69539143511 q if (lon_a0 < lon_b0 && lon_b0 < lon_a1) 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)) ? 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) 71332669956 q if (lon_a < lon_b1 && lon_b0 < lon_a) 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); 71930184553 q else if (lon_b1 < lon_b0) 72130133753 q if (lon_a < lon_b0 && lon_b1 < lon_a) 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); 7275089 q else // lon_b0 == lon_b1 7295089 q if (lon_a != lon_b0) 73452 q return ((lat_a0 < lat_b1 && lat_b0 < lat_a1) ? HIT : 0); 73651 q return ((lat_a0 < lat_b0 && lat_b1 < lat_a1) ? HIT : 0); 74151 q return ((lat_a1 < lat_b1 && lat_b0 < lat_a0) ? HIT : 0); 74372 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) 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) 7652729939469 q if (coords_a[i].second < coords_a[i+1].second) 76717456881769 q for (std::vector< std::pair< uint32, uint32 > >::size_type j = 0; j < coords_b.size()-1; ++j) 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); 7761308106469 q else if (coords_a[i+1].second < coords_a[i].second) 77816432250569 q for (std::vector< std::pair< uint32, uint32 > >::size_type j = 0; j < coords_b.size()-1; ++j) 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); 78870564356 q for (std::vector< std::pair< uint32, uint32 > >::size_type j = 0; j < coords_b.size()-1; ++j) 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); 802void has_inner_points(const Area_Block& string_a, const Area_Block& string_b, int& inside) 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) 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) 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) 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); 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) 84830472235 q for (typename std::vector< Way_Skeleton >::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) 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)); 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 8614865545 q while (!(area_it == area_blocks_db.discrete_end())) 8634854244 q current_idx = area_it.index().val(); 8644854244 q if (loop_count > 64*1024) 86612319 q rman.health_check(*this); 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)) 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()); 88017953244 q while (nodes_it != way_coords_to_id.end() && nodes_it->first < current_idx) 88215988044 q while (nodes_it != way_coords_to_id.end() && 88315988044 q (nodes_it->first & 0xffffff00) == current_idx) 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) 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) 8952440829131 q for (std::vector< Area_Block >::const_iterator it2 = it->second.begin(); it2 != it->second.end(); 9001981529931 q int check(Coord_Query_Statement::check_area_block(current_idx, *it2, ilat, ilon)); 9011981529931 q if (check == Coord_Query_Statement::HIT) 90310806623 q inside = Coord_Query_Statement::HIT; 909470105831 q if (inside & (Coord_Query_Statement::TOGGLE_EAST | Coord_Query_Statement::TOGGLE_WEST)) 910187174926 q ways_inside[iit->second] = true; 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) 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) 92427676431 q if (ways_inside[Way::Id_Type(sit->id)]) 926107663227 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 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; 94089931427 q else if (intersect == Coord_Query_Statement::HIT) 9430 ways_inside[Way::Id_Type(sit->id)] = true; 945453823 q area_status[it2->id] = Coord_Query_Statement::HIT; 94889477627 q has_inner_points(*sit, *it2, area_status[it2->id]); 95143873731 q for (std::map< Area::Id_Type, int >::const_iterator it = area_status.begin(); it != area_status.end(); ++it) 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; 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) 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) 96924272733 q if (ways_inside[it2->id]) 9716326826 q cur_result.push_back(*it2); 9726326826 q it2->id = Way::Id_Type(0u); 9756199535 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() 100214145 q constraints.push_back(new Area_Constraint(*this));