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

overpass_api/statements/localize.cc

503 of 587 instrumented lines run by the suite (85.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 "../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)
316020 q : Output_Statement(line_number_), type(data)
336020 q std::map< std::string, std::string > attributes;
356020 q attributes["from"] = "_";
366020 q attributes["into"] = "_";
376020 q attributes["type"] = "l";
386020 q attributes["s"] = "";
396020 q attributes["n"] = "";
406020 q attributes["w"] = "";
416020 q attributes["e"] = "";
436020 q eval_attributes_array(get_name(), attributes, input_attributes);
456020 q set_output(attributes["into"]);
466020 q input = attributes["from"];
486020 q if (attributes["type"] == "ll")
49155 q type = also_loose;
504515 q else if (attributes["type"] == "llb")
51248 q type = all;
52217 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.");
556020 q south = 100.;
566020 q if (!attributes["s"].empty() || !attributes["n"].empty()
576020 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 {
9023521015 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 {
1002646713 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;
1131164712 q Node_Context& context_by_node_id(Node_Skeleton::Id_Type id) { return context_by_node_id_[id]; }
1141347013 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)
12672261315 q {
12772261315 q if (lhs->ll_lower != rhs->ll_lower)
12872238615 q return lhs->ll_lower < rhs->ll_lower;
1292274 q return lhs->id < rhs->id;
13072261315 q }
131};
134bool admissible_by_bbox(const Owner< Bbox_Double >& bbox, const NWR_Context::Node_Context& node_context)
1351164712 q{
1361164712 q return (!bbox || bbox->contains(Point_Double(node_context.lat, node_context.lon)));
1371164712 q}
140bool admissible_by_bbox(const Owner< Bbox_Double >& bbox, const std::vector< Point_Double >& points)
1412646713 q{
1422646713 q if (!bbox)
1432646713 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)
16216820 q for (typename std::map< Index, std::vector< Object > >::const_iterator
1631561820 q it_idx = container.begin(); it_idx != container.end(); ++it_idx)
1641545015 q {
1651545015 q for (typename std::vector< Object >::const_iterator it_elem = it_idx->second.begin();
16615841915 q it_elem != it_idx->second.end(); ++it_elem)
16714296915 q task(it_idx->first, *it_elem);
1681545015 q }
172struct Set_Coord_In_Context
174 void operator()(Uint32_Index idx, const Node_Skeleton& node)
17510381915 q {
17610381915 q NWR_Context::Node_Context& context = (*context_by_node_id)[node.id];
17710381915 q context.lat = ::lat(idx.val(), node.ll_lower);
17810381915 q context.lon = ::lon(idx.val(), node.ll_lower);
17910381915 q }
181 Set_Coord_In_Context(std::map< Node_Skeleton::Id_Type, NWR_Context::Node_Context >& context_by_node_id_)
1824220 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)
1911305013 q {
1921305013 q if (!way.nds.empty())
1931305013 q {
1941305013 q (*context_by_node_id)[way.nds.front()].count += 2;
19524105613 q for (uint i = 1; i < way.nds.size()-1; ++i)
19622800613 q ++(*context_by_node_id)[way.nds[i]].count;
1971305013 q (*context_by_node_id)[way.nds.back()].count += 2;
1981305013 q }
1991305013 q }
201 Count_Node_Use(std::map< Node_Skeleton::Id_Type, NWR_Context::Node_Context >& context_by_node_id_)
2024220 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)
2111305013 q {
2121305013 q if (!way.nds.empty())
2131305013 q {
2141305013 q std::vector< NWR_Context::Way_Section_Context >& way_context = (*context_by_way_id)[way.id];
2161305013 q std::vector< NWR_Context::Node_Context* > nds_contexts;
2171305013 q nds_contexts.reserve(way.nds.size());
21811601013 q for (std::vector< Node::Id_Type >::const_iterator it_nds = way.nds.begin(); it_nds != way.nds.end(); ++it_nds)
21910958213 q {
22010958213 q std::map< Node_Skeleton::Id_Type, NWR_Context::Node_Context >::iterator it_ctx
22110958213 q = context_by_node_id->find(*it_nds);
22210958213 q if (it_ctx == context_by_node_id->end() || it_ctx->second.lat == 100.)
223662213 q return;
22410296013 q nds_contexts.push_back(&it_ctx->second);
22510296013 q }
227642813 q uint start = 0;
228642813 q const NWR_Context::Node_Context& start_context = *nds_contexts[start];
229642813 q way_context.push_back(NWR_Context::Way_Section_Context(start));
230642813 q way_context.back().local_from = start_context.local_id;
231642813 q way_context.back().points.push_back(Point_Double(start_context.lat, start_context.lon));
2339095213 q for (uint i = 1; i < way.nds.size()-1; ++i)
2348452413 q {
2358452413 q NWR_Context::Node_Context& context = *nds_contexts[i];
2368452413 q way_context.back().points.push_back(Point_Double(context.lat, context.lon));
2388452413 q if (context.local_id.val() || context.count > 1)
2392003912 q {
2402003912 q context.link_by_origin[way.nds[start]].push_back(way.id);
2412003912 q start = i;
2422003912 q way_context.back().local_to = context.local_id;
2432003912 q if (!admissible_by_bbox(*bbox, way_context.back().points))
2440 way_context.back().points.clear();
2452003912 q way_context.push_back(NWR_Context::Way_Section_Context(start));
2462003912 q way_context.back().local_from = context.local_id;
2472003912 q way_context.back().points.push_back(Point_Double(context.lat, context.lon));
2482003912 q }
2498452413 q }
251642813 q NWR_Context::Node_Context& context = *nds_contexts.back();
252642813 q way_context.back().points.push_back(Point_Double(context.lat, context.lon));
253642813 q way_context.back().local_to = context.local_id;
254642813 q context.link_by_origin[way.nds[start]].push_back(way.id);
255642813 q if (!admissible_by_bbox(*bbox, way_context.back().points))
2560 way_context.back().points.clear();
257642813 q }
2581305013 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_)
2644220 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)
2751305013 q {
2761305013 q if (!way.nds.empty())
2771305013 q {
2781305013 q std::vector< NWR_Context::Way_Section_Context >& way_context = (*context_by_way_id)[way.id];
2791305013 q std::vector< std::pair< Node_Skeleton::Id_Type, Node_Skeleton::Id_Type > > segment_count;
2813951713 q for (uint i = 0; i < way_context.size(); ++i)
2822646713 q {
2832646713 q NWR_Context::Node_Context& context = (*context_by_node_id)[
2842646713 q i+1 < way_context.size() ? way.nds[way_context[i+1].pos] : way.nds.back()];
2852646713 q const std::vector< Way_Skeleton::Id_Type >& parallel_links
2862646713 q = context.link_by_origin.find(way.nds[way_context[i].pos])->second;
2872646713 q if (parallel_links.size() > 1)
28856309 q {
28956309 q uint parallel_links_pos = 0;
29085809 q while (parallel_links_pos < parallel_links.size()
29185809 q && !(parallel_links[parallel_links_pos] == way.id))
29229509 q ++parallel_links_pos;
29356309 q if (parallel_links_pos+1 == parallel_links.size() || !(parallel_links[parallel_links_pos+1] == way.id))
29456309 q way_context[i].local_id = parallel_links_pos + 1;
2950 else
2960 {
2970 Node_Skeleton::Id_Type target_id =
2980 (i+1 < way_context.size() ? way.nds[way_context[i+1].pos] : way.nds.back());
2990 for (std::vector< std::pair< Node_Skeleton::Id_Type, Node_Skeleton::Id_Type > >::const_iterator
3000 it_pair = segment_count.begin(); it_pair != segment_count.end(); ++it_pair)
3010 {
3020 if (it_pair->first == way.nds[way_context[i].pos] && it_pair->second == target_id)
3030 ++parallel_links_pos;
3040 }
3050 way_context[i].local_id = parallel_links_pos + 1;
3060 segment_count.push_back(std::make_pair(way.nds[way_context[i].pos], target_id));
3070 }
30856309 q }
3092646713 q }
3101305013 q }
3111305013 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_)
3164220 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
3282120 q Request_Context context(&stmt, rman);
3292120 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 }
3342120 q else
3352120 q add_nw_member_objects(context, input_set, relevant_nwrs);
3372120 q Set extra_ways;
3382120 q if (rman.get_desired_timestamp() == NOW)
3401717 q collect_ways(context, relevant_nwrs.nodes, {}, 0, {}, true).swap(extra_ways.ways, extra_ways.attic_ways);
3411717 q sort_second(extra_ways.ways);
3421717 q indexed_set_union(relevant_nwrs.ways, extra_ways.ways);
34444 q else
34644 q collect_ways(context, relevant_nwrs.nodes, relevant_nwrs.attic_nodes, 0,
34744 q {}, true).swap(extra_ways.ways, extra_ways.attic_ways);
34844 q indexed_set_union(relevant_nwrs.attic_ways, extra_ways.attic_ways);
34944 q indexed_set_union(relevant_nwrs.ways, extra_ways.ways);
35044 q keep_matching_skeletons(relevant_nwrs.ways, relevant_nwrs.attic_ways, rman.get_desired_timestamp());
353 // Build local ids for node segments
3552120 q indexed_set_union(relevant_nwrs.nodes, input_set.nodes);
3562120 q indexed_set_union(relevant_nwrs.attic_nodes, input_set.attic_nodes);
3572120 q keep_matching_skeletons(relevant_nwrs.nodes, relevant_nwrs.attic_nodes, rman.get_desired_timestamp());
3592120 q std::map< Uint32_Index, std::vector< Node_Skeleton > >::const_iterator it_now_idx
3602120 q = relevant_nwrs.nodes.begin();
3612120 q std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >::const_iterator it_attic_idx
3622120 q = relevant_nwrs.attic_nodes.begin();
364700920 q while (it_now_idx != relevant_nwrs.nodes.end() || it_attic_idx != relevant_nwrs.attic_nodes.end())
365698815 q {
366698815 q std::vector< const Node_Skeleton* > elem_refs;
368698815 q bool now_relevant = false;
369698815 q bool attic_relevant = false;
370698815 q if (it_attic_idx == relevant_nwrs.attic_nodes.end())
371681613 q now_relevant = true;
3721722 q else if (it_now_idx == relevant_nwrs.nodes.end())
373241 q attic_relevant = true;
3741482 q else if (it_now_idx->first < it_attic_idx->first)
375502 q now_relevant = true;
376982 q else if (it_attic_idx->first < it_now_idx->first)
377751 q attic_relevant = true;
378232 q else
379232 q {
380232 q now_relevant = true;
381232 q attic_relevant = true;
382232 q }
384698815 q if (now_relevant)
385688915 q {
386688915 q for (std::vector< Node_Skeleton >::const_iterator it_elem = it_now_idx->second.begin();
38711018115 q it_elem != it_now_idx->second.end(); ++it_elem)
38810329215 q elem_refs.push_back(&*it_elem);
390688915 q ++it_now_idx;
391688915 q }
393698815 q if (attic_relevant)
3941222 q {
3951222 q for (std::vector< Attic< Node_Skeleton > >::const_iterator it_elem = it_attic_idx->second.begin();
3966492 q it_elem != it_attic_idx->second.end(); ++it_elem)
3975272 q elem_refs.push_back(&*it_elem);
3991222 q ++it_attic_idx;
4001222 q }
402698815 q std::sort(elem_refs.begin(), elem_refs.end(), Node_Skeleton_By_Coord_Then_Id());
404698815 q uint equal_since = 0;
40510381915 q for (uint i = 1; i < elem_refs.size(); ++i)
4069683115 q {
4079683115 q if (elem_refs[equal_since]->ll_lower == elem_refs[i]->ll_lower)
4081764 q {
4091764 q if (i - equal_since == 1)
4101664 q context_by_node_id_[elem_refs[equal_since]->id].local_id = 1;
4111764 q context_by_node_id_[elem_refs[i]->id].local_id = i + 1 - equal_since;
4121764 q }
4139665515 q else
4149665515 q equal_since = i;
4159683115 q }
416698815 q }
418 // Store node coordinates for use by links
4192120 q for_each_elem(relevant_nwrs.nodes, Set_Coord_In_Context(context_by_node_id_));
4202120 q for_each_elem(relevant_nwrs.attic_nodes, Set_Coord_In_Context(context_by_node_id_));
422 // Count node use by ways
4232120 q for_each_elem(relevant_nwrs.ways, Count_Node_Use(context_by_node_id_));
4242120 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
4322120 q for_each_elem(relevant_nwrs.ways, Partition_Into_Links(context_by_node_id_, context_by_way_id_, bbox));
4332120 q for_each_elem(relevant_nwrs.attic_ways, Partition_Into_Links(context_by_node_id_, context_by_way_id_, bbox));
4352120 q for (std::map< Node_Skeleton::Id_Type, Node_Context >::iterator it_ctx = context_by_node_id_.begin();
43623522820 q it_ctx != context_by_node_id_.end(); ++it_ctx)
43723520715 q {
43823520715 q for (std::map< Node_Skeleton::Id_Type, std::vector< Way_Skeleton::Id_Type > >::iterator
43925881415 q it_orig = it_ctx->second.link_by_origin.begin(); it_orig != it_ctx->second.link_by_origin.end(); ++it_orig)
4402360713 q std::sort(it_orig->second.begin(), it_orig->second.end());
44123520715 q }
443 // Figure out local ids of links
4442120 q for_each_elem(relevant_nwrs.ways, Figure_Out_Local_Ids_Of_Links(context_by_node_id_, context_by_way_id_));
4452120 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
4501101911 q{
4511101911 q std::map< Node_Skeleton::Id_Type, Node_Context >::const_iterator it
4521101911 q = context_by_node_id_.find(id);
4531101911 q if (it != context_by_node_id_.end())
4541101911 q return it->second.local_id;
4550 return Derived_Skeleton::Id_Type(0ull);
4561101911 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)
4644963214 q : target(name, 0ull)
4654963214 q {
4664963214 q target.id = rman.get_global_settings().dispense_derived_id();
4674963214 q target.acquire_geometry(geometry);
4694963214 q if (tags)
4704466114 q {
4714466114 q for (std::vector< std::pair< std::string, std::string > >::const_iterator it_tag = tags->begin();
47234414814 q it_tag != tags->end(); ++it_tag)
47329948714 q target.tags.push_back(std::make_pair(
47429948714 q it_tag->first.empty() ? "" : it_tag->first[0] == '_' ? "_" + it_tag->first : it_tag->first,
47529948714 q it_tag->second));
4764466114 q }
4774963214 q }
479 void set_tag(const std::string& key, const std::string& value)
4803889912 q { target.tags.push_back(std::make_pair(key, value)); }
482 const Derived_Structure& get()
4837254914 q {
4847254914 q std::sort(target.tags.begin(), target.tags.end());
4857254914 q return target;
4867254914 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)
4982320 q for (typename std::map< Index, std::vector< Maybe_Attic > >::const_iterator it_idx = items.begin();
499513720 q it_idx != items.end(); ++it_idx)
500511413 q {
501511413 q for (typename std::vector< Maybe_Attic >::const_iterator it_elem = it_idx->second.begin();
5027070913 q it_elem != it_idx->second.end(); ++it_elem)
5036559513 q {
5046559513 q const Element_With_Context< Maybe_Attic >& data = context_from.get_context(it_idx->first, *it_elem);
5066559513 q if (mode == Localize_Statement::all || (data.tags && !data.tags->empty()))
5071101911 q {
5081101911 q NWR_Context::Node_Context& node_context = nwr_context.context_by_node_id(it_elem->id);
5091101911 q if (data.tags && !data.tags->empty())
51068279 q ++node_context.count;
5121101911 q if (admissible_by_bbox(bbox, node_context))
5131101911 q {
5141101911 q Derived_Structure_Builder result(rman, "vertex",
5151101911 q new Point_Geometry(
5161101911 q ::lat(it_idx->first.val(), it_elem->ll_lower), ::lon(it_idx->first.val(), it_elem->ll_lower)),
5171101911 q data.tags);
5191101911 q Derived_Skeleton::Id_Type local_id = nwr_context.local_id_by_node_id(it_elem->id);
5201101911 q if (local_id.val())
52132 q result.set_tag("_local_id", to_string(local_id.val()));
5231101911 q if (mode == Localize_Statement::all)
52469234 q result.set_tag("_node_ref", to_string(it_elem->id.val()));
5261101911 q into.deriveds[Uint31_Index(0u)].push_back(result.get());
5271101911 q }
5281101911 q }
5296559513 q }
530511413 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)
5392320 q for (typename std::map< Index, std::vector< Maybe_Attic > >::const_iterator it_idx = items.begin();
540121620 q it_idx != items.end(); ++it_idx)
541119312 q {
542119312 q for (typename std::vector< Maybe_Attic >::const_iterator it_elem = it_idx->second.begin();
543596012 q it_elem != it_idx->second.end(); ++it_elem)
544476712 q {
545476712 q const Element_With_Context< Maybe_Attic >& data = context_from.get_context(it_idx->first, *it_elem);
547476712 q if (mode == Localize_Statement::all || (data.tags && !data.tags->empty()))
548395911 q {
549395911 q std::vector< NWR_Context::Way_Section_Context >& way_context = nwr_context.context_by_way_id(it_elem->id);
551395911 q for (std::vector< NWR_Context::Way_Section_Context >::const_iterator it_way_ctx = way_context.begin();
5521921911 q it_way_ctx != way_context.end(); ++it_way_ctx)
5531526011 q {
5541526011 q if (it_way_ctx->points.empty())
5550 continue;
5571526011 q Derived_Structure_Builder result(rman, "link",
5581526011 q new Linestring_Geometry(it_way_ctx->points), data.tags);
5601526011 q if (it_way_ctx->local_from.val())
5611074 q result.set_tag("_local_from", to_string(it_way_ctx->local_from.val()));
5621526011 q if (it_way_ctx->local_to.val())
5631044 q result.set_tag("_local_to", to_string(it_way_ctx->local_to.val()));
5641526011 q if (it_way_ctx->local_id.val())
56513928 q result.set_tag("_local_id", to_string(it_way_ctx->local_id.val()));
5671526011 q if (mode == Localize_Statement::all)
56828065 q result.set_tag("_way_ref", to_string(it_elem->id.val()));
5701526011 q into.deriveds[Uint31_Index(0u)].push_back(result.get());
5711526011 q }
572395911 q }
573476712 q }
574119312 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)
5821513 q for (typename std::map< Index, std::vector< Maybe_Attic > >::const_iterator it_idx = items.begin();
583103413 q it_idx != items.end(); ++it_idx)
58410199 q {
58510199 q for (typename std::vector< Maybe_Attic >::const_iterator it_elem = it_idx->second.begin();
58655529 q it_elem != it_idx->second.end(); ++it_elem)
58745339 q {
58845339 q const Element_With_Context< Maybe_Attic >& data = context_from.get_context(it_idx->first, *it_elem);
59045339 q if ((mode == Localize_Statement::all || (data.tags && !data.tags->empty()))
59145339 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 }
60045339 q }
60110199 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)
6102320 q for (typename std::map< Index, std::vector< Maybe_Attic > >::const_iterator it_idx = items.begin();
61125120 q it_idx != items.end(); ++it_idx)
6122286 q {
6132286 q for (typename std::vector< Maybe_Attic >::const_iterator it_elem = it_idx->second.begin();
6146636 q it_elem != it_idx->second.end(); ++it_elem)
6154356 q {
6164356 q const Element_With_Context< Maybe_Attic >& data = context_from.get_context(it_idx->first, *it_elem);
6184356 q if (mode == Localize_Statement::all || (data.tags && !data.tags->empty()))
6194356 q {
6204356 q Derived_Structure* last = 0;
6214356 q Point_Double last_point(100., 0.);
6234356 q for (std::vector< Relation_Entry >::const_iterator it_memb = data.object->members.begin();
62498596 q it_memb != data.object->members.end(); ++it_memb)
62594246 q {
62694246 q if (it_memb->type == Relation_Entry::NODE)
6276283 q {
6286283 q NWR_Context::Node_Context& node_context = nwr_context.context_by_node_id(it_memb->ref);
6306283 q if (admissible_by_bbox(bbox, node_context))
6316283 q {
6326283 q Derived_Structure_Builder result(rman, "trigraph",
6336283 q new Point_Geometry(node_context.lat, node_context.lon), data.tags);
6356283 q if (node_context.local_id.val())
6360 result.set_tag("_local_ref", to_string(node_context.local_id.val()));
6386283 q if (mode == Localize_Statement::all)
6390 {
6400 result.set_tag("_child_node_ref", to_string(it_memb->ref.val()));
6410 result.set_tag("_relation_ref", to_string(it_elem->id.val()));
6420 }
6446283 q if (last)
6456143 q {
6466143 q last->tags.push_back(std::make_pair("_next", to_string(result.get().id.val())));
6476143 q result.set_tag("_previous", to_string(last->id.val()));
6486143 q }
6496283 q into.deriveds[Uint31_Index(0u)].push_back(result.get());
6506283 q last = &into.deriveds[Uint31_Index(0u)].back();
6516283 q }
6520 else
6530 last = 0;
6546283 q }
65587966 q else if (it_memb->type == Relation_Entry::WAY)
65684516 q {
65784516 q std::vector< NWR_Context::Way_Section_Context >& way_context
65884516 q = nwr_context.context_by_way_id(Way_Skeleton::Id_Type(it_memb->ref.val()));
66084516 q if (way_context.empty())
66114132 q {
66214132 q last = 0;
66314132 q continue;
66414132 q }
66670386 q enum { both_possible, must_forwards, must_backwards } elem_orientation = both_possible;
66770386 q if (last && last_point.lat <= 90.)
66866076 q {
66966076 q if (!way_context.front().points.empty() && last_point == way_context.front().points.front())
67031785 q elem_orientation = must_forwards;
67134296 q else if (!way_context.back().points.empty() && last_point == way_context.back().points.back())
67227276 q elem_orientation = must_backwards;
67366076 q }
67470386 q if (elem_orientation == both_possible)
67511336 q {
67611336 q std::vector< Relation_Entry >::const_iterator it_next = it_memb+1;
67711336 q if (it_next != data.object->members.end() && it_next->type == Relation_Entry::WAY)
67810606 q {
67910606 q std::vector< NWR_Context::Way_Section_Context >& next_way_context
68010606 q = nwr_context.context_by_way_id(Way_Skeleton::Id_Type(it_next->ref.val()));
68110606 q if (!next_way_context.empty())
68210596 q {
68310596 q if (!way_context.back().points.empty()
68410596 q && ((!next_way_context.front().points.empty()
68510596 q && next_way_context.front().points.front() == way_context.back().points.back())
68610596 q || (!next_way_context.back().points.empty()
6877386 q && next_way_context.back().points.back() == way_context.back().points.back())))
6884395 q elem_orientation = must_forwards;
6896205 q else if (!way_context.front().points.empty()
6906205 q && ((!next_way_context.front().points.empty()
6916205 q && next_way_context.front().points.front() == way_context.front().points.front())
6926205 q || (!next_way_context.back().points.empty()
6935365 q && next_way_context.back().points.back() == way_context.front().points.front())))
6943015 q elem_orientation = must_backwards;
69510596 q }
69610606 q }
69711336 q }
69970386 q if (elem_orientation != must_backwards)
70040106 q {
701189606 q for (uint i = 0; i < way_context.size(); ++i)
702149506 q {
703149506 q if (way_context[i].points.empty())
7040 {
7050 last = 0;
7060 continue;
7070 }
709149506 q Derived_Structure_Builder result(rman, "trigraph",
710149506 q new Linestring_Geometry(way_context[i].points), data.tags);
712149506 q if (way_context[i].local_from.val())
7132521 q result.set_tag("_vertex_local_from", to_string(way_context[i].local_from.val()));
714149506 q if (way_context[i].local_to.val())
7152511 q result.set_tag("_vertex_local_to", to_string(way_context[i].local_to.val()));
716149506 q if (way_context[i].local_id.val())
71732794 q result.set_tag("_local_ref", to_string(way_context[i].local_id.val()));
719149506 q if (mode == Localize_Statement::all)
7201321 q {
7211321 q result.set_tag("_child_way_ref", to_string(it_memb->ref.val()));
7221321 q result.set_tag("_relation_ref", to_string(it_elem->id.val()));
7231321 q }
725149506 q if (last)
726146776 q {
727146776 q last->tags.push_back(std::make_pair("_next", to_string(result.get().id.val())));
728146776 q result.set_tag("_previous", to_string(last->id.val()));
729146776 q }
730149506 q into.deriveds[Uint31_Index(0u)].push_back(result.get());
731149506 q last = &into.deriveds[Uint31_Index(0u)].back();
732149506 q }
73340106 q last_point = (way_context.empty() || way_context.back().points.empty()) ? Point_Double(100., 0.)
73440106 q : way_context.back().points.back();
73540106 q }
73630286 q else
73730286 q {
738104606 q for (int i = way_context.size()-1; i >= 0; --i)
73974326 q {
74074326 q if (way_context[i].points.empty())
7410 {
7420 last = 0;
7430 continue;
7440 }
74674326 q std::vector< Point_Double > points = way_context[i].points;
74774326 q std::reverse(points.begin(), points.end());
74874326 q Derived_Structure_Builder result(rman, "trigraph",
74974326 q new Linestring_Geometry(points), data.tags);
75174326 q if (way_context[i].local_to.val())
752742 q result.set_tag("_vertex_local_from", to_string(way_context[i].local_to.val()));
75374326 q if (way_context[i].local_from.val())
754742 q result.set_tag("_vertex_local_to", to_string(way_context[i].local_from.val()));
75574326 q if (way_context[i].local_id.val())
7561973 q result.set_tag("_local_ref", to_string(way_context[i].local_id.val()));
75874326 q if (mode == Localize_Statement::all)
7591281 q {
7601281 q result.set_tag("_child_way_ref", to_string(it_memb->ref.val()));
7611281 q result.set_tag("_relation_ref", to_string(it_elem->id.val()));
7621281 q }
76474326 q if (last)
76572926 q {
76672926 q last->tags.push_back(std::make_pair("_next", to_string(result.get().id.val())));
76772926 q result.set_tag("_previous", to_string(last->id.val()));
76872926 q }
76974326 q into.deriveds[Uint31_Index(0u)].push_back(result.get());
77074326 q last = &into.deriveds[Uint31_Index(0u)].back();
77174326 q }
77230286 q last_point = (way_context.empty() || way_context.front().points.empty()) ? Point_Double(100., 0.)
77330286 q : way_context.front().points.front();
77430286 q }
77570386 q }
7763452 q else if (mode >= Localize_Statement::also_loose)
7773431 q {
7783431 q Derived_Structure_Builder result(rman, "trigraph", new Null_Geometry(), data.tags);
7803431 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 }
7863431 q if (last)
7873341 q {
7883341 q last->tags.push_back(std::make_pair("_next", to_string(result.get().id.val())));
7893341 q result.set_tag("_previous", to_string(last->id.val()));
7903341 q }
7913431 q into.deriveds[Uint31_Index(0u)].push_back(result.get());
7923431 q last = &into.deriveds[Uint31_Index(0u)].back();
7933431 q }
79421 q else
795 // Relation members do not have any effect without loose mode
79621 q last = 0;
79794246 q }
7984356 q }
7994356 q }
8002286 q }
804void Localize_Statement::execute(Resource_Manager& rman)
806 //TODO: diff
8082120 q Set into;
8102120 q const Set* input_set = rman.get_set(input);
8112120 q if (!input_set)
8120 {
8130 transfer_output(rman, into);
8140 return;
8150 }
8172120 q Owner< Bbox_Double > bbox(south <= 90. ? new Bbox_Double(south, west, north, east) : 0);
8192120 q Requested_Context requested_context;
8202120 q requested_context.add_usage(input, Set_Usage::TAGS);
8212120 q Prepare_Task_Context context(requested_context, *this, rman);
8222120 q Set_With_Context* context_from = context.get_set(input);
8242120 q if (context_from && context_from->base)
8262120 q NWR_Context nwr_context(rman, *this, *input_set, bbox);
8272120 q process_vertices(context_from->base->nodes, *context_from, into, rman, nwr_context, type, bbox);
8282120 q if (!context_from->base->attic_nodes.empty())
82922 q process_vertices(context_from->base->attic_nodes, *context_from, into, rman, nwr_context, type, bbox);
8312120 q nwr_context.prepare_links(rman, *this, *input_set, bbox);
8322120 q generate_links(context_from->base->ways, *context_from, into, rman, nwr_context, type, bbox);
8332120 q if (!context_from->base->attic_ways.empty())
83422 q generate_links(context_from->base->attic_ways, *context_from, into, rman, nwr_context, type, bbox);
8362120 q if (type >= also_loose)
8381413 q generate_loose_links(context_from->base->ways, *context_from, into, rman, type);
8391413 q if (!context_from->base->attic_ways.empty())
84011 q generate_loose_links(context_from->base->attic_ways, *context_from, into, rman, type);
8432120 q generate_trigraphs(context_from->base->relations, *context_from, into, rman, nwr_context, type, bbox);
8442120 q if (!context_from->base->attic_ways.empty())
84522 q generate_trigraphs(context_from->base->attic_relations, *context_from, into, rman, nwr_context, type, bbox);
8482120 q transfer_output(rman, into);
8492120 q rman.health_check(*this);