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

overpass_api/statements/around.cc

603 of 665 instrumented lines run by the suite (90.7%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#include "../../template_db/block_backend.h"
20#include "../../template_db/random_file.h"
21#include "../core/settings.h"
22#include "../data/collect_members.h"
23#include "around.h"
24#include "recurse.h"
26#include <algorithm>
27#include <cmath>
28#include <fstream>
29#include <sstream>
30#include <vector>
33//-----------------------------------------------------------------------------
35template < class TIndex, class TObject >
36Ranges< TIndex > ranges(const std::map< TIndex, std::vector< TObject > >& elems)
3789632 q{
3889632 q Ranges< TIndex > result;
3989632 q if (elems.empty())
4072528 q return result;
4117123 q std::pair< TIndex, TIndex > range = std::make_pair(elems.begin()->first, inc(elems.begin()->first));
4217123 q for (typename std::map< TIndex, std::vector< TObject > >::const_iterator
439248523 q it = elems.begin(); it != elems.end(); ++it)
449231423 q {
459231423 q if (!(range.second < it->first))
461114323 q range.second = inc(it->first);
478117116 q else
488117116 q {
498117116 q result.push_back(range.first, range.second);
508117116 q range = std::make_pair(it->first, inc(it->first));
518117116 q }
529231423 q }
5317123 q result.push_back(range.first, range.second);
5417123 q result.sort();
5617123 q return result;
5789632 q}
60Ranges< Uint32_Index > ranges(double lat, double lon)
6143065 q{
6243065 q Uint32_Index idx = ::ll_upper_(lat, lon);
6343065 q return { idx, inc(idx) };
6443065 q}
67std::vector< std::pair< Uint32_Index, Uint32_Index > > blockwise_split(const Ranges< Uint32_Index >& idxs)
6859694 q{
6959694 q std::vector< std::pair< Uint32_Index, Uint32_Index > > result;
71194394 q for (auto it = idxs.begin(); it != idxs.end(); ++it)
72134785 q {
73134785 q uint32 start = it.lower_bound().val();
74351885 q while (start < it.upper_bound().val())
75217185 q {
76217185 q uint32 end;
77217185 q if ((start & 0x3) != 0 || it.upper_bound().val() < start + 0x4)
78105376 q end = start + 1;
79111815 q else if ((start & 0x3c) != 0 || it.upper_bound().val() < start + 0x40)
804399 q end = start + 0x4;
8167911 q else if ((start & 0x3c0) != 0 || it.upper_bound().val() < start + 0x400)
821914 q end = start + 0x40;
8348810 q else if ((start & 0x3c00) != 0 || it.upper_bound().val() < start + 0x4000)
841044 q end = start + 0x400;
853847 q else if ((start & 0x3c000) != 0 || it.upper_bound().val() < start + 0x40000)
86502 q end = start + 0x4000;
873345 q else if ((start & 0x3c0000) != 0 || it.upper_bound().val() < start + 0x400000)
88663 q end = start + 0x40000;
892682 q else if ((start & 0x3c00000) != 0 || it.upper_bound().val() < start + 0x4000000)
90281 q end = start + 0x400000;
912401 q else
922401 q end = start + 0x4000000;
94217185 q result.push_back(std::make_pair(Uint32_Index(start), Uint32_Index(end)));
95217185 q start = end;
96217185 q }
97134785 q }
9959694 q return result;
103Ranges< Uint32_Index > expand(const Ranges< Uint32_Index >& idxs, double radius)
10559694 q std::vector< std::pair< Uint32_Index, Uint32_Index > > blockwise_idxs = blockwise_split(idxs);
10759694 q Ranges< Uint32_Index > result;
108276794 q for (auto it = blockwise_idxs.begin(); it != blockwise_idxs.end(); ++it)
109217185 q {
110217185 q double south = ::lat(it->first.val(), 0) - radius*(90.0/10/1000/1000);
111217185 q double north = ::lat(dec(it->second).val(), 0xffffffff) + radius*(90.0/10/1000/1000);
112217185 q double lon_factor = cos((-north < south ? north : south)*(acos(0)/90.0));
113217185 q double west = ::lon(it->first.val(), 0) - radius*(90.0/10/1000/1000)/lon_factor;
114217185 q double east = ::lon(dec(it->second).val(), 0xffffffff)
115217185 q + radius*(90.0/10/1000/1000)/lon_factor;
117217185 q result = result.union_(calc_ranges(south, north, west, east));
118217185 q }
12059694 q return result;
123//-----------------------------------------------------------------------------
125class Around_Constraint : public Query_Constraint
127 public:
12827494 q Around_Constraint(Around_Statement& around_) : around(&around_), ranges_used(false) {}
130 Query_Filter_Strategy delivers_data(Resource_Manager& rman)
1319271 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() {}
140 private:
141 Around_Statement* around;
142 bool ranges_used;
143};
146bool Around_Constraint::get_ranges
147 (Resource_Manager& rman, Ranges< Uint32_Index >& ranges)
14959694 q ranges_used = true;
15159694 q const Set* input = rman.get_set(around->get_source_name());
15259694 q ranges = around->calc_ranges(input ? *input : Set(), rman);
15359694 q return true;
157bool Around_Constraint::get_ranges
158 (Resource_Manager& rman, Ranges< Uint31_Index >& ranges)
16030894 q Ranges< Uint32_Index > node_ranges;
16130894 q this->get_ranges(rman, node_ranges);
16230894 q ranges = calc_parents(node_ranges);
16330894 q return true;
167void Around_Constraint::filter(Resource_Manager& rman, Set& into)
1709271 q Ranges< Uint32_Index > ranges;
1719271 q get_ranges(rman, ranges);
1739271 q auto ranges_it = ranges.begin();
1749271 q std::map< Uint32_Index, std::vector< Node_Skeleton > >::iterator nit = into.nodes.begin();
17536311471 q for (; nit != into.nodes.end() && ranges_it != ranges.end(); )
17636302235 q {
17736302235 q if (!(nit->first < ranges_it.upper_bound()))
178793235 q ++ranges_it;
17935509035 q else if (!(nit->first < ranges_it.lower_bound()))
18035509035 q ++nit;
1810 else
1820 {
1830 nit->second.clear();
1840 ++nit;
1850 }
18636302235 q }
1879271 q for (; nit != into.nodes.end(); ++nit)
1880 nit->second.clear();
1909271 q ranges_it = ranges.begin();
1919271 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::iterator it = into.attic_nodes.begin();
1924922971 q for (; it != into.attic_nodes.end() && ranges_it != ranges.end(); )
1934913715 q {
1944913715 q if (!(it->first < ranges_it.upper_bound()))
195247415 q ++ranges_it;
1964666315 q else if (!(it->first < ranges_it.lower_bound()))
1974666315 q ++it;
1980 else
1990 {
2000 it->second.clear();
2010 ++it;
2020 }
2034913715 q }
2049271 q for (; it != into.attic_nodes.end(); ++it)
2050 it->second.clear();
2089271 q Ranges< Uint31_Index > ranges;
2099271 q get_ranges(rman, ranges);
211 // pre-process ways to reduce the load of the expensive filter
212 // pre-filter ways
2139271 q filter_ways_by_ranges(into.ways, ranges);
2149271 q filter_ways_by_ranges(into.attic_ways, ranges);
216 // pre-filter relations
2179271 q filter_relations_by_ranges(into.relations, ranges);
2189271 q filter_relations_by_ranges(into.attic_relations, ranges);
2209271 q ranges_used = false;
222 //TODO: areas
226template< typename Node_Skeleton >
227void filter_nodes_expensive(const Around_Statement& around,
228 std::map< Uint32_Index, std::vector< Node_Skeleton > >& nodes)
23013594 q for (typename std::map< Uint32_Index, std::vector< Node_Skeleton > >::iterator it = nodes.begin();
2314821294 q it != nodes.end(); ++it)
2324807749 q {
2334807749 q std::vector< Node_Skeleton > local_into;
2344807749 q for (typename std::vector< Node_Skeleton >::const_iterator iit = it->second.begin();
235332870649 q iit != it->second.end(); ++iit)
236328062947 q {
237328062947 q double lat(::lat(it->first.val(), iit->ll_lower));
238328062947 q double lon(::lon(it->first.val(), iit->ll_lower));
239328062947 q if (around.is_inside(lat, lon))
2402736744 q local_into.push_back(*iit);
241328062947 q }
2424807749 q it->second.swap(local_into);
2434807749 q }
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)
25213594 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin();
2531433594 q it != ways.end(); ++it)
2541420041 q {
2551420041 q std::vector< Way_Skeleton > local_into;
2561420041 q for (typename std::vector< Way_Skeleton >::const_iterator iit = it->second.begin();
2575338541 q iit != it->second.end(); ++iit)
2583918539 q {
2593918539 q if (around.is_inside(way_geometries.get_geometry(*iit)))
260431137 q local_into.push_back(*iit);
2613918539 q }
2621420041 q it->second.swap(local_into);
2631420041 q }
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)
27412694 q for (typename std::map< Uint31_Index, std::vector< Relation_Skeleton > >::iterator it = relations.begin();
27595694 q it != relations.end(); ++it)
27683025 q {
27783025 q std::vector< Relation_Skeleton > local_into;
27883025 q for (typename std::vector< Relation_Skeleton >::const_iterator iit = it->second.begin();
279751325 q iit != it->second.end(); ++iit)
280668325 q {
281668325 q for (std::vector< Relation_Entry >::const_iterator nit = iit->members.begin();
28286535425 q nit != iit->members.end(); ++nit)
28385894425 q {
28485894425 q if (nit->type == Relation_Entry::NODE)
285551123 q {
286551123 q const std::pair< Uint32_Index, const Node_Skeleton* >* second_nd =
287551123 q binary_search_for_pair_id(node_members_by_id, nit->ref);
288551123 q if (!second_nd)
289543923 q continue;
2907210 q double lat(::lat(second_nd->first.val(), second_nd->second->ll_lower));
2917210 q double lon(::lon(second_nd->first.val(), second_nd->second->ll_lower));
2937210 q if (around.is_inside(lat, lon))
294154 q {
295154 q local_into.push_back(*iit);
296154 q break;
297154 q }
29985343325 q else if (nit->type == Relation_Entry::WAY)
30084026925 q {
30184026925 q const std::pair< Uint31_Index, const Way_Skeleton* >* second_nd =
30284026925 q binary_search_for_pair_id(way_members_by_id, nit->ref32());
30384026925 q if (!second_nd)
30483295225 q continue;
305731723 q if (around.is_inside(way_geometries.get_geometry(*second_nd->second)))
30625818 q {
30725818 q local_into.push_back(*iit);
30825818 q break;
30925818 q }
310731723 q }
31185894425 q }
312668325 q }
31383025 q it->second.swap(local_into);
31483025 q }
318void Around_Constraint::filter(const Statement& query, Resource_Manager& rman, Set& into)
32011794 q const Set* input = rman.get_set(around->get_source_name());
32111794 q around->calc_lat_lons(input ? *input : Set(), *around, rman);
32311794 q filter_nodes_expensive(*around, into.nodes);
32411794 q filter_ways_expensive(*around, Way_Geometry_Store(into.ways, query, rman), into.ways);
32611794 q Request_Context context(&query, rman);
32711794 q {
328 //Process relations
330 // Retrieve all node and way members referred by the relations.
33111794 q Ranges< Uint32_Index > node_ranges;
33211794 q get_ranges(rman, node_ranges);
33411794 q Timeless< Uint32_Index, Node_Skeleton > node_members
33511794 q = relation_node_members(context, into.relations, {}, node_ranges, {}, true);
33611794 q std::vector< std::pair< Uint32_Index, const Node_Skeleton* > > node_members_by_id
33711794 q = order_by_id(node_members.current, Order_By_Node_Id());
339 // Retrieve all ways referred by the relations.
34011794 q Ranges< Uint31_Index > way_ranges;
34111794 q get_ranges(rman, way_ranges);
34311794 q Timeless< Uint31_Index, Way_Skeleton > way_members_
34411794 q = relation_way_members(context, into.relations, {}, way_ranges, {}, true);
34511794 q std::vector< std::pair< Uint31_Index, const Way_Skeleton* > > way_members_by_id
34611794 q = order_by_id(way_members_.current, Order_By_Way_Id());
348 // Retrieve all nodes referred by the ways.
34911794 q filter_relations_expensive(*around, node_members_by_id, way_members_by_id,
35011794 q Way_Geometry_Store(way_members_.current, query, rman), into.relations);
35111794 q }
35311794 q if (!into.attic_nodes.empty())
3541818 q filter_nodes_expensive(*around, into.attic_nodes);
35611794 q if (!into.attic_ways.empty())
3571817 q filter_ways_expensive(*around, Way_Geometry_Store(into.attic_ways, query, rman), into.attic_ways);
35911794 q if (!into.attic_relations.empty())
361 //Process relations
36299 q Ranges< Uint32_Index > node_ranges;
36399 q get_ranges(rman, node_ranges);
36599 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > > node_members
36699 q = relation_node_members(context, into.attic_relations, node_ranges);
36799 q std::vector< std::pair< Uint32_Index, const Node_Skeleton* > > node_members_by_id
36899 q = order_attic_by_id(node_members, Order_By_Node_Id());
370 // Retrieve all ways referred by the relations.
37199 q Ranges< Uint31_Index > way_ranges;
37299 q get_ranges(rman, way_ranges);
37499 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > way_members_
37599 q = relation_way_members(context, into.attic_relations, way_ranges);
37699 q std::vector< std::pair< Uint31_Index, const Way_Skeleton* > > way_members_by_id
37799 q = order_attic_by_id(way_members_, Order_By_Way_Id());
37999 q filter_relations_expensive(*around, node_members_by_id, way_members_by_id,
38099 q Way_Geometry_Store(way_members_, query, rman), into.attic_relations);
383 //TODO: areas
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)
39642894 q Token_Node_Ptr tree_it = input_tree;
39742894 q uint line_nr = tree_it->line_col.first;
39842894 q std::vector< std::pair< std::string, std::string > > coords;
40078094 q while (tree_it->token == "," && tree_it->rhs && tree_it->lhs)
40135269 q {
40235269 q std::string lon = tree_it.rhs()->token;
40335269 q tree_it = tree_it.lhs();
40535269 q if (tree_it->token != "," || !tree_it->rhs || !tree_it->lhs)
4060 {
4070 if (error_output)
4080 error_output->add_parse_error("around requires an odd number of arguments", line_nr);
4090 return 0;
4100 }
41235269 q coords.push_back(std::make_pair(tree_it.rhs()->token, lon));
41335269 q tree_it = tree_it.lhs();
41435269 q }
41642894 q if (tree_it->token == ":" && tree_it->rhs)
41742894 q {
41842894 q std::string radius = decode_json(tree_it.rhs()->token, error_output);
42042894 q tree_it = tree_it.lhs();
42142894 q std::string from = "_";
42242894 q if (tree_it->token == "." && tree_it->rhs)
42310426 q from = tree_it.rhs()->token;
42542894 q std::map< std::string, std::string > attributes;
42642894 q attributes["from"] = from;
42742894 q attributes["into"] = into;
42842894 q attributes["radius"] = radius;
42942894 q if (coords.size() == 1)
43029666 q {
43129666 q attributes["lat"] = coords.front().first;
43229666 q attributes["lon"] = coords.front().second;
43329666 q }
43413232 q else if (!coords.empty())
435164 q {
436164 q std::reverse(coords.begin(),coords.end());
437164 q for (std::vector< std::pair< std::string, std::string > >::const_iterator it = coords.begin();
438724 q it != coords.end(); ++it)
439564 q attributes["polyline"] += it->first + "," + it->second + ",";
440164 q attributes["polyline"].resize(attributes["polyline"].size()-1);
441164 q }
44242894 q return new Around_Statement(line_nr, attributes, global_settings);
44342894 q }
4440 else if (error_output)
4450 error_output->add_parse_error("around requires the radius as first argument", line_nr);
4470 return 0;
451Around_Statement::Around_Statement
452 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
45342894 q : Output_Statement(line_number_)
45542894 q std::map< std::string, std::string > attributes;
45742894 q attributes["from"] = "_";
45842894 q attributes["into"] = "_";
45942894 q attributes["radius"] = "";
46042894 q attributes["lat"] = "";
46142894 q attributes["lon"] = "";
46242894 q attributes["polyline"] = "";
46442894 q eval_attributes_array(get_name(), attributes, input_attributes);
46642894 q input = attributes["from"];
46742894 q set_output(attributes["into"]);
46942894 q radius = atof(attributes["radius"].c_str());
47042894 q if ((radius < 0.0) || (attributes["radius"] == ""))
4710 {
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());
4760 }
47842894 q double lat = 100.;
47942894 q double lon = 0;
48042894 q if (attributes["lat"] != "")
48129666 q {
48229666 q lat = atof(attributes["lat"].c_str());
48329666 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.");
48629666 q }
48842894 q if (attributes["lon"] != "")
48929666 q {
49029666 q lon = atof(attributes["lon"].c_str());
49129666 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.");
49429666 q }
49642894 q if (attributes["polyline"] != "")
497164 q {
498164 q if (attributes["lat"] != "" || attributes["lon"] != "")
4990 add_static_error("In \"around\", the attribute \"polyline\" cannot be used if \"lat\" or \"lon\" are used.");
501164 q std::string& polystring = attributes["polyline"];
502164 q std::string::size_type from = 0;
503164 q std::string::size_type to = polystring.find(",");
504724 q while (to != std::string::npos)
505564 q {
506564 q double lat = atof(polystring.substr(from, to).c_str());
507564 q from = to+1;
508564 q to = polystring.find(",", from);
509564 q if (to != std::string::npos)
510404 q {
511404 q points.push_back(Point_Double(lat, atof(polystring.substr(from, to).c_str())));
512404 q from = to+1;
513404 q to = polystring.find(",", from);
514404 q }
515164 q else
516164 q points.push_back(Point_Double(lat, atof(polystring.substr(from).c_str())));
517564 q }
519164 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.");
522164 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.");
525164 q }
52641291 q else if (lat < 100.)
52729666 q points.push_back(Point_Double(lat, lon));
530Around_Statement::~Around_Statement()
53242894 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin();
53367794 q it != constraints.end(); ++it)
53424972 q delete *it;
538std::vector< double > cartesian(double lat, double lon)
539878747784 q{
540878747784 q std::vector< double > result(3);
542878747784 q result[0] = sin(lat/90.0*acos(0));
543878747784 q result[1] = cos(lat/90.0*acos(0))*sin(lon/90.0*acos(0));
544878747784 q result[2] = cos(lat/90.0*acos(0))*cos(lon/90.0*acos(0));
546878747784 q return result;
547878747784 q}
550void rescale(double a, std::vector< double >& v)
551729427115 q{
552729427115 q v[0] *= a;
553729427115 q v[1] *= a;
554729427115 q v[2] *= a;
555729427115 q}
558std::vector< double > sum(const std::vector< double >& v, const std::vector< double >& w)
5591458854225 q{
5601458854225 q std::vector< double > result(3);
5621458854225 q result[0] = v[0] + w[0];
5631458854225 q result[1] = v[1] + w[1];
5641458854225 q result[2] = v[2] + w[2];
5661458854225 q return result;
5671458854225 q}
570double scalar_prod(const std::vector< double >& v, const std::vector< double >& w)
57162471955348 q{
57262471955348 q return v[0]*w[0] + v[1]*w[1] + v[2]*w[2];
57362471955348 q}
576std::vector< double > cross_prod(const std::vector< double >& v, const std::vector< double >& w)
5777474431054 q{
5787474431054 q std::vector< double > result(3);
5807474431054 q result[0] = v[1]*w[2] - v[2]*w[1];
5817474431054 q result[1] = v[2]*w[0] - v[0]*w[2];
5827474431054 q result[2] = v[0]*w[1] - v[1]*w[0];
5847474431054 q return result;
5857474431054 q}
589Prepared_Segment::Prepared_Segment
590 (double first_lat_, double first_lon_, double second_lat_, double second_lon_)
591180159954 q : first_lat(first_lat_), first_lon(first_lon_), second_lat(second_lat_), second_lon(second_lon_)
592180159954 q{
593180159954 q first_cartesian = cartesian(first_lat, first_lon);
594180159954 q second_cartesian = cartesian(second_lat, second_lon);
595180159954 q norm = cross_prod(first_cartesian, second_cartesian);
596180159954 q}
599Prepared_Point::Prepared_Point
600 (double lat_, double lon_)
60133461582 q : lat(lat_), lon(lon_)
60233461582 q{
60333461582 q cartesian = ::cartesian(lat, lon);
60433461582 q}
607double great_circle_line_dist(const Prepared_Segment& segment, const std::vector< double >& cartesian)
60820284761248 q{
60920284761248 q double scalar_prod_ = std::abs(scalar_prod(cartesian, segment.norm))
61020284761248 q /sqrt(scalar_prod(segment.norm, segment.norm));
61220284761248 q if (scalar_prod_ > 1)
6130 scalar_prod_ = 1;
61520284761248 q return asin(scalar_prod_)*(10*1000*1000/acos(0));
61620284761248 q}
619double great_circle_line_dist(double llat1, double llon1, double llat2, double llon2,
620 double plat, double plon)
6210{
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)
6280 scalar_prod_ = 1;
6300 return asin(scalar_prod_)*(10*1000*1000/acos(0));
6310}
634bool intersect(const Prepared_Segment& segment_a,
635 const Prepared_Segment& segment_b)
636729427115 q{
637729427115 q std::vector< double > intersection_pt = cross_prod(segment_a.norm, segment_b.norm);
638729427115 q rescale(1.0/sqrt(scalar_prod(intersection_pt, intersection_pt)), intersection_pt);
640729427115 q std::vector< double > asum = sum(segment_a.first_cartesian, segment_a.second_cartesian);
641729427115 q std::vector< double > bsum = sum(segment_b.first_cartesian, segment_b.second_cartesian);
643729427115 q return (std::abs(scalar_prod(asum, intersection_pt)) >= scalar_prod(asum, segment_a.first_cartesian)
644729427115 q && std::abs(scalar_prod(bsum, intersection_pt)) >= scalar_prod(bsum, segment_b.first_cartesian));
645729427115 q}
648bool intersect(double alat1, double alon1, double alat2, double alon2,
649 double blat1, double blon1, double blat2, double blon2)
6500{
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));
6660}
669Ranges< Uint32_Index > Around_Statement::calc_ranges(const Set& input, Resource_Manager& rman) const
67159694 q if (points.size() == 1)
67243065 q return expand(ranges(points[0].lat, points[0].lon), radius);
67416632 q else if (points.size() > 1)
675204 q {
676204 q std::vector< uint32 > nd_idxs;
677204 q std::map< Uint31_Index, std::vector< Way_Skeleton > > ways;
678204 q std::pair< Uint31_Index, std::vector< Way_Skeleton > > way;
680864 q for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it)
681664 q nd_idxs.push_back(::ll_upper_(it->lat, it->lon));
683204 q Uint31_Index idx = Way::calc_index(nd_idxs);
684204 q way = std::make_pair(idx, std::vector< Way_Skeleton >());
685204 q ways.insert(way);
686204 q return expand(calc_children(ranges(ways)), radius);
687204 q }
68814628 q else
68914628 q return expand(
69014628 q ranges(input.nodes).union_(ranges(input.attic_nodes)).union_(
69114628 q calc_children(
69214628 q ranges(input.ways).union_(ranges(input.attic_ways)).union_(
69314628 q ranges(input.relations).union_(ranges(input.attic_relations))))),
69414628 q radius);
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)
70133461582 q{
70233461582 q double south = lat - radius*(360.0/(40000.0*1000.0));
70333461582 q double north = lat + radius*(360.0/(40000.0*1000.0));
70433461582 q double scale_lat = lat > 0.0 ? north : south;
70533461582 q if (std::abs(scale_lat) >= 89.9)
7060 scale_lat = 89.9;
70733461582 q double west = lon - radius*(360.0/(40000.0*1000.0))/cos(scale_lat/90.0*acos(0));
70833461582 q double east = lon + radius*(360.0/(40000.0*1000.0))/cos(scale_lat/90.0*acos(0));
71033461582 q simple_lat_lons.push_back(Prepared_Point(lat, lon));
71233461582 q auto uint_ranges = calc_ranges(south, north, west, east);
71372211082 q for (auto it = uint_ranges.begin(); it != uint_ranges.end(); ++it)
71438749582 q {
71582169182 q for (uint32 idx = it.lower_bound().val(); idx < it.upper_bound().val(); ++idx)
71643419682 q radius_lat_lons[idx].push_back(Point_Double(lat, lon));
71738749582 q }
71833461582 q}
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)
7243419711 q{
7253419711 q add_coord(::lat(idx.val(), node.ll_lower), ::lon(idx.val(), node.ll_lower),
7263419711 q radius, radius_lat_lons, simple_lat_lons);
7273419711 q}
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)
7341996610 q{
735 // add nodes
73732029010 q for (std::vector< Quad_Coord >::const_iterator nit = way_geometry.begin(); nit != way_geometry.end(); ++nit)
73830032410 q add_coord(::lat(nit->ll_upper, nit->ll_lower),
73930032410 q ::lon(nit->ll_upper, nit->ll_lower),
74030032410 q radius, radius_lat_lons, simple_lat_lons);
742 // add segments
7441996610 q std::vector< Quad_Coord >::const_iterator nit = way_geometry.begin();
7451996610 q if (nit == way_geometry.end())
7460 return;
7481996610 q double first_lat(::lat(nit->ll_upper, nit->ll_lower));
7491996610 q double first_lon(::lon(nit->ll_upper, nit->ll_lower));
75130032410 q for (++nit; nit != way_geometry.end(); ++nit)
75228035810 q {
75328035810 q double second_lat(::lat(nit->ll_upper, nit->ll_lower));
75428035810 q double second_lon(::lon(nit->ll_upper, nit->ll_lower));
75628035810 q simple_segments.push_back(Prepared_Segment(first_lat, first_lon, second_lat, second_lon));
75828035810 q first_lat = second_lat;
75928035810 q first_lon = second_lon;
76028035810 q }
7611996610 q}
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)
768 // add nodes
769184 q for (std::vector< Point_Double >::const_iterator nit = points.begin(); nit != points.end(); ++nit)
770144 q {
771144 q double lat = nit->lat;
772144 q double lon = nit->lon;
773144 q add_coord(lat, lon, radius, radius_lat_lons, simple_lat_lons);
774144 q }
776 // add segments
77844 q std::vector< Point_Double >::const_iterator nit = points.begin();
77944 q if (nit == points.end())
7800 return;
78244 q double first_lat(nit->lat);
78344 q double first_lon(nit->lon);
785144 q for (++nit; nit != points.end(); ++nit)
786104 q {
787104 q double second_lat(nit->lat);
788104 q double second_lon(nit->lon);
790104 q simple_segments.push_back(Prepared_Segment(first_lat, first_lon, second_lat, second_lon));
792104 q first_lat = second_lat;
793104 q first_lon = second_lon;
794104 q }
798template< typename Node_Skeleton >
799void Around_Statement::add_nodes(const std::map< Uint32_Index, std::vector< Node_Skeleton > >& nodes)
8019828 q for (typename std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator iit(nodes.begin());
80296228 q iit != nodes.end(); ++iit)
80386411 q {
80486411 q for (typename std::vector< Node_Skeleton >::const_iterator nit(iit->second.begin());
8053506111 q nit != iit->second.end(); ++nit)
8063419711 q add_node(iit->first, *nit, radius, radius_lat_lons, simple_lat_lons);
80786411 q }
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)
8159828 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator it = ways.begin();
816499928 q it != ways.end(); ++it)
817490111 q {
818490111 q for (typename std::vector< Way_Skeleton >::const_iterator iit = it->second.begin();
8192486711 q iit != it->second.end(); ++iit)
8201996610 q add_way(way_geometries.get_geometry(*iit), radius,
8211996610 q radius_lat_lons, simple_lat_lons, simple_segments);
822490111 q }
826void Around_Statement::calc_lat_lons(const Set& input, Statement& query, Resource_Manager& rman)
82811794 q radius_lat_lons.clear();
82911794 q simple_lat_lons.clear();
83111794 q simple_segments.clear();
83311794 q if (points.size() == 1)
8358065 q add_coord(points[0].lat, points[0].lon, radius, radius_lat_lons, simple_lat_lons);
8368065 q return;
8383732 q else if (points.size() > 1)
84044 q add_way(points, radius, radius_lat_lons, simple_lat_lons, simple_segments);
84144 q return;
8433328 q Request_Context context(&query, rman);
8453328 q add_nodes(input.nodes);
8463328 q add_ways(input.ways, Way_Geometry_Store(input.ways, query, rman));
848 // Retrieve all node and way members referred by the relations.
8493328 q add_nodes(relation_node_members(context, input.relations, {}, Ranges< Uint32_Index >::global(), {}, true).current);
851 // Retrieve all ways referred by the relations.
8523328 q Timeless< Uint31_Index, Way_Skeleton > way_members
8533328 q = relation_way_members(context, input.relations, {}, Ranges< Uint31_Index >::global(), {}, true);
8543328 q add_ways(way_members.current, Way_Geometry_Store(way_members.current, query, rman));
8563328 q if (rman.get_desired_timestamp() != NOW)
8581614 q add_nodes(input.attic_nodes);
8591614 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.
8621614 q add_nodes(relation_node_members(
8631614 q context, input.attic_relations, relation_node_member_indices({}, input.attic_relations)));
865 // Retrieve all ways referred by the relations.
8661614 q std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > > way_members
8671614 q = relation_way_members(context, input.attic_relations, Ranges< Uint31_Index >::global());
8681614 q add_ways(way_members, Way_Geometry_Store(way_members, query, rman));
873bool Around_Statement::is_inside(double lat, double lon) const
874488154173 q{
875488154173 q std::map< Uint32_Index, std::vector< Point_Double > >::const_iterator mit
876488154173 q = radius_lat_lons.find(::ll_upper_(lat, lon));
877488154173 q if (mit != radius_lat_lons.end())
87813135469 q {
87913135469 q for (std::vector< Point_Double >::const_iterator cit = mit->second.begin();
88037243569 q cit != mit->second.end(); ++cit)
88127295869 q {
88227295869 q if ((radius > 0 && great_circle_dist(cit->lat, cit->lon, lat, lon) <= radius)
88327295869 q || (std::abs(cit->lat - lat) < 1e-7 && std::abs(cit->lon - lon) < 1e-7))
8843187765 q return true;
88527295869 q }
88613135469 q }
888484966473 q std::vector< double > coord_cartesian = cartesian(lat, lon);
889484966473 q for (std::vector< Prepared_Segment >::const_iterator
89011084641773 q it = simple_segments.begin(); it != simple_segments.end(); ++it)
89110599679910 q {
89210599679910 q if (great_circle_line_dist(*it, coord_cartesian) <= radius)
89332165425 q {
89432165425 q double gcdist = great_circle_dist
89532165425 q (it->first_lat, it->first_lon, it->second_lat, it->second_lon);
89632165425 q double limit = sqrt(gcdist*gcdist + radius*radius);
89732165425 q if (great_circle_dist(lat, lon, it->first_lat, it->first_lon) <= limit &&
89832165425 q great_circle_dist(lat, lon, it->second_lat, it->second_lon) <= limit)
899463 q return true;
90032165425 q }
90110599679910 q }
903484961873 q return false;
904484966473 q}
906bool Around_Statement::is_inside
907 (double first_lat, double first_lon, double second_lat, double second_lon) const
908152123145 q{
909152123145 q Prepared_Segment segment(first_lat, first_lon, second_lat, second_lon);
911152123145 q for (std::vector< Prepared_Point >::const_iterator cit = simple_lat_lons.begin();
9129837202845 q cit != simple_lat_lons.end(); ++cit)
9139685081343 q {
9149685081343 q if (great_circle_line_dist(segment, cit->cartesian) <= radius)
915326042439 q {
916326042439 q double gcdist = great_circle_dist(first_lat, first_lon, second_lat, second_lon);
917326042439 q double limit = sqrt(gcdist*gcdist + radius*radius);
918326042439 q if (great_circle_dist(cit->lat, cit->lon, first_lat, first_lon) <= limit &&
919326042439 q great_circle_dist(cit->lat, cit->lon, second_lat, second_lon) <= limit)
920168 q return true;
921326042439 q }
9229685081343 q }
924152121545 q for (std::vector< Prepared_Segment >::const_iterator
9257446391445 q cit = simple_segments.begin(); cit != simple_segments.end(); ++cit)
926729427115 q {
927729427115 q if (intersect(*cit, segment))
928122 q return true;
929729427115 q }
931152120345 q return false;
932152121545 q}
935bool Around_Statement::is_inside(const std::vector< Quad_Coord >& way_geometry) const
9364650245 q{
9374650245 q std::vector< Quad_Coord >::const_iterator nit = way_geometry.begin();
9384650245 q if (nit == way_geometry.end())
93941 q return false;
941 // Pre-check if a node is inside
942164279745 q for (std::vector< Quad_Coord >::const_iterator it = way_geometry.begin(); it != way_geometry.end(); ++it)
943160084045 q {
944160084045 q double second_lat(::lat(it->ll_upper, it->ll_lower));
945160084045 q double second_lon(::lon(it->ll_upper, it->ll_lower));
947160084045 q if (is_inside(second_lat, second_lon))
948454139 q return true;
949160084045 q }
9514195745 q double first_lat(::lat(nit->ll_upper, nit->ll_lower));
9524195745 q double first_lon(::lon(nit->ll_upper, nit->ll_lower));
954156316045 q for (++nit; nit != way_geometry.end(); ++nit)
955152123145 q {
956152123145 q double second_lat(::lat(nit->ll_upper, nit->ll_lower));
957152123145 q double second_lon(::lon(nit->ll_upper, nit->ll_lower));
959152123145 q if (is_inside(first_lat, first_lon, second_lat, second_lon))
9602810 q return true;
962152120345 q first_lat = second_lat;
963152120345 q first_lon = second_lon;
964152120345 q }
9654192945 q return false;
9664195745 q}
969void Around_Statement::execute(Resource_Manager& rman)
9712523 q Set into;
9732523 q Around_Constraint constraint(*this);
9742523 q Ranges< Uint32_Index > ranges;
9752523 q constraint.get_ranges(rman, ranges);
9762523 q get_elements_from_db< Uint32_Index, Node_Skeleton >(ranges, *this, rman)
9772523 q .swap(into.nodes, into.attic_nodes);
9782523 q constraint.filter(*this, rman, into);
9792523 q filter_attic_elements(rman, rman.get_desired_timestamp(), into.nodes, into.attic_nodes);
9812523 q transfer_output(rman, into);
9822523 q rman.health_check(*this);
986Query_Constraint* Around_Statement::get_query_constraint()
98824972 q constraints.push_back(new Around_Constraint(*this));
98924972 q return constraints.back();