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/>. 19#include "../../template_db/block_backend.h" 20#include "../../template_db/random_file.h" 21#include "../core/settings.h" 22#include "../data/collect_members.h" 33//----------------------------------------------------------------------------- 35template < class TIndex, class TObject > 36Ranges< TIndex > ranges(const std::map< TIndex, std::vector< TObject > >& elems) 38114836 q Ranges< TIndex > result; 4121725 q std::pair< TIndex, TIndex > range = std::make_pair(elems.begin()->first, inc(elems.begin()->first)); 4221725 q for (typename std::map< TIndex, std::vector< TObject > >::const_iterator 4321287425 q it = elems.begin(); it != elems.end(); ++it) 4521265725 q if (!(range.second < it->first)) 465280425 q range.second = inc(it->first); 4915985319 q result.push_back(range.first, range.second); 5015985319 q range = std::make_pair(it->first, inc(it->first)); 5321725 q result.push_back(range.first, range.second); 60Ranges< Uint32_Index > ranges(double lat, double lon) 6255380 q Uint32_Index idx = ::ll_upper_(lat, lon); 6355380 q return { idx, inc(idx) }; 67std::vector< std::pair< Uint32_Index, Uint32_Index > > blockwise_split(const Ranges< Uint32_Index >& idxs) 69751116 q std::vector< std::pair< Uint32_Index, Uint32_Index > > result; 712323116 q for (auto it = idxs.begin(); it != idxs.end(); ++it) 731572105 q uint32 start = it.lower_bound().val(); 743779105 q while (start < it.upper_bound().val()) 772207105 q if ((start & 0x3) != 0 || it.upper_bound().val() < start + 0x4) 7989117 q else if ((start & 0x3c) != 0 || it.upper_bound().val() < start + 0x40) 8150010 q else if ((start & 0x3c0) != 0 || it.upper_bound().val() < start + 0x400) 821244 q end = start + 0x40; 833767 q else if ((start & 0x3c00) != 0 || it.upper_bound().val() < start + 0x4000) 84603 q end = start + 0x400; 853164 q else if ((start & 0x3c000) != 0 || it.upper_bound().val() < start + 0x40000) 86201 q end = start + 0x4000; 872963 q else if ((start & 0x3c0000) != 0 || it.upper_bound().val() < start + 0x400000) 88562 q end = start + 0x40000; 892401 q else if ((start & 0x3c00000) != 0 || it.upper_bound().val() < start + 0x4000000) 900 end = start + 0x400000; 922401 q end = start + 0x4000000; 942207105 q result.push_back(std::make_pair(Uint32_Index(start), Uint32_Index(end))); 103Ranges< Uint32_Index > expand(const Ranges< Uint32_Index >& idxs, double radius) 105751116 q std::vector< std::pair< Uint32_Index, Uint32_Index > > blockwise_idxs = blockwise_split(idxs); 107751116 q Ranges< Uint32_Index > result; 1082958116 q for (auto it = blockwise_idxs.begin(); it != blockwise_idxs.end(); ++it) 1102207105 q double south = ::lat(it->first.val(), 0) - radius*(90.0/10/1000/1000); 1112207105 q double north = ::lat(dec(it->second).val(), 0xffffffff) + radius*(90.0/10/1000/1000); 1122207105 q double lon_factor = cos((-north < south ? north : south)*(acos(0)/90.0)); 1132207105 q double west = ::lon(it->first.val(), 0) - radius*(90.0/10/1000/1000)/lon_factor; 1142207105 q double east = ::lon(dec(it->second).val(), 0xffffffff) 1152207105 q + radius*(90.0/10/1000/1000)/lon_factor; 1172207105 q result = result.union_(calc_ranges(south, north, west, east)); 123//----------------------------------------------------------------------------- 125class Around_Constraint : public Query_Constraint 128340116 q Around_Constraint(Around_Statement& around_) : around(&around_), ranges_used(false) {} 130 Query_Filter_Strategy delivers_data(Resource_Manager& rman) 13112398 q { return (around->get_radius() < 2000) ? prefer_ranges : ids_useful; } 133 bool get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges); 134 bool get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges); 136 void filter(Resource_Manager& rman, Set& into); 137 void filter(const Statement& query, Resource_Manager& rman, Set& into); 1380 virtual ~Around_Constraint() {} 141 Around_Statement* around; 146bool Around_Constraint::get_ranges 147 (Resource_Manager& rman, Ranges< Uint32_Index >& ranges) 151751116 q const Set* input = rman.get_set(around->get_source_name()); 152751116 q ranges = around->calc_ranges(input ? *input : Set(), rman); 157bool Around_Constraint::get_ranges 158 (Resource_Manager& rman, Ranges< Uint31_Index >& ranges) 160394116 q Ranges< Uint32_Index > node_ranges; 161394116 q this->get_ranges(rman, node_ranges); 162394116 q ranges = calc_parents(node_ranges); 167void Around_Constraint::filter(Resource_Manager& rman, Set& into) 17012498 q Ranges< Uint32_Index > ranges; 17112498 q get_ranges(rman, ranges); 17312498 q auto ranges_it = ranges.begin(); 17412498 q std::map< Uint32_Index, std::vector< Node_Skeleton > >::iterator nit = into.nodes.begin(); 175275298 q for (; nit != into.nodes.end() && ranges_it != ranges.end(); ) 177262844 q if (!(nit->first < ranges_it.upper_bound())) 179196344 q else if (!(nit->first < ranges_it.lower_bound())) 1830 nit->second.clear(); 18712498 q for (; nit != into.nodes.end(); ++nit) 1880 nit->second.clear(); 19012498 q ranges_it = ranges.begin(); 19112498 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::iterator it = into.attic_nodes.begin(); 192145898 q for (; it != into.attic_nodes.end() && ranges_it != ranges.end(); ) 194133420 q if (!(it->first < ranges_it.upper_bound())) 19697320 q else if (!(it->first < ranges_it.lower_bound())) 20412498 q for (; it != into.attic_nodes.end(); ++it) 20812498 q Ranges< Uint31_Index > ranges; 20912498 q get_ranges(rman, ranges); 211 // pre-process ways to reduce the load of the expensive filter 21312498 q filter_ways_by_ranges(into.ways, ranges); 21412498 q filter_ways_by_ranges(into.attic_ways, ranges); 216 // pre-filter relations 21712498 q filter_relations_by_ranges(into.relations, ranges); 21812498 q filter_relations_by_ranges(into.attic_relations, ranges); 226template< typename Node_Skeleton > 227void filter_nodes_expensive(const Around_Statement& around, 228 std::map< Uint32_Index, std::vector< Node_Skeleton > >& nodes) 230165116 q for (typename std::map< Uint32_Index, std::vector< Node_Skeleton > >::iterator it = nodes.begin(); 233293948 q std::vector< Node_Skeleton > local_into; 234293948 q for (typename std::vector< Node_Skeleton >::const_iterator iit = it->second.begin(); 235102103448 q iit != it->second.end(); ++iit) 237101809547 q double lat(::lat(it->first.val(), iit->ll_lower)); 238101809547 q double lon(::lon(it->first.val(), iit->ll_lower)); 239101809547 q if (around.is_inside(lat, lon)) 24023659946 q local_into.push_back(*iit); 242293948 q it->second.swap(local_into); 247template< typename Way_Skeleton > 248void filter_ways_expensive(const Around_Statement& around, 249 const Way_Geometry_Store& way_geometries, 250 std::map< Uint31_Index, std::vector< Way_Skeleton > >& ways) 252169116 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin(); 255534360 q std::vector< Way_Skeleton > local_into; 256534360 q for (typename std::vector< Way_Skeleton >::const_iterator iit = it->second.begin(); 25726216060 q iit != it->second.end(); ++iit) 25925681758 q if (around.is_inside(way_geometries.get_geometry(*iit))) 2604652054 q local_into.push_back(*iit); 262534360 q it->second.swap(local_into); 267template< typename Relation_Skeleton > 268void filter_relations_expensive(const Around_Statement& around, 269 const std::vector< std::pair< Uint32_Index, const Node_Skeleton* > > node_members_by_id, 270 const std::vector< std::pair< Uint31_Index, const Way_Skeleton* > > way_members_by_id, 271 const Way_Geometry_Store& way_geometries, 272 std::map< Uint31_Index, std::vector< Relation_Skeleton > >& relations) 274155116 q for (typename std::map< Uint31_Index, std::vector< Relation_Skeleton > >::iterator it = relations.begin(); 2751686116 q it != relations.end(); ++it) 277153128 q std::vector< Relation_Skeleton > local_into; 278153128 q for (typename std::vector< Relation_Skeleton >::const_iterator iit = it->second.begin(); 2792401928 q iit != it->second.end(); ++iit) 2812248827 q for (std::vector< Relation_Entry >::const_iterator nit = iit->members.begin(); 282850498427 q nit != iit->members.end(); ++nit) 284848311327 q if (nit->type == Relation_Entry::NODE) 28627401026 q const std::pair< Uint32_Index, const Node_Skeleton* >* second_nd = 28727401026 q binary_search_for_pair_id(node_members_by_id, nit->ref); 290173322 q double lat(::lat(second_nd->first.val(), second_nd->second->ll_lower)); 291173322 q double lon(::lon(second_nd->first.val(), second_nd->second->ll_lower)); 293173322 q if (around.is_inside(lat, lon)) 2951558 q local_into.push_back(*iit); 299820910327 q else if (nit->type == Relation_Entry::WAY) 301818185027 q const std::pair< Uint31_Index, const Way_Skeleton* >* second_nd = 302818185027 q binary_search_for_pair_id(way_members_by_id, nit->ref32()); 3052392927 q if (around.is_inside(way_geometries.get_geometry(*second_nd->second))) 30746221 q local_into.push_back(*iit); 313153128 q it->second.swap(local_into); 318void Around_Constraint::filter(const Statement& query, Resource_Manager& rman, Set& into) 320143116 q const Set* input = rman.get_set(around->get_source_name()); 321143116 q around->calc_lat_lons(input ? *input : Set(), *around, rman); 323143116 q filter_nodes_expensive(*around, into.nodes); 324143116 q filter_ways_expensive(*around, Way_Geometry_Store(into.ways, query, rman), into.ways); 326143116 q Request_Context context(&query, rman); 330 // Retrieve all node and way members referred by the relations. 331143116 q Ranges< Uint32_Index > node_ranges; 332143116 q get_ranges(rman, node_ranges); 334143116 q Timeless< Uint32_Index, Node_Skeleton > node_members 335143116 q = relation_node_members(context, into.relations, {}, node_ranges, {}, true); 336143116 q std::vector< std::pair< Uint32_Index, const Node_Skeleton* > > node_members_by_id 337143116 q = order_by_id(node_members.current, Order_By_Node_Id()); 339 // Retrieve all ways referred by the relations. 340143116 q Ranges< Uint31_Index > way_ranges; 341143116 q get_ranges(rman, way_ranges); 343143116 q Timeless< Uint31_Index, Way_Skeleton > way_members_ 344143116 q = relation_way_members(context, into.relations, {}, way_ranges, {}, true); 345143116 q std::vector< std::pair< Uint31_Index, const Way_Skeleton* > > way_members_by_id 346143116 q = order_by_id(way_members_.current, Order_By_Way_Id()); 348 // Retrieve all nodes referred by the ways. 349143116 q filter_relations_expensive(*around, node_members_by_id, way_members_by_id, 350143116 q Way_Geometry_Store(way_members_.current, query, rman), into.relations); 353143116 q if (!into.attic_nodes.empty()) 3542220 q filter_nodes_expensive(*around, into.attic_nodes); 356143116 q if (!into.attic_ways.empty()) 3572624 q filter_ways_expensive(*around, Way_Geometry_Store(into.attic_ways, query, rman), into.attic_ways); 359143116 q if (!into.attic_relations.empty()) 3621212 q Ranges< Uint32_Index > node_ranges; 3631212 q get_ranges(rman, node_ranges); 3651212 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > > node_members 3661212 q = relation_node_members(context, into.attic_relations, node_ranges); 3671212 q std::vector< std::pair< Uint32_Index, const Node_Skeleton* > > node_members_by_id 3681212 q = order_attic_by_id(node_members, Order_By_Node_Id()); 370 // Retrieve all ways referred by the relations. 3711212 q Ranges< Uint31_Index > way_ranges; 3721212 q get_ranges(rman, way_ranges); 3741212 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > way_members_ 3751212 q = relation_way_members(context, into.attic_relations, way_ranges); 3761212 q std::vector< std::pair< Uint31_Index, const Way_Skeleton* > > way_members_by_id 3771212 q = order_attic_by_id(way_members_, Order_By_Way_Id()); 3791212 q filter_relations_expensive(*around, node_members_by_id, way_members_by_id, 3801212 q Way_Geometry_Store(way_members_, query, rman), into.attic_relations); 386//----------------------------------------------------------------------------- 388Around_Statement::Statement_Maker Around_Statement::statement_maker; 389Around_Statement::Criterion_Maker Around_Statement::criterion_maker; 392Statement* Around_Statement::Criterion_Maker::create_criterion(const Token_Node_Ptr& input_tree, 393 const std::string& type, const std::string& into, 394 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output) 396504117 q Token_Node_Ptr tree_it = input_tree; 397504117 q uint line_nr = tree_it->line_col.first; 398504117 q std::vector< std::pair< std::string, std::string > > coords; 400892117 q while (tree_it->token == "," && tree_it->rhs && tree_it->lhs) 40238883 q std::string lon = tree_it.rhs()->token; 40338883 q tree_it = tree_it.lhs(); 40538883 q if (tree_it->token != "," || !tree_it->rhs || !tree_it->lhs) 4080 error_output->add_parse_error("around requires an odd number of arguments", line_nr); 41238883 q coords.push_back(std::make_pair(tree_it.rhs()->token, lon)); 41338883 q tree_it = tree_it.lhs(); 416504117 q if (tree_it->token == ":" && tree_it->rhs) 418504117 q std::string radius = decode_json(tree_it.rhs()->token, error_output); 422504117 q if (tree_it->token == "." && tree_it->rhs) 42313232 q from = tree_it.rhs()->token; 425504117 q std::map< std::string, std::string > attributes; 428504117 q attributes["radius"] = radius; 43135681 q attributes["lat"] = coords.front().first; 43235681 q attributes["lon"] = coords.front().second; 43414836 q else if (!coords.empty()) 43682 q std::reverse(coords.begin(),coords.end()); 43782 q for (std::vector< std::pair< std::string, std::string > >::const_iterator it = coords.begin(); 438402 q it != coords.end(); ++it) 439322 q attributes["polyline"] += it->first + "," + it->second + ","; 44082 q attributes["polyline"].resize(attributes["polyline"].size()-1); 442504117 q return new Around_Statement(line_nr, attributes, global_settings); 4440 else if (error_output) 4450 error_output->add_parse_error("around requires the radius as first argument", line_nr); 451Around_Statement::Around_Statement 452 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings) 453504117 q : Output_Statement(line_number_) 455504117 q std::map< std::string, std::string > attributes; 462504117 q attributes["polyline"] = ""; 464504117 q eval_attributes_array(get_name(), attributes, input_attributes); 467504117 q set_output(attributes["into"]); 469504117 q radius = atof(attributes["radius"].c_str()); 470504117 q if ((radius < 0.0) || (attributes["radius"] == "")) 4720 std::ostringstream temp; 4730 temp<<"For the attribute \"radius\" of the element \"around\"" 4740 <<" the only allowed values are nonnegative floats."; 4750 add_static_error(temp.str()); 480504117 q if (attributes["lat"] != "") 48235681 q lat = atof(attributes["lat"].c_str()); 48335681 q if ((lat < -90.0) || (lat > 90.0)) 4840 add_static_error("For the attribute \"lat\" of the element \"around\"" 4850 " the only allowed values are floats between -90.0 and 90.0 or an empty value."); 488504117 q if (attributes["lon"] != "") 49035681 q lon = atof(attributes["lon"].c_str()); 49135681 q if ((lon < -180.0) || (lon > 180.0)) 4920 add_static_error("For the attribute \"lon\" of the element \"around\"" 4930 " the only allowed values are floats between -1800.0 and 180.0 or an empty value."); 496504117 q if (attributes["polyline"] != "") 49882 q if (attributes["lat"] != "" || attributes["lon"] != "") 4990 add_static_error("In \"around\", the attribute \"polyline\" cannot be used if \"lat\" or \"lon\" are used."); 50182 q std::string& polystring = attributes["polyline"]; 50282 q std::string::size_type from = 0; 50382 q std::string::size_type to = polystring.find(","); 504402 q while (to != std::string::npos) 506322 q double lat = atof(polystring.substr(from, to).c_str()); 508322 q to = polystring.find(",", from); 509322 q if (to != std::string::npos) 511242 q points.push_back(Point_Double(lat, atof(polystring.substr(from, to).c_str()))); 513242 q to = polystring.find(",", from); 51682 q points.push_back(Point_Double(lat, atof(polystring.substr(from).c_str()))); 51982 q if ((points.back().lat < -90.0) || (points.back().lat > 90.0)) 5200 add_static_error("For a latitude entry in the attribute \"polyline\" of the element \"around\"" 5210 " the only allowed values are floats between -90.0 and 90.0 or an empty value."); 52282 q if ((points.back().lon < -180.0) || (points.back().lon > 180.0)) 5230 add_static_error("For a latitude entry in the attribute \"polyline\" of the element \"around\"" 5240 " the only allowed values are floats between -1800.0 and 180.0 or an empty value."); 52735681 q points.push_back(Point_Double(lat, lon)); 530Around_Statement::~Around_Statement() 532504117 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin(); 533825117 q it != constraints.end(); ++it) 538std::vector< double > cartesian(double lat, double lon) 54010237309104 q std::vector< double > result(3); 54210237309104 q result[0] = sin(lat/90.0*acos(0)); 54310237309104 q result[1] = cos(lat/90.0*acos(0))*sin(lon/90.0*acos(0)); 54410237309104 q result[2] = cos(lat/90.0*acos(0))*cos(lon/90.0*acos(0)); 550void rescale(double a, std::vector< double >& v) 558std::vector< double > sum(const std::vector< double >& v, const std::vector< double >& w) 5602122370547 q std::vector< double > result(3); 5622122370547 q result[0] = v[0] + w[0]; 5632122370547 q result[1] = v[1] + w[1]; 5642122370547 q result[2] = v[2] + w[2]; 5662122370547 q return result; 570double scalar_prod(const std::vector< double >& v, const std::vector< double >& w) 57283237000571 q return v[0]*w[0] + v[1]*w[1] + v[2]*w[2]; 576std::vector< double > cross_prod(const std::vector< double >& v, const std::vector< double >& w) 57810915856779 q std::vector< double > result(3); 58010915856779 q result[0] = v[1]*w[2] - v[2]*w[1]; 58110915856779 q result[1] = v[2]*w[0] - v[0]*w[2]; 58210915856779 q result[2] = v[0]*w[1] - v[1]*w[0]; 589Prepared_Segment::Prepared_Segment 590 (double first_lat_, double first_lon_, double second_lat_, double second_lon_) 591304004079 q : first_lat(first_lat_), first_lon(first_lon_), second_lat(second_lat_), second_lon(second_lon_) 593304004079 q first_cartesian = cartesian(first_lat, first_lon); 594304004079 q second_cartesian = cartesian(second_lat, second_lon); 595304004079 q norm = cross_prod(first_cartesian, second_cartesian); 599Prepared_Point::Prepared_Point 600 (double lat_, double lon_) 603272215103 q cartesian = ::cartesian(lat, lon); 607double great_circle_line_dist(const Prepared_Segment& segment, const std::vector< double >& cartesian) 60925691403971 q double scalar_prod_ = std::abs(scalar_prod(cartesian, segment.norm)) 61025691403971 q /sqrt(scalar_prod(segment.norm, segment.norm)); 61225691403971 q if (scalar_prod_ > 1) 61525691403971 q return asin(scalar_prod_)*(10*1000*1000/acos(0)); 619double great_circle_line_dist(double llat1, double llon1, double llat2, double llon2, 620 double plat, double plon) 6220 std::vector< double > norm = cross_prod(cartesian(llat1, llon1), cartesian(llat2, llon2)); 6240 double scalar_prod_ = std::abs(scalar_prod(cartesian(plat, plon), norm)) 6250 /sqrt(scalar_prod(norm, norm)); 6270 if (scalar_prod_ > 1) 6300 return asin(scalar_prod_)*(10*1000*1000/acos(0)); 634bool intersect(const Prepared_Segment& segment_a, 635 const Prepared_Segment& segment_b) 6371061185277 q std::vector< double > intersection_pt = cross_prod(segment_a.norm, segment_b.norm); 6381061185277 q rescale(1.0/sqrt(scalar_prod(intersection_pt, intersection_pt)), intersection_pt); 6401061185277 q std::vector< double > asum = sum(segment_a.first_cartesian, segment_a.second_cartesian); 6411061185277 q std::vector< double > bsum = sum(segment_b.first_cartesian, segment_b.second_cartesian); 6431061185277 q return (std::abs(scalar_prod(asum, intersection_pt)) >= scalar_prod(asum, segment_a.first_cartesian) 6441061185277 q && std::abs(scalar_prod(bsum, intersection_pt)) >= scalar_prod(bsum, segment_b.first_cartesian)); 648bool intersect(double alat1, double alon1, double alat2, double alon2, 649 double blat1, double blon1, double blat2, double blon2) 6510 std::vector< double > a1 = cartesian(alat1, alon1); 6520 std::vector< double > a2 = cartesian(alat2, alon2); 6530 std::vector< double > norm_a = cross_prod(a1, a2); 6540 std::vector< double > b1 = cartesian(blat1, blon1); 6550 std::vector< double > b2 = cartesian(blat2, blon2); 6560 std::vector< double > norm_b = cross_prod(b1, b2); 6580 std::vector< double > intersection_pt = cross_prod(norm_a, norm_b); 6590 rescale(1.0/sqrt(scalar_prod(intersection_pt, intersection_pt)), intersection_pt); 6610 std::vector< double > asum = sum(a1, a2); 6620 std::vector< double > bsum = sum(b1, b2); 6640 return (std::abs(scalar_prod(asum, intersection_pt)) >= scalar_prod(asum, a1) 6650 && std::abs(scalar_prod(bsum, intersection_pt)) >= scalar_prod(bsum, b1)); 669Ranges< Uint32_Index > Around_Statement::calc_ranges(const Set& input, Resource_Manager& rman) const 67255380 q return expand(ranges(points[0].lat, points[0].lon), radius); 67419836 q else if (points.size() > 1) 67682 q std::vector< uint32 > nd_idxs; 67782 q std::map< Uint31_Index, std::vector< Way_Skeleton > > ways; 67882 q std::pair< Uint31_Index, std::vector< Way_Skeleton > > way; 680362 q for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it) 681282 q nd_idxs.push_back(::ll_upper_(it->lat, it->lon)); 68382 q Uint31_Index idx = Way::calc_index(nd_idxs); 68482 q way = std::make_pair(idx, std::vector< Way_Skeleton >()); 68682 q return expand(calc_children(ranges(ways)), radius); 69019034 q ranges(input.nodes).union_(ranges(input.attic_nodes)).union_( 69219034 q ranges(input.ways).union_(ranges(input.attic_ways)).union_( 69319034 q ranges(input.relations).union_(ranges(input.attic_relations))))), 698void add_coord(double lat, double lon, double radius, 699 std::map< Uint32_Index, std::vector< Point_Double > >& radius_lat_lons, 700 std::vector< Prepared_Point >& simple_lat_lons) 702272215103 q double south = lat - radius*(360.0/(40000.0*1000.0)); 703272215103 q double north = lat + radius*(360.0/(40000.0*1000.0)); 704272215103 q double scale_lat = lat > 0.0 ? north : south; 705272215103 q if (std::abs(scale_lat) >= 89.9) 707272215103 q double west = lon - radius*(360.0/(40000.0*1000.0))/cos(scale_lat/90.0*acos(0)); 708272215103 q double east = lon + radius*(360.0/(40000.0*1000.0))/cos(scale_lat/90.0*acos(0)); 710272215103 q simple_lat_lons.push_back(Prepared_Point(lat, lon)); 712272215103 q auto uint_ranges = calc_ranges(south, north, west, east); 713635159103 q for (auto it = uint_ranges.begin(); it != uint_ranges.end(); ++it) 715820453103 q for (uint32 idx = it.lower_bound().val(); idx < it.upper_bound().val(); ++idx) 716457509103 q radius_lat_lons[idx].push_back(Point_Double(lat, lon)); 721void add_node(Uint32_Index idx, const Node_Skeleton& node, double radius, 722 std::map< Uint32_Index, std::vector< Point_Double > >& radius_lat_lons, 723 std::vector< Prepared_Point >& simple_lat_lons) 7252814311 q add_coord(::lat(idx.val(), node.ll_lower), ::lon(idx.val(), node.ll_lower), 7262814311 q radius, radius_lat_lons, simple_lat_lons); 730void add_way(const std::vector< Quad_Coord >& way_geometry, double radius, 731 std::map< Uint32_Index, std::vector< Point_Double > >& radius_lat_lons, 732 std::vector< Prepared_Point >& simple_lat_lons, 733 std::vector< Prepared_Segment >& simple_segments) 73727430716 q for (std::vector< Quad_Coord >::const_iterator nit = way_geometry.begin(); nit != way_geometry.end(); ++nit) 73824396416 q add_coord(::lat(nit->ll_upper, nit->ll_lower), 73924396416 q ::lon(nit->ll_upper, nit->ll_lower), 74024396416 q radius, radius_lat_lons, simple_lat_lons); 7443034316 q std::vector< Quad_Coord >::const_iterator nit = way_geometry.begin(); 7453034316 q if (nit == way_geometry.end()) 7483034316 q double first_lat(::lat(nit->ll_upper, nit->ll_lower)); 7493034316 q double first_lon(::lon(nit->ll_upper, nit->ll_lower)); 75124396416 q for (++nit; nit != way_geometry.end(); ++nit) 75321362116 q double second_lat(::lat(nit->ll_upper, nit->ll_lower)); 75421362116 q double second_lon(::lon(nit->ll_upper, nit->ll_lower)); 75621362116 q simple_segments.push_back(Prepared_Segment(first_lat, first_lon, second_lat, second_lon)); 75821362116 q first_lat = second_lat; 75921362116 q first_lon = second_lon; 763void add_way(const std::vector< Point_Double >& points, double radius, 764 std::map< Uint32_Index, std::vector< Point_Double > >& radius_lat_lons, 765 std::vector< Prepared_Point >& simple_lat_lons, 766 std::vector< Prepared_Segment >& simple_segments) 769102 q for (std::vector< Point_Double >::const_iterator nit = points.begin(); nit != points.end(); ++nit) 77182 q double lat = nit->lat; 77282 q double lon = nit->lon; 77382 q add_coord(lat, lon, radius, radius_lat_lons, simple_lat_lons); 77822 q std::vector< Point_Double >::const_iterator nit = points.begin(); 77922 q if (nit == points.end()) 78222 q double first_lat(nit->lat); 78322 q double first_lon(nit->lon); 78582 q for (++nit; nit != points.end(); ++nit) 78762 q double second_lat(nit->lat); 78862 q double second_lon(nit->lon); 79062 q simple_segments.push_back(Prepared_Segment(first_lat, first_lon, second_lat, second_lon)); 79262 q first_lat = second_lat; 79362 q first_lon = second_lon; 798template< typename Node_Skeleton > 799void Around_Statement::add_nodes(const std::map< Uint32_Index, std::vector< Node_Skeleton > >& nodes) 80112634 q for (typename std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator iit(nodes.begin()); 802104334 q iit != nodes.end(); ++iit) 80491711 q for (typename std::vector< Node_Skeleton >::const_iterator nit(iit->second.begin()); 8052906011 q nit != iit->second.end(); ++nit) 8062814311 q add_node(iit->first, *nit, radius, radius_lat_lons, simple_lat_lons); 811template< typename Way_Skeleton > 812void Around_Statement::add_ways(const std::map< Uint31_Index, std::vector< Way_Skeleton > >& ways, 813 const Way_Geometry_Store& way_geometries) 81512634 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator it = ways.begin(); 81880117 q for (typename std::vector< Way_Skeleton >::const_iterator iit = it->second.begin(); 8193114417 q iit != it->second.end(); ++iit) 8203034316 q add_way(way_geometries.get_geometry(*iit), radius, 8213034316 q radius_lat_lons, simple_lat_lons, simple_segments); 826void Around_Statement::calc_lat_lons(const Set& input, Statement& query, Resource_Manager& rman) 83510080 q add_coord(points[0].lat, points[0].lon, radius, radius_lat_lons, simple_lat_lons); 8384336 q else if (points.size() > 1) 84022 q add_way(points, radius, radius_lat_lons, simple_lat_lons, simple_segments); 8434134 q Request_Context context(&query, rman); 8464134 q add_ways(input.ways, Way_Geometry_Store(input.ways, query, rman)); 848 // Retrieve all node and way members referred by the relations. 8494134 q add_nodes(relation_node_members(context, input.relations, {}, Ranges< Uint32_Index >::global(), {}, true).current); 851 // Retrieve all ways referred by the relations. 8524134 q Timeless< Uint31_Index, Way_Skeleton > way_members 8534134 q = relation_way_members(context, input.relations, {}, Ranges< Uint31_Index >::global(), {}, true); 8544134 q add_ways(way_members.current, Way_Geometry_Store(way_members.current, query, rman)); 8564134 q if (rman.get_desired_timestamp() != NOW) 8582219 q add_nodes(input.attic_nodes); 8592219 q add_ways(input.attic_ways, Way_Geometry_Store(input.attic_ways, query, rman)); 861 // Retrieve all node and way members referred by the relations. 8622219 q add_nodes(relation_node_members( 8632219 q context, input.attic_relations, relation_node_member_indices({}, input.attic_relations))); 865 // Retrieve all ways referred by the relations. 8662219 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > way_members 8672219 q = relation_way_members(context, input.attic_relations, Ranges< Uint31_Index >::global()); 8682219 q add_ways(way_members, Way_Geometry_Store(way_members, query, rman)); 873bool Around_Statement::is_inside(double lat, double lon) const 875416864591 q std::map< Uint32_Index, std::vector< Point_Double > >::const_iterator mit 876416864591 q = radius_lat_lons.find(::ll_upper_(lat, lon)); 877416864591 q if (mit != radius_lat_lons.end()) 87999456489 q for (std::vector< Point_Double >::const_iterator cit = mit->second.begin(); 880327531789 q cit != mit->second.end(); ++cit) 882256438489 q if ((radius > 0 && great_circle_dist(cit->lat, cit->lon, lat, lon) <= radius) 883256438489 q || (std::abs(cit->lat - lat) < 1e-7 && std::abs(cit->lon - lon) < 1e-7)) 888388501491 q std::vector< double > coord_cartesian = cartesian(lat, lon); 889388501491 q for (std::vector< Prepared_Segment >::const_iterator 89013094161691 q it = simple_segments.begin(); it != simple_segments.end(); ++it) 89212705667011 q if (great_circle_line_dist(*it, coord_cartesian) <= radius) 89424460207 q double gcdist = great_circle_dist 89524460207 q (it->first_lat, it->first_lon, it->second_lat, it->second_lon); 89624460207 q double limit = sqrt(gcdist*gcdist + radius*radius); 89724460207 q if (great_circle_dist(lat, lon, it->first_lat, it->first_lon) <= limit && 89824460207 q great_circle_dist(lat, lon, it->second_lat, it->second_lon) <= limit) 906bool Around_Statement::is_inside 907 (double first_lat, double first_lon, double second_lat, double second_lon) const 909282641368 q Prepared_Segment segment(first_lat, first_lon, second_lat, second_lon); 911282641368 q for (std::vector< Prepared_Point >::const_iterator cit = simple_lat_lons.begin(); 91213268374968 q cit != simple_lat_lons.end(); ++cit) 91412985736967 q if (great_circle_line_dist(segment, cit->cartesian) <= radius) 916313723165 q double gcdist = great_circle_dist(first_lat, first_lon, second_lat, second_lon); 917313723165 q double limit = sqrt(gcdist*gcdist + radius*radius); 918313723165 q if (great_circle_dist(cit->lat, cit->lon, first_lat, first_lon) <= limit && 919313723165 q great_circle_dist(cit->lat, cit->lon, second_lat, second_lon) <= limit) 924282638068 q for (std::vector< Prepared_Segment >::const_iterator 92510894490368 q cit = simple_segments.begin(); cit != simple_segments.end(); ++cit) 9271061185277 q if (intersect(*cit, segment)) 935bool Around_Statement::is_inside(const std::vector< Quad_Coord >& way_geometry) const 93728074668 q std::vector< Quad_Coord >::const_iterator nit = way_geometry.begin(); 93828074668 q if (nit == way_geometry.end()) 941 // Pre-check if a node is inside 942338247768 q for (std::vector< Quad_Coord >::const_iterator it = way_geometry.begin(); it != way_geometry.end(); ++it) 944314881768 q double second_lat(::lat(it->ll_upper, it->ll_lower)); 945314881768 q double second_lon(::lon(it->ll_upper, it->ll_lower)); 947314881768 q if (is_inside(second_lat, second_lon)) 95123366068 q double first_lat(::lat(nit->ll_upper, nit->ll_lower)); 95223366068 q double first_lon(::lon(nit->ll_upper, nit->ll_lower)); 954306003668 q for (++nit; nit != way_geometry.end(); ++nit) 956282641368 q double second_lat(::lat(nit->ll_upper, nit->ll_lower)); 957282641368 q double second_lon(::lon(nit->ll_upper, nit->ll_lower)); 959282641368 q if (is_inside(first_lat, first_lon, second_lat, second_lon)) 962282637668 q first_lat = second_lat; 963282637668 q first_lon = second_lon; 969void Around_Statement::execute(Resource_Manager& rman) 9731918 q Around_Constraint constraint(*this); 9741918 q Ranges< Uint32_Index > ranges; 9751918 q constraint.get_ranges(rman, ranges); 9761918 q get_elements_from_db< Uint32_Index, Node_Skeleton >(ranges, *this, rman) 9771918 q .swap(into.nodes, into.attic_nodes); 9781918 q constraint.filter(*this, rman, into); 9791918 q filter_attic_elements(rman, rman.get_desired_timestamp(), into.nodes, into.attic_nodes); 9811918 q transfer_output(rman, into); 9821918 q rman.health_check(*this); 986Query_Constraint* Around_Statement::get_query_constraint() 98832198 q constraints.push_back(new Around_Constraint(*this)); 98932198 q return constraints.back();