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

overpass_api/statements/localize.cc

519 of 587 instrumented lines run by the suite (88.4%) 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 "../data/bbox_filter.h"
20#include "../data/tag_store.h"
21#include "../data/utils.h"
22#include "localize.h"
23#include "set_prop.h"
26Generic_Statement_Maker< Localize_Statement > Localize_Statement::statement_maker("localize");
29Localize_Statement::Localize_Statement
30 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
316019 q : Output_Statement(line_number_), type(data)
336019 q std::map< std::string, std::string > attributes;
356019 q attributes["from"] = "_";
366019 q attributes["into"] = "_";
376019 q attributes["type"] = "l";
386019 q attributes["s"] = "";
396019 q attributes["n"] = "";
406019 q attributes["w"] = "";
416019 q attributes["e"] = "";
436019 q eval_attributes_array(get_name(), attributes, input_attributes);
456019 q set_output(attributes["into"]);
466019 q input = attributes["from"];
486019 q if (attributes["type"] == "ll")
493310 q type = also_loose;
50279 q else if (attributes["type"] == "llb")
51124 q type = all;
52155 q else if (attributes["type"] != "l")
530 add_static_error("Localize must have one of the values \"l\", \"ll\", or \"llb\". \"l\" is the default value.");
556019 q south = 100.;
566019 q if (!attributes["s"].empty() || !attributes["n"].empty()
576019 q || !attributes["w"].empty() || !attributes["e"].empty())
580 {
590 south = atof(attributes["s"].c_str());
600 if ((south < -90.0) || (south > 90.0) || (attributes["s"] == ""))
610 add_static_error("For the attribute \"s\" of the element \"bbox-query\""
620 " the only allowed values are floats between -90.0 and 90.0.");
640 north = atof(attributes["n"].c_str());
650 if ((north < -90.0) || (north > 90.0) || (attributes["n"] == ""))
660 add_static_error("For the attribute \"n\" of the element \"bbox-query\""
670 " the only allowed values are floats between -90.0 and 90.0.");
680 if (north < south)
690 add_static_error("The value of attribute \"n\" of the element \"bbox-query\""
700 " must always be greater or equal than the value of attribute \"s\".");
720 west = atof(attributes["w"].c_str());
730 if ((west < -180.0) || (west > 180.0) || (attributes["w"] == ""))
740 add_static_error("For the attribute \"w\" of the element \"bbox-query\""
750 " the only allowed values are floats between -180.0 and 180.0.");
770 east = atof(attributes["e"].c_str());
780 if ((east < -180.0) || (east > 180.0) || (attributes["e"] == ""))
790 add_static_error("For the attribute \"e\" of the element \"bbox-query\""
800 " the only allowed values are floats between -180.0 and 180.0.");
810 }
85class NWR_Context
87public:
88 struct Node_Context
89 {
90104337616 q Node_Context() : local_id(0ull), count(0), lat(100.), lon(0.) {}
91 Derived_Skeleton::Id_Type local_id;
92 uint count;
93 double lat;
94 double lon;
95 std::map< Node_Skeleton::Id_Type, std::vector< Way_Skeleton::Id_Type > > link_by_origin;
96 };
98 struct Way_Section_Context
99 {
10014176514 q Way_Section_Context(uint pos_) : pos(pos_), local_id(0ull), local_from(0ull), local_to(0ull) {}
101 uint pos;
102 Derived_Skeleton::Id_Type local_id;
103 Derived_Skeleton::Id_Type local_from;
104 Derived_Skeleton::Id_Type local_to;
105 std::vector< Point_Double > points;
106 };
108 NWR_Context(Resource_Manager& rman, const Statement& stmt, const Set& input_set,
109 const Owner< Bbox_Double >& bbox);
110 void prepare_links(Resource_Manager& rman, const Statement& stmt, const Set& input_set,
111 const Owner< Bbox_Double >& bbox);
112 Derived_Skeleton::Id_Type local_id_by_node_id(Node_Skeleton::Id_Type id) const;
1132435316 q Node_Context& context_by_node_id(Node_Skeleton::Id_Type id) { return context_by_node_id_[id]; }
11419474114 q std::vector< Way_Section_Context >& context_by_way_id(Way_Skeleton::Id_Type id) { return context_by_way_id_[id]; }
116private:
117 Set relevant_nwrs;
118 std::map< Node_Skeleton::Id_Type, Node_Context > context_by_node_id_;
119 std::map< Way_Skeleton::Id_Type, std::vector< Way_Section_Context > > context_by_way_id_;
120};
123struct Node_Skeleton_By_Coord_Then_Id
125 bool operator()(const Node_Skeleton* lhs, const Node_Skeleton* rhs)
126335325816 q {
127335325816 q if (lhs->ll_lower != rhs->ll_lower)
128335316216 q return lhs->ll_lower < rhs->ll_lower;
129964 q return lhs->id < rhs->id;
130335325816 q }
131};
134bool admissible_by_bbox(const Owner< Bbox_Double >& bbox, const NWR_Context::Node_Context& node_context)
1352435316 q{
1362435316 q return (!bbox || bbox->contains(Point_Double(node_context.lat, node_context.lon)));
1372435316 q}
140bool admissible_by_bbox(const Owner< Bbox_Double >& bbox, const std::vector< Point_Double >& points)
14114176514 q{
14214176514 q if (!bbox)
14314176514 q return true;
1450 for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it)
1460 {
1470 if (bbox->contains(*it))
1480 return true;
1490 }
1500 for (uint j = 1; j < points.size(); ++j)
1510 {
1520 if (bbox->intersects(points[j-1], points[j]))
1530 return true;
1540 }
1550 return false;
1560}
159template< typename Index, typename Object, typename Task >
160void for_each_elem(const std::map< Index, std::vector< Object > >& container, Task task)
16216819 q for (typename std::map< Index, std::vector< Object > >::const_iterator
1636916119 q it_idx = container.begin(); it_idx != container.end(); ++it_idx)
1646899316 q {
1656899316 q for (typename std::vector< Object >::const_iterator it_elem = it_idx->second.begin();
16687990516 q it_elem != it_idx->second.end(); ++it_elem)
16781091216 q task(it_idx->first, *it_elem);
1686899316 q }
172struct Set_Coord_In_Context
174 void operator()(Uint32_Index idx, const Node_Skeleton& node)
17545199216 q {
17645199216 q NWR_Context::Node_Context& context = (*context_by_node_id)[node.id];
17745199216 q context.lat = ::lat(idx.val(), node.ll_lower);
17845199216 q context.lon = ::lon(idx.val(), node.ll_lower);
17945199216 q }
181 Set_Coord_In_Context(std::map< Node_Skeleton::Id_Type, NWR_Context::Node_Context >& context_by_node_id_)
1824219 q : context_by_node_id(&context_by_node_id_) {}
183private:
184 std::map< Node_Skeleton::Id_Type, NWR_Context::Node_Context >* context_by_node_id;
185};
188struct Count_Node_Use
190 void operator()(Uint31_Index idx, const Way_Skeleton& way)
19111964014 q {
19211964014 q if (!way.nds.empty())
19311964014 q {
19411964014 q (*context_by_node_id)[way.nds.front()].count += 2;
195110223914 q for (uint i = 1; i < way.nds.size()-1; ++i)
19698259914 q ++(*context_by_node_id)[way.nds[i]].count;
19711964014 q (*context_by_node_id)[way.nds.back()].count += 2;
19811964014 q }
19911964014 q }
201 Count_Node_Use(std::map< Node_Skeleton::Id_Type, NWR_Context::Node_Context >& context_by_node_id_)
2024219 q : context_by_node_id(&context_by_node_id_) {}
203private:
204 std::map< Node_Skeleton::Id_Type, NWR_Context::Node_Context >* context_by_node_id;
205};
208struct Partition_Into_Links
210 void operator()(Uint31_Index idx, const Way_Skeleton& way)
21111964014 q {
21211964014 q if (!way.nds.empty())
21311964014 q {
21411964014 q std::vector< NWR_Context::Way_Section_Context >& way_context = (*context_by_way_id)[way.id];
21611964014 q std::vector< NWR_Context::Node_Context* > nds_contexts;
21711964014 q nds_contexts.reserve(way.nds.size());
21864827814 q for (std::vector< Node::Id_Type >::const_iterator it_nds = way.nds.begin(); it_nds != way.nds.end(); ++it_nds)
21958552714 q {
22058552714 q std::map< Node_Skeleton::Id_Type, NWR_Context::Node_Context >::iterator it_ctx
22158552714 q = context_by_node_id->find(*it_nds);
22258552714 q if (it_ctx == context_by_node_id->end() || it_ctx->second.lat == 100.)
2235688914 q return;
22452863814 q nds_contexts.push_back(&it_ctx->second);
22552863814 q }
2276275114 q uint start = 0;
2286275114 q const NWR_Context::Node_Context& start_context = *nds_contexts[start];
2296275114 q way_context.push_back(NWR_Context::Way_Section_Context(start));
2306275114 q way_context.back().local_from = start_context.local_id;
2316275114 q way_context.back().points.push_back(Point_Double(start_context.lat, start_context.lon));
23343597914 q for (uint i = 1; i < way.nds.size()-1; ++i)
23437322814 q {
23537322814 q NWR_Context::Node_Context& context = *nds_contexts[i];
23637322814 q way_context.back().points.push_back(Point_Double(context.lat, context.lon));
23837322814 q if (context.local_id.val() || context.count > 1)
2397901414 q {
2407901414 q context.link_by_origin[way.nds[start]].push_back(way.id);
2417901414 q start = i;
2427901414 q way_context.back().local_to = context.local_id;
2437901414 q if (!admissible_by_bbox(*bbox, way_context.back().points))
2440 way_context.back().points.clear();
2457901414 q way_context.push_back(NWR_Context::Way_Section_Context(start));
2467901414 q way_context.back().local_from = context.local_id;
2477901414 q way_context.back().points.push_back(Point_Double(context.lat, context.lon));
2487901414 q }
24937322814 q }
2516275114 q NWR_Context::Node_Context& context = *nds_contexts.back();
2526275114 q way_context.back().points.push_back(Point_Double(context.lat, context.lon));
2536275114 q way_context.back().local_to = context.local_id;
2546275114 q context.link_by_origin[way.nds[start]].push_back(way.id);
2556275114 q if (!admissible_by_bbox(*bbox, way_context.back().points))
2560 way_context.back().points.clear();
2576275114 q }
25811964014 q }
260 Partition_Into_Links(
261 std::map< Node_Skeleton::Id_Type, NWR_Context::Node_Context >& context_by_node_id_,
262 std::map< Way_Skeleton::Id_Type, std::vector< NWR_Context::Way_Section_Context > >& context_by_way_id_,
263 const Owner< Bbox_Double >& bbox_)
2644219 q : context_by_node_id(&context_by_node_id_), context_by_way_id(&context_by_way_id_), bbox(&bbox_) {}
265private:
266 std::map< Node_Skeleton::Id_Type, NWR_Context::Node_Context >* context_by_node_id;
267 std::map< Way_Skeleton::Id_Type, std::vector< NWR_Context::Way_Section_Context > >* context_by_way_id;
268 const Owner< Bbox_Double >* bbox;
269};
272struct Figure_Out_Local_Ids_Of_Links
274 void operator()(Uint31_Index idx, const Way_Skeleton& way)
27511964014 q {
27611964014 q if (!way.nds.empty())
27711964014 q {
27811964014 q std::vector< NWR_Context::Way_Section_Context >& way_context = (*context_by_way_id)[way.id];
27911964014 q std::vector< std::pair< Node_Skeleton::Id_Type, Node_Skeleton::Id_Type > > segment_count;
28126140514 q for (uint i = 0; i < way_context.size(); ++i)
28214176514 q {
28314176514 q NWR_Context::Node_Context& context = (*context_by_node_id)[
28414176514 q i+1 < way_context.size() ? way.nds[way_context[i+1].pos] : way.nds.back()];
28514176514 q const std::vector< Way_Skeleton::Id_Type >& parallel_links
28614176514 q = context.link_by_origin.find(way.nds[way_context[i].pos])->second;
28714176514 q if (parallel_links.size() > 1)
2881037811 q {
2891037811 q uint parallel_links_pos = 0;
2901558711 q while (parallel_links_pos < parallel_links.size()
2911558711 q && !(parallel_links[parallel_links_pos] == way.id))
292520911 q ++parallel_links_pos;
2931037811 q if (parallel_links_pos+1 == parallel_links.size() || !(parallel_links[parallel_links_pos+1] == way.id))
2941037611 q way_context[i].local_id = parallel_links_pos + 1;
29521 q else
29721 q Node_Skeleton::Id_Type target_id =
29821 q (i+1 < way_context.size() ? way.nds[way_context[i+1].pos] : way.nds.back());
29921 q for (std::vector< std::pair< Node_Skeleton::Id_Type, Node_Skeleton::Id_Type > >::const_iterator
30031 q it_pair = segment_count.begin(); it_pair != segment_count.end(); ++it_pair)
30211 q if (it_pair->first == way.nds[way_context[i].pos] && it_pair->second == target_id)
30311 q ++parallel_links_pos;
30521 q way_context[i].local_id = parallel_links_pos + 1;
30621 q segment_count.push_back(std::make_pair(way.nds[way_context[i].pos], target_id));
3081037811 q }
30914176514 q }
31011964014 q }
31111964014 q }
313 Figure_Out_Local_Ids_Of_Links(
314 std::map< Node_Skeleton::Id_Type, NWR_Context::Node_Context >& context_by_node_id_,
315 std::map< Way_Skeleton::Id_Type, std::vector< NWR_Context::Way_Section_Context > >& context_by_way_id_)
3164219 q : context_by_node_id(&context_by_node_id_), context_by_way_id(&context_by_way_id_) {}
317private:
318 std::map< Node_Skeleton::Id_Type, NWR_Context::Node_Context >* context_by_node_id;
319 std::map< Way_Skeleton::Id_Type, std::vector< NWR_Context::Way_Section_Context > >* context_by_way_id;
320};
323NWR_Context::NWR_Context(Resource_Manager& rman, const Statement& stmt, const Set& input_set,
324 const Owner< Bbox_Double >& bbox)
326 // Collect all objects required for context
3282119 q Request_Context context(&stmt, rman);
3292119 q if (bbox)
3300 {
3310 Bbox_Filter filter(*bbox);
3320 add_nw_member_objects(context, input_set, relevant_nwrs, &filter.get_ranges_32(), &filter.get_ranges_31());
3330 }
3342119 q else
3352119 q add_nw_member_objects(context, input_set, relevant_nwrs);
3372119 q Set extra_ways;
3382119 q if (rman.get_desired_timestamp() == NOW)
3401414 q collect_ways(context, relevant_nwrs.nodes, {}, 0, {}, true).swap(extra_ways.ways, extra_ways.attic_ways);
3411414 q sort_second(extra_ways.ways);
3421414 q indexed_set_union(relevant_nwrs.ways, extra_ways.ways);
34476 q else
34676 q collect_ways(context, relevant_nwrs.nodes, relevant_nwrs.attic_nodes, 0,
34776 q {}, true).swap(extra_ways.ways, extra_ways.attic_ways);
34876 q indexed_set_union(relevant_nwrs.attic_ways, extra_ways.attic_ways);
34976 q indexed_set_union(relevant_nwrs.ways, extra_ways.ways);
35076 q keep_matching_skeletons(relevant_nwrs.ways, relevant_nwrs.attic_ways, rman.get_desired_timestamp());
353 // Build local ids for node segments
3552119 q indexed_set_union(relevant_nwrs.nodes, input_set.nodes);
3562119 q indexed_set_union(relevant_nwrs.attic_nodes, input_set.attic_nodes);
3572119 q keep_matching_skeletons(relevant_nwrs.nodes, relevant_nwrs.attic_nodes, rman.get_desired_timestamp());
3592119 q std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator it_now_idx
3602119 q = relevant_nwrs.nodes.begin();
3612119 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::const_iterator it_attic_idx
3622119 q = relevant_nwrs.attic_nodes.begin();
3641634919 q while (it_now_idx != relevant_nwrs.nodes.end() || it_attic_idx != relevant_nwrs.attic_nodes.end())
3651632816 q {
3661632816 q std::vector< const Node_Skeleton* > elem_refs;
3681632816 q bool now_relevant = false;
3691632816 q bool attic_relevant = false;
3701632816 q if (it_attic_idx == relevant_nwrs.attic_nodes.end())
3711369713 q now_relevant = true;
37226314 q else if (it_now_idx == relevant_nwrs.nodes.end())
3730 attic_relevant = true;
37426314 q else if (it_now_idx->first < it_attic_idx->first)
37517292 q now_relevant = true;
3769024 q else if (it_attic_idx->first < it_now_idx->first)
377652 q attic_relevant = true;
3788374 q else
3798374 q {
3808374 q now_relevant = true;
3818374 q attic_relevant = true;
3828374 q }
3841632816 q if (now_relevant)
3851626316 q {
3861626316 q for (std::vector< Node_Skeleton >::const_iterator it_elem = it_now_idx->second.begin();
38746388616 q it_elem != it_now_idx->second.end(); ++it_elem)
38844762316 q elem_refs.push_back(&*it_elem);
3901626316 q ++it_now_idx;
3911626316 q }
3931632816 q if (attic_relevant)
3949024 q {
3959024 q for (std::vector< Attic< Node_Skeleton > >::const_iterator it_elem = it_attic_idx->second.begin();
39652714 q it_elem != it_attic_idx->second.end(); ++it_elem)
39743694 q elem_refs.push_back(&*it_elem);
3999024 q ++it_attic_idx;
4009024 q }
4021632816 q std::sort(elem_refs.begin(), elem_refs.end(), Node_Skeleton_By_Coord_Then_Id());
4041632816 q uint equal_since = 0;
40545210416 q for (uint i = 1; i < elem_refs.size(); ++i)
40643577616 q {
40743577616 q if (elem_refs[equal_since]->ll_lower == elem_refs[i]->ll_lower)
408764 q {
409764 q if (i - equal_since == 1)
410684 q context_by_node_id_[elem_refs[equal_since]->id].local_id = 1;
411764 q context_by_node_id_[elem_refs[i]->id].local_id = i + 1 - equal_since;
412764 q }
41343570016 q else
41443570016 q equal_since = i;
41543577616 q }
4161632816 q }
418 // Store node coordinates for use by links
4192119 q for_each_elem(relevant_nwrs.nodes, Set_Coord_In_Context(context_by_node_id_));
4202119 q for_each_elem(relevant_nwrs.attic_nodes, Set_Coord_In_Context(context_by_node_id_));
422 // Count node use by ways
4232119 q for_each_elem(relevant_nwrs.ways, Count_Node_Use(context_by_node_id_));
4242119 q for_each_elem(relevant_nwrs.attic_ways, Count_Node_Use(context_by_node_id_));
428void NWR_Context::prepare_links(Resource_Manager& rman, const Statement& stmt, const Set& input_set,
429 const Owner< Bbox_Double >& bbox)
431 // Figure out links
4322119 q for_each_elem(relevant_nwrs.ways, Partition_Into_Links(context_by_node_id_, context_by_way_id_, bbox));
4332119 q for_each_elem(relevant_nwrs.attic_ways, Partition_Into_Links(context_by_node_id_, context_by_way_id_, bbox));
4352119 q for (std::map< Node_Skeleton::Id_Type, Node_Context >::iterator it_ctx = context_by_node_id_.begin();
436104310019 q it_ctx != context_by_node_id_.end(); ++it_ctx)
437104307916 q {
438104307916 q for (std::map< Node_Skeleton::Id_Type, std::vector< Way_Skeleton::Id_Type > >::iterator
439117964816 q it_orig = it_ctx->second.link_by_origin.begin(); it_orig != it_ctx->second.link_by_origin.end(); ++it_orig)
44013656914 q std::sort(it_orig->second.begin(), it_orig->second.end());
441104307916 q }
443 // Figure out local ids of links
4442119 q for_each_elem(relevant_nwrs.ways, Figure_Out_Local_Ids_Of_Links(context_by_node_id_, context_by_way_id_));
4452119 q for_each_elem(relevant_nwrs.attic_ways, Figure_Out_Local_Ids_Of_Links(context_by_node_id_, context_by_way_id_));
449Derived_Skeleton::Id_Type NWR_Context::local_id_by_node_id(Node_Skeleton::Id_Type id) const
4501684610 q{
4511684610 q std::map< Node_Skeleton::Id_Type, Node_Context >::const_iterator it
4521684610 q = context_by_node_id_.find(id);
4531684610 q if (it != context_by_node_id_.end())
4541684610 q return it->second.local_id;
4550 return Derived_Skeleton::Id_Type(0ull);
4561684610 q}
459struct Derived_Structure_Builder
461 Derived_Structure_Builder(Resource_Manager& rman,
462 const std::string& name, Opaque_Geometry* geometry,
463 const std::vector< std::pair< std::string, std::string > >* tags)
46425584116 q : target(name, 0ull)
46525584116 q {
46625584116 q target.id = rman.get_global_settings().dispense_derived_id();
46725584116 q target.acquire_geometry(geometry);
46925584116 q if (tags)
47024170816 q {
47124170816 q for (std::vector< std::pair< std::string, std::string > >::const_iterator it_tag = tags->begin();
472281309816 q it_tag != tags->end(); ++it_tag)
473257139016 q target.tags.push_back(std::make_pair(
474257139016 q it_tag->first.empty() ? "" : it_tag->first[0] == '_' ? "_" + it_tag->first : it_tag->first,
475257139016 q it_tag->second));
47624170816 q }
47725584116 q }
479 void set_tag(const std::string& key, const std::string& value)
48021526814 q { target.tags.push_back(std::make_pair(key, value)); }
482 const Derived_Structure& get()
48343987416 q {
48443987416 q std::sort(target.tags.begin(), target.tags.end());
48543987416 q return target;
48643987416 q }
488private:
489 Derived_Structure target;
490};
493template< typename Index, typename Maybe_Attic >
494void process_vertices(const std::map< Index, std::vector< Maybe_Attic > >& items, Set_With_Context& context_from,
495 Set& into, Resource_Manager& rman, NWR_Context& nwr_context,
496 Localize_Statement::Mode mode, const Owner< Bbox_Double >& bbox)
4982519 q for (typename std::map< Index, std::vector< Maybe_Attic > >::const_iterator it_idx = items.begin();
49977419 q it_idx != items.end(); ++it_idx)
50074910 q {
50174910 q for (typename std::vector< Maybe_Attic >::const_iterator it_elem = it_idx->second.begin();
5028369710 q it_elem != it_idx->second.end(); ++it_elem)
5038294810 q {
5048294810 q const Element_With_Context< Maybe_Attic >& data = context_from.get_context(it_idx->first, *it_elem);
5068294810 q if (mode == Localize_Statement::all || (data.tags && !data.tags->empty()))
5071684610 q {
5081684610 q NWR_Context::Node_Context& node_context = nwr_context.context_by_node_id(it_elem->id);
5091684610 q if (data.tags && !data.tags->empty())
51033449 q ++node_context.count;
5121684610 q if (admissible_by_bbox(bbox, node_context))
5131684610 q {
5141684610 q Derived_Structure_Builder result(rman, "vertex",
5151684610 q new Point_Geometry(
5161684610 q ::lat(it_idx->first.val(), it_elem->ll_lower), ::lon(it_idx->first.val(), it_elem->ll_lower)),
5171684610 q data.tags);
5191684610 q Derived_Skeleton::Id_Type local_id = nwr_context.local_id_by_node_id(it_elem->id);
5201684610 q if (local_id.val())
521622 q result.set_tag("_local_id", to_string(local_id.val()));
5231684610 q if (mode == Localize_Statement::all)
524139614 q result.set_tag("_node_ref", to_string(it_elem->id.val()));
5261684610 q into.deriveds[Uint31_Index(0u)].push_back(result.get());
5271684610 q }
5281684610 q }
5298294810 q }
53074910 q }
534template< typename Index, typename Maybe_Attic >
535void generate_links(const std::map< Index, std::vector< Maybe_Attic > >& items, Set_With_Context& context_from,
536 Set& into, Resource_Manager& rman, NWR_Context& nwr_context,
537 Localize_Statement::Mode mode, const Owner< Bbox_Double >& bbox)
5392619 q for (typename std::map< Index, std::vector< Maybe_Attic > >::const_iterator it_idx = items.begin();
54077019 q it_idx != items.end(); ++it_idx)
54174411 q {
54274411 q for (typename std::vector< Maybe_Attic >::const_iterator it_elem = it_idx->second.begin();
5432079511 q it_elem != it_idx->second.end(); ++it_elem)
5442005111 q {
5452005111 q const Element_With_Context< Maybe_Attic >& data = context_from.get_context(it_idx->first, *it_elem);
5472005111 q if (mode == Localize_Statement::all || (data.tags && !data.tags->empty()))
5481995411 q {
5491995411 q std::vector< NWR_Context::Way_Section_Context >& way_context = nwr_context.context_by_way_id(it_elem->id);
5511995411 q for (std::vector< NWR_Context::Way_Section_Context >::const_iterator it_way_ctx = way_context.begin();
5527351711 q it_way_ctx != way_context.end(); ++it_way_ctx)
5535356311 q {
5545356311 q if (it_way_ctx->points.empty())
5550 continue;
5575356311 q Derived_Structure_Builder result(rman, "link",
5585356311 q new Linestring_Geometry(it_way_ctx->points), data.tags);
5605356311 q if (it_way_ctx->local_from.val())
5611072 q result.set_tag("_local_from", to_string(it_way_ctx->local_from.val()));
5625356311 q if (it_way_ctx->local_to.val())
5631072 q result.set_tag("_local_to", to_string(it_way_ctx->local_to.val()));
5645356311 q if (it_way_ctx->local_id.val())
56570467 q result.set_tag("_local_id", to_string(it_way_ctx->local_id.val()));
5675356311 q if (mode == Localize_Statement::all)
56872694 q result.set_tag("_way_ref", to_string(it_elem->id.val()));
5705356311 q into.deriveds[Uint31_Index(0u)].push_back(result.get());
5715356311 q }
5721995411 q }
5732005111 q }
57474411 q }
578template< typename Index, typename Maybe_Attic >
579void generate_loose_links(const std::map< Index, std::vector< Maybe_Attic > >& items,
580 Set_With_Context& context_from, Set& into, Resource_Manager& rman, Localize_Statement::Mode mode)
5822014 q for (typename std::map< Index, std::vector< Maybe_Attic > >::const_iterator it_idx = items.begin();
58367814 q it_idx != items.end(); ++it_idx)
5846589 q {
5856589 q for (typename std::vector< Maybe_Attic >::const_iterator it_elem = it_idx->second.begin();
58696439 q it_elem != it_idx->second.end(); ++it_elem)
58789859 q {
58889859 q const Element_With_Context< Maybe_Attic >& data = context_from.get_context(it_idx->first, *it_elem);
59089859 q if ((mode == Localize_Statement::all || (data.tags && !data.tags->empty()))
59189859 q && it_elem->nds.empty())
5920 {
5930 Derived_Structure_Builder result(rman, "link", new Null_Geometry(), data.tags);
5950 if (mode == Localize_Statement::all)
5960 result.set_tag("_way_ref", to_string(it_elem->id.val()));
5980 into.deriveds[Uint31_Index(0u)].push_back(result.get());
5990 }
60089859 q }
6016589 q }
605template< typename Index, typename Maybe_Attic >
606void generate_trigraphs(const std::map< Index, std::vector< Maybe_Attic > >& items, Set_With_Context& context_from,
607 Set& into, Resource_Manager& rman, NWR_Context& nwr_context,
608 Localize_Statement::Mode mode, const Owner< Bbox_Double >& bbox)
6102619 q for (typename std::map< Index, std::vector< Maybe_Attic > >::const_iterator it_idx = items.begin();
61117319 q it_idx != items.end(); ++it_idx)
6121478 q {
6131478 q for (typename std::vector< Maybe_Attic >::const_iterator it_elem = it_idx->second.begin();
6149998 q it_elem != it_idx->second.end(); ++it_elem)
6158528 q {
6168528 q const Element_With_Context< Maybe_Attic >& data = context_from.get_context(it_idx->first, *it_elem);
6188528 q if (mode == Localize_Statement::all || (data.tags && !data.tags->empty()))
6198528 q {
6208528 q Derived_Structure* last = 0;
6218528 q Point_Double last_point(100., 0.);
6238528 q for (std::vector< Relation_Entry >::const_iterator it_memb = data.object->members.begin();
6241817338 q it_memb != data.object->members.end(); ++it_memb)
6251808818 q {
6261808818 q if (it_memb->type == Relation_Entry::NODE)
62775078 q {
62875078 q NWR_Context::Node_Context& node_context = nwr_context.context_by_node_id(it_memb->ref);
63075078 q if (admissible_by_bbox(bbox, node_context))
63175078 q {
63275078 q Derived_Structure_Builder result(rman, "trigraph",
63375078 q new Point_Geometry(node_context.lat, node_context.lon), data.tags);
63575078 q if (node_context.local_id.val())
6360 result.set_tag("_local_ref", to_string(node_context.local_id.val()));
63875078 q if (mode == Localize_Statement::all)
64021 q result.set_tag("_child_node_ref", to_string(it_memb->ref.val()));
64121 q result.set_tag("_relation_ref", to_string(it_elem->id.val()));
64475078 q if (last)
64568488 q {
64668488 q last->tags.push_back(std::make_pair("_next", to_string(result.get().id.val())));
64768488 q result.set_tag("_previous", to_string(last->id.val()));
64868488 q }
64975078 q into.deriveds[Uint31_Index(0u)].push_back(result.get());
65075078 q last = &into.deriveds[Uint31_Index(0u)].back();
65175078 q }
6520 else
6530 last = 0;
65475078 q }
6551733748 q else if (it_memb->type == Relation_Entry::WAY)
6561726248 q {
6571726248 q std::vector< NWR_Context::Way_Section_Context >& way_context
6581726248 q = nwr_context.context_by_way_id(Way_Skeleton::Id_Type(it_memb->ref.val()));
6601726248 q if (way_context.empty())
661795985 q {
662795985 q last = 0;
663795985 q continue;
664795985 q }
666930268 q enum { both_possible, must_forwards, must_backwards } elem_orientation = both_possible;
667930268 q if (last && last_point.lat <= 90.)
668920518 q {
669920518 q if (!way_context.front().points.empty() && last_point == way_context.front().points.front())
670597068 q elem_orientation = must_forwards;
671323458 q else if (!way_context.back().points.empty() && last_point == way_context.back().points.back())
672296418 q elem_orientation = must_backwards;
673920518 q }
674930268 q if (elem_orientation == both_possible)
67536798 q {
67636798 q std::vector< Relation_Entry >::const_iterator it_next = it_memb+1;
67736798 q if (it_next != data.object->members.end() && it_next->type == Relation_Entry::WAY)
67821638 q {
67921638 q std::vector< NWR_Context::Way_Section_Context >& next_way_context
68021638 q = nwr_context.context_by_way_id(Way_Skeleton::Id_Type(it_next->ref.val()));
68121638 q if (!next_way_context.empty())
68220348 q {
68320348 q if (!way_context.back().points.empty()
68420348 q && ((!next_way_context.front().points.empty()
68520348 q && next_way_context.front().points.front() == way_context.back().points.back())
68620348 q || (!next_way_context.back().points.empty()
68712348 q && next_way_context.back().points.back() == way_context.back().points.back())))
6889138 q elem_orientation = must_forwards;
68911218 q else if (!way_context.front().points.empty()
69011218 q && ((!next_way_context.front().points.empty()
69111218 q && next_way_context.front().points.front() == way_context.front().points.front())
69211218 q || (!next_way_context.back().points.empty()
69310228 q && next_way_context.back().points.back() == way_context.front().points.front())))
6946348 q elem_orientation = must_backwards;
69520348 q }
69621638 q }
69736798 q }
699930268 q if (elem_orientation != must_backwards)
700627518 q {
7011780508 q for (uint i = 0; i < way_context.size(); ++i)
7021152998 q {
7031152998 q if (way_context[i].points.empty())
7040 {
7050 last = 0;
7060 continue;
7070 }
7091152998 q Derived_Structure_Builder result(rman, "trigraph",
7101152998 q new Linestring_Geometry(way_context[i].points), data.tags);
7121152998 q if (way_context[i].local_from.val())
71362 q result.set_tag("_vertex_local_from", to_string(way_context[i].local_from.val()));
7141152998 q if (way_context[i].local_to.val())
71562 q result.set_tag("_vertex_local_to", to_string(way_context[i].local_to.val()));
7161152998 q if (way_context[i].local_id.val())
71717756 q result.set_tag("_local_ref", to_string(way_context[i].local_id.val()));
7191152998 q if (mode == Localize_Statement::all)
72151 q result.set_tag("_child_way_ref", to_string(it_memb->ref.val()));
72251 q result.set_tag("_relation_ref", to_string(it_elem->id.val()));
7251152998 q if (last)
7261147228 q {
7271147228 q last->tags.push_back(std::make_pair("_next", to_string(result.get().id.val())));
7281147228 q result.set_tag("_previous", to_string(last->id.val()));
7291147228 q }
7301152998 q into.deriveds[Uint31_Index(0u)].push_back(result.get());
7311152998 q last = &into.deriveds[Uint31_Index(0u)].back();
7321152998 q }
733627518 q last_point = (way_context.empty() || way_context.back().points.empty()) ? Point_Double(100., 0.)
734627518 q : way_context.back().points.back();
735627518 q }
736302758 q else
737302758 q {
738922948 q for (int i = way_context.size()-1; i >= 0; --i)
739620198 q {
740620198 q if (way_context[i].points.empty())
7410 {
7420 last = 0;
7430 continue;
7440 }
746620198 q std::vector< Point_Double > points = way_context[i].points;
747620198 q std::reverse(points.begin(), points.end());
748620198 q Derived_Structure_Builder result(rman, "trigraph",
749620198 q new Linestring_Geometry(points), data.tags);
751620198 q if (way_context[i].local_to.val())
75282 q result.set_tag("_vertex_local_from", to_string(way_context[i].local_to.val()));
753620198 q if (way_context[i].local_from.val())
75482 q result.set_tag("_vertex_local_to", to_string(way_context[i].local_from.val()));
755620198 q if (way_context[i].local_id.val())
7568605 q result.set_tag("_local_ref", to_string(way_context[i].local_id.val()));
758620198 q if (mode == Localize_Statement::all)
76031 q result.set_tag("_child_way_ref", to_string(it_memb->ref.val()));
76131 q result.set_tag("_relation_ref", to_string(it_elem->id.val()));
764620198 q if (last)
765618968 q {
766618968 q last->tags.push_back(std::make_pair("_next", to_string(result.get().id.val())));
767618968 q result.set_tag("_previous", to_string(last->id.val()));
768618968 q }
769620198 q into.deriveds[Uint31_Index(0u)].push_back(result.get());
770620198 q last = &into.deriveds[Uint31_Index(0u)].back();
771620198 q }
772302758 q last_point = (way_context.empty() || way_context.front().points.empty()) ? Point_Double(100., 0.)
773302758 q : way_context.front().points.front();
774302758 q }
775930268 q }
7767505 q else if (mode >= Localize_Statement::also_loose)
7776074 q {
7786074 q Derived_Structure_Builder result(rman, "trigraph", new Null_Geometry(), data.tags);
7806074 q if (mode == Localize_Statement::all)
7810 {
7820 result.set_tag("_child_relation_ref", to_string(it_memb->ref.val()));
7830 result.set_tag("_relation_ref", to_string(it_elem->id.val()));
7840 }
7866074 q if (last)
7875674 q {
7885674 q last->tags.push_back(std::make_pair("_next", to_string(result.get().id.val())));
7895674 q result.set_tag("_previous", to_string(last->id.val()));
7905674 q }
7916074 q into.deriveds[Uint31_Index(0u)].push_back(result.get());
7926074 q last = &into.deriveds[Uint31_Index(0u)].back();
7936074 q }
7941431 q else
795 // Relation members do not have any effect without loose mode
7961431 q last = 0;
7971808818 q }
7988528 q }
7998528 q }
8001478 q }
804void Localize_Statement::execute(Resource_Manager& rman)
806 //TODO: diff
8082119 q Set into;
8102119 q const Set* input_set = rman.get_set(input);
8112119 q if (!input_set)
8120 {
8130 transfer_output(rman, into);
8140 return;
8150 }
8172119 q Owner< Bbox_Double > bbox(south <= 90. ? new Bbox_Double(south, west, north, east) : 0);
8192119 q Requested_Context requested_context;
8202119 q requested_context.add_usage(input, Set_Usage::TAGS);
8212119 q Prepare_Task_Context context(requested_context, *this, rman);
8222119 q Set_With_Context* context_from = context.get_set(input);
8242119 q if (context_from && context_from->base)
8262119 q NWR_Context nwr_context(rman, *this, *input_set, bbox);
8272119 q process_vertices(context_from->base->nodes, *context_from, into, rman, nwr_context, type, bbox);
8282119 q if (!context_from->base->attic_nodes.empty())
82943 q process_vertices(context_from->base->attic_nodes, *context_from, into, rman, nwr_context, type, bbox);
8312119 q nwr_context.prepare_links(rman, *this, *input_set, bbox);
8322119 q generate_links(context_from->base->ways, *context_from, into, rman, nwr_context, type, bbox);
8332119 q if (!context_from->base->attic_ways.empty())
83454 q generate_links(context_from->base->attic_ways, *context_from, into, rman, nwr_context, type, bbox);
8362119 q if (type >= also_loose)
8381614 q generate_loose_links(context_from->base->ways, *context_from, into, rman, type);
8391614 q if (!context_from->base->attic_ways.empty())
84043 q generate_loose_links(context_from->base->attic_ways, *context_from, into, rman, type);
8432119 q generate_trigraphs(context_from->base->relations, *context_from, into, rman, nwr_context, type, bbox);
8442119 q if (!context_from->base->attic_ways.empty())
84554 q generate_trigraphs(context_from->base->attic_relations, *context_from, into, rman, nwr_context, type, bbox);
8482119 q transfer_output(rman, into);
8492119 q rman.health_check(*this);