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

overpass_api/statements/around.cc

602 of 665 instrumented lines run by the suite (90.5%) 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)
37114836 q{
38114836 q Ranges< TIndex > result;
39114836 q if (elems.empty())
4093134 q return 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)
4421265725 q {
4521265725 q if (!(range.second < it->first))
465280425 q range.second = inc(it->first);
4715985319 q else
4815985319 q {
4915985319 q result.push_back(range.first, range.second);
5015985319 q range = std::make_pair(it->first, inc(it->first));
5115985319 q }
5221265725 q }
5321725 q result.push_back(range.first, range.second);
5421725 q result.sort();
5621725 q return result;
57114836 q}
60Ranges< Uint32_Index > ranges(double lat, double lon)
6155380 q{
6255380 q Uint32_Index idx = ::ll_upper_(lat, lon);
6355380 q return { idx, inc(idx) };
6455380 q}
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)
721572105 q {
731572105 q uint32 start = it.lower_bound().val();
743779105 q while (start < it.upper_bound().val())
752207105 q {
762207105 q uint32 end;
772207105 q if ((start & 0x3) != 0 || it.upper_bound().val() < start + 0x4)
78131697 q end = start + 1;
7989117 q else if ((start & 0x3c) != 0 || it.upper_bound().val() < start + 0x40)
8039112 q end = start + 0x4;
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;
912401 q else
922401 q end = start + 0x4000000;
942207105 q result.push_back(std::make_pair(Uint32_Index(start), Uint32_Index(end)));
952207105 q start = end;
962207105 q }
971572105 q }
99751116 q return result;
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)
1092207105 q {
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));
1182207105 q }
120751116 q return result;
123//-----------------------------------------------------------------------------
125class Around_Constraint : public Query_Constraint
127 public:
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() {}
140 private:
141 Around_Statement* around;
142 bool ranges_used;
143};
146bool Around_Constraint::get_ranges
147 (Resource_Manager& rman, Ranges< Uint32_Index >& ranges)
149751116 q ranges_used = true;
151751116 q const Set* input = rman.get_set(around->get_source_name());
152751116 q ranges = around->calc_ranges(input ? *input : Set(), rman);
153751116 q return true;
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);
163394116 q return true;
167void Around_Constraint::filter(Resource_Manager& rman, Set& into)
16912498 q {
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(); )
176262844 q {
177262844 q if (!(nit->first < ranges_it.upper_bound()))
17866543 q ++ranges_it;
179196344 q else if (!(nit->first < ranges_it.lower_bound()))
180196344 q ++nit;
1810 else
1820 {
1830 nit->second.clear();
1840 ++nit;
1850 }
186262844 q }
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(); )
193133420 q {
194133420 q if (!(it->first < ranges_it.upper_bound()))
19536119 q ++ranges_it;
19697320 q else if (!(it->first < ranges_it.lower_bound()))
19797320 q ++it;
1980 else
1990 {
2000 it->second.clear();
2010 ++it;
2020 }
203133420 q }
20412498 q for (; it != into.attic_nodes.end(); ++it)
2050 it->second.clear();
20612498 q }
20812498 q Ranges< Uint31_Index > ranges;
20912498 q get_ranges(rman, ranges);
211 // pre-process ways to reduce the load of the expensive filter
212 // pre-filter ways
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);
22012498 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)
230165116 q for (typename std::map< Uint32_Index, std::vector< Node_Skeleton > >::iterator it = nodes.begin();
2313104116 q it != nodes.end(); ++it)
232293948 q {
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)
236101809547 q {
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);
241101809547 q }
242293948 q it->second.swap(local_into);
243293948 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)
252169116 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::iterator it = ways.begin();
2535512116 q it != ways.end(); ++it)
254534360 q {
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)
25825681758 q {
25925681758 q if (around.is_inside(way_geometries.get_geometry(*iit)))
2604652054 q local_into.push_back(*iit);
26125681758 q }
262534360 q it->second.swap(local_into);
263534360 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)
274155116 q for (typename std::map< Uint31_Index, std::vector< Relation_Skeleton > >::iterator it = relations.begin();
2751686116 q it != relations.end(); ++it)
276153128 q {
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)
2802248827 q {
2812248827 q for (std::vector< Relation_Entry >::const_iterator nit = iit->members.begin();
282850498427 q nit != iit->members.end(); ++nit)
283848311327 q {
284848311327 q if (nit->type == Relation_Entry::NODE)
28527401026 q {
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);
28827401026 q if (!second_nd)
28927227726 q continue;
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))
2941558 q {
2951558 q local_into.push_back(*iit);
2961558 q break;
2971558 q }
298173322 q }
299820910327 q else if (nit->type == Relation_Entry::WAY)
300818185027 q {
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());
303818185027 q if (!second_nd)
304815792127 q continue;
3052392927 q if (around.is_inside(way_geometries.get_geometry(*second_nd->second)))
30646221 q {
30746221 q local_into.push_back(*iit);
30846221 q break;
30946221 q }
3102392927 q }
311848311327 q }
3122248827 q }
313153128 q it->second.swap(local_into);
314153128 q }
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);
328 //Process relations
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())
361 //Process relations
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);
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)
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)
40138883 q {
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)
4060 {
4070 if (error_output)
4080 error_output->add_parse_error("around requires an odd number of arguments", line_nr);
4090 return 0;
4100 }
41238883 q coords.push_back(std::make_pair(tree_it.rhs()->token, lon));
41338883 q tree_it = tree_it.lhs();
41438883 q }
416504117 q if (tree_it->token == ":" && tree_it->rhs)
418504117 q std::string radius = decode_json(tree_it.rhs()->token, error_output);
420504117 q tree_it = tree_it.lhs();
421504117 q std::string from = "_";
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;
426504117 q attributes["from"] = from;
427504117 q attributes["into"] = into;
428504117 q attributes["radius"] = radius;
429504117 q if (coords.size() == 1)
43035681 q {
43135681 q attributes["lat"] = coords.front().first;
43235681 q attributes["lon"] = coords.front().second;
43335681 q }
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);
4470 return 0;
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;
457504117 q attributes["from"] = "_";
458504117 q attributes["into"] = "_";
459504117 q attributes["radius"] = "";
460504117 q attributes["lat"] = "";
461504117 q attributes["lon"] = "";
462504117 q attributes["polyline"] = "";
464504117 q eval_attributes_array(get_name(), attributes, input_attributes);
466504117 q input = attributes["from"];
467504117 q set_output(attributes["into"]);
469504117 q radius = atof(attributes["radius"].c_str());
470504117 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 }
478504117 q double lat = 100.;
479504117 q double lon = 0;
480504117 q if (attributes["lat"] != "")
48135681 q {
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.");
48635681 q }
488504117 q if (attributes["lon"] != "")
48935681 q {
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.");
49435681 q }
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)
505322 q {
506322 q double lat = atof(polystring.substr(from, to).c_str());
507322 q from = to+1;
508322 q to = polystring.find(",", from);
509322 q if (to != std::string::npos)
510242 q {
511242 q points.push_back(Point_Double(lat, atof(polystring.substr(from, to).c_str())));
512242 q from = to+1;
513242 q to = polystring.find(",", from);
514242 q }
51582 q else
51682 q points.push_back(Point_Double(lat, atof(polystring.substr(from).c_str())));
517322 q }
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.");
526496115 q else if (lat < 100.)
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)
53432198 q delete *it;
538std::vector< double > cartesian(double lat, double lon)
53910237309104 q{
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));
54610237309104 q return result;
54710237309104 q}
550void rescale(double a, std::vector< double >& v)
5511061185277 q{
5521061185277 q v[0] *= a;
5531061185277 q v[1] *= a;
5541061185277 q v[2] *= a;
5551061185277 q}
558std::vector< double > sum(const std::vector< double >& v, const std::vector< double >& w)
5592122370547 q{
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;
5672122370547 q}
570double scalar_prod(const std::vector< double >& v, const std::vector< double >& w)
57183237000571 q{
57283237000571 q return v[0]*w[0] + v[1]*w[1] + v[2]*w[2];
57383237000571 q}
576std::vector< double > cross_prod(const std::vector< double >& v, const std::vector< double >& w)
57710915856779 q{
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];
58410915856779 q return result;
58510915856779 q}
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_)
592304004079 q{
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);
596304004079 q}
599Prepared_Point::Prepared_Point
600 (double lat_, double lon_)
601272215103 q : lat(lat_), lon(lon_)
602272215103 q{
603272215103 q cartesian = ::cartesian(lat, lon);
604272215103 q}
607double great_circle_line_dist(const Prepared_Segment& segment, const std::vector< double >& cartesian)
60825691403971 q{
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)
6130 scalar_prod_ = 1;
61525691403971 q return asin(scalar_prod_)*(10*1000*1000/acos(0));
61625691403971 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)
6361061185277 q{
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));
6451061185277 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
671751116 q if (points.size() == 1)
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 >());
68582 q ways.insert(way);
68682 q return expand(calc_children(ranges(ways)), radius);
68819034 q else
68919034 q return expand(
69019034 q ranges(input.nodes).union_(ranges(input.attic_nodes)).union_(
69119034 q calc_children(
69219034 q ranges(input.ways).union_(ranges(input.attic_ways)).union_(
69319034 q ranges(input.relations).union_(ranges(input.attic_relations))))),
69419034 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)
701272215103 q{
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)
7060 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)
714362944103 q {
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));
717362944103 q }
718272215103 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)
7242814311 q{
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);
7272814311 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)
7343034316 q{
735 // add nodes
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);
742 // add segments
7443034316 q std::vector< Quad_Coord >::const_iterator nit = way_geometry.begin();
7453034316 q if (nit == way_geometry.end())
7460 return;
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)
75221362116 q {
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;
76021362116 q }
7613034316 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
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);
776 // add segments
77822 q std::vector< Point_Double >::const_iterator nit = points.begin();
77922 q if (nit == points.end())
7800 return;
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)
80391711 q {
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);
80791711 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)
81512634 q for (typename std::map< Uint31_Index, std::vector< Way_Skeleton > >::const_iterator it = ways.begin();
81692734 q it != ways.end(); ++it)
81780117 q {
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);
82280117 q }
826void Around_Statement::calc_lat_lons(const Set& input, Statement& query, Resource_Manager& rman)
828143116 q radius_lat_lons.clear();
829143116 q simple_lat_lons.clear();
831143116 q simple_segments.clear();
833143116 q if (points.size() == 1)
83410080 q {
83510080 q add_coord(points[0].lat, points[0].lon, radius, radius_lat_lons, simple_lat_lons);
83610080 q return;
83710080 q }
8384336 q else if (points.size() > 1)
84022 q add_way(points, radius, radius_lat_lons, simple_lat_lons, simple_segments);
84122 q return;
8434134 q Request_Context context(&query, rman);
8454134 q add_nodes(input.nodes);
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
874416864591 q{
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())
87899456489 q {
87999456489 q for (std::vector< Point_Double >::const_iterator cit = mit->second.begin();
880327531789 q cit != mit->second.end(); ++cit)
881256438489 q {
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))
88428363186 q return true;
885256438489 q }
88699456489 q }
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)
89112705667011 q {
89212705667011 q if (great_circle_line_dist(*it, coord_cartesian) <= radius)
89324460207 q {
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)
899684 q return true;
90024460207 q }
90112705667011 q }
903388494691 q return false;
904388501491 q}
906bool Around_Statement::is_inside
907 (double first_lat, double first_lon, double second_lat, double second_lon) const
908282641368 q{
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)
91312985736967 q {
91412985736967 q if (great_circle_line_dist(segment, cit->cartesian) <= radius)
915313723165 q {
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)
9203311 q return true;
921313723165 q }
92212985736967 q }
924282638068 q for (std::vector< Prepared_Segment >::const_iterator
92510894490368 q cit = simple_segments.begin(); cit != simple_segments.end(); ++cit)
9261061185277 q {
9271061185277 q if (intersect(*cit, segment))
92841 q return true;
9291061185277 q }
931282637668 q return false;
932282638068 q}
935bool Around_Statement::is_inside(const std::vector< Quad_Coord >& way_geometry) const
93628074668 q{
93728074668 q std::vector< Quad_Coord >::const_iterator nit = way_geometry.begin();
93828074668 q if (nit == way_geometry.end())
9391414 q return false;
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)
943314881768 q {
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))
9484694564 q return true;
949314881768 q }
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)
955282641368 q {
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))
9603712 q return true;
962282637668 q first_lat = second_lat;
963282637668 q first_lon = second_lon;
964282637668 q }
96523362368 q return false;
96623366068 q}
969void Around_Statement::execute(Resource_Manager& rman)
9711918 q Set into;
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();