Overpass API regression suite coverage (Nova Scotia) / overpass_api/statements/per_member.h

overpass_api/statements/per_member.h

97 of 135 instrumented lines run by the suite (71.9%) 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#ifndef DE__OSM3S___OVERPASS_API__STATEMENTS__PER_MEMBER_H
20#define DE__OSM3S___OVERPASS_API__STATEMENTS__PER_MEMBER_H
23#include "../../expat/escape_json.h"
24#include "../../expat/escape_xml.h"
25#include "../data/tag_store.h"
26#include "../data/utils.h"
27#include "evaluator.h"
28#include "statement.h"
30#include <map>
31#include <string>
32#include <vector>
35struct Per_Member_Aggregator : public Evaluator
377616 q Per_Member_Aggregator(int line_number_) : Evaluator(line_number_), rhs(0) {}
38 virtual void add_statement(Statement* statement, std::string text);
40 Evaluator* rhs;
41};
44template< typename Evaluator_ >
45struct Per_Member_Aggregator_Syntax : public Per_Member_Aggregator
477616 q Per_Member_Aggregator_Syntax(int line_number_) : Per_Member_Aggregator(line_number_) {}
49 virtual std::string dump_xml(const std::string& indent) const
501916 q {
511916 q return indent + "<" + Evaluator_::stmt_name() + ">\n"
521916 q + (rhs ? rhs->dump_xml(indent + " ") : "")
531916 q + indent + "</" + Evaluator_::stmt_name() + ">\n";
541916 q }
56 virtual std::string dump_compact_ql(const std::string&) const
573816 q {
583816 q return Evaluator_::stmt_func_name() + "("
593816 q + (rhs ? rhs->dump_compact_ql("") : "")
603816 q + ")";
613816 q }
637616 q virtual std::string get_name() const { return Evaluator_::stmt_name(); }
640 virtual std::string get_result_name() const { return ""; }
65};
68template< typename Evaluator_ >
69struct Per_Member_Aggregator_Maker : Statement::Evaluator_Maker
71 virtual Statement* create_evaluator(
72 const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context,
73 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)
748017 q {
758017 q if (!tree_it.assert_is_function(error_output) || !tree_it.assert_has_input_set(error_output, false)
768017 q || !tree_it.assert_has_arguments(error_output, true)
778017 q || !assert_element_in_context(error_output, tree_it, tree_context))
7841 q return 0;
807616 q Statement* result = new Evaluator_(
817616 q tree_it->line_col.first, std::map< std::string, std::string >(), global_settings);
827616 q if (result)
837616 q {
847616 q Statement* rhs = stmt_factory.create_evaluator(
857616 q tree_it.rhs(), Statement::member_eval_possible,
867616 q Statement::Single_Return_Type_Checker(Statement::string));
877616 q if (rhs)
887616 q result->add_statement(rhs, "");
890 else if (error_output)
900 error_output->add_parse_error(Evaluator_::stmt_func_name() + "(...) needs an argument",
910 tree_it->line_col.first);
927616 q }
937616 q return result;
948017 q }
96 Per_Member_Aggregator_Maker()
974896612 q {
984896612 q Statement::maker_by_func_name()[Evaluator_::stmt_func_name()].push_back(this);
994896612 q }
100};
103/* === Per Member Aggregators ===
105Aggregators per member help to process information that does apply to only a single member of a way or relation.
106Examples of this are the position within the way or relation, geometric properties like the angles, or roles.
107A per member aggregator needs an element to operate on and executes its argument multiple times,
108passing the possible positions one by one in addition to the element to its argument.
110==== Per Member ====
112For each element, the aggregator executes its argument once per member of the element.
113The return value is a semicolon separated list of the return values.
114No deduplication or sotring takes place.
115Note that the aggregator returns an empty value for nodes and deriveds
116and does not execute its argument in that case.
118The syntax is
120 per_member(<Evaluator>)
121*/
123struct Per_Member_Eval_Task : public Eval_Task
1251110 q Per_Member_Eval_Task(Eval_Task* rhs) : rhs_task(rhs) {}
127218292 q virtual std::string eval(const std::string* key) const { return ""; }
129 virtual std::string eval(const Element_With_Context< Way_Skeleton >& data, const std::string* key) const;
130 virtual std::string eval(const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const;
131 virtual std::string eval(const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const;
132 virtual std::string eval(const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const;
134private:
135 Owner< Eval_Task > rhs_task;
136};
139class Evaluator_Per_Member : public Per_Member_Aggregator_Syntax< Evaluator_Per_Member >
141public:
142 struct Statement_Maker : public Generic_Statement_Maker< Evaluator_Per_Member >
143 {
1442448612 q Statement_Maker() : Generic_Statement_Maker< Evaluator_Per_Member >("eval-per-member") {}
145 };
146 static Statement_Maker statement_maker;
147 static Per_Member_Aggregator_Maker< Evaluator_Per_Member > evaluator_maker;
1492470612 q static std::string stmt_func_name() { return "per_member"; }
1506611 q static std::string stmt_name() { return "eval-per-member"; }
152 Evaluator_Per_Member(int line_number_, const std::map< std::string, std::string >& input_attributes,
153 Parsed_Query& global_settings);
1540 virtual std::string get_result_name() const { return ""; }
1550 virtual void execute(Resource_Manager& rman) {}
1560 virtual ~Evaluator_Per_Member() {}
158 virtual Requested_Context request_context() const
1591110 q { return (rhs ? rhs->request_context() : Requested_Context()).add_usage(Set_Usage::SKELETON); }
1614411 q virtual Statement::Eval_Return_Type return_type() const { return Statement::string; };
162 virtual Eval_Task* get_string_task(Prepare_Task_Context& context, const std::string* key);
163};
166/* ==== Per Vertex ====
168For each element, the aggregator executes its argument once per (inner) vertex of the element.
169Otherwise it behaves similar to the per_member operator.
170For closed ways, this means that it is executed once for all vertices but the first member.
171For open ways, it is executed once for all vertices but the first and last member.
172For relations its behaviour is currently undefined.
174The syntax is
176 per_vertex(<Evaluator>)
177*/
179struct Per_Vertex_Eval_Task : public Eval_Task
18198 q Per_Vertex_Eval_Task(Eval_Task* rhs) : rhs_task(rhs) {}
183160166 q virtual std::string eval(const std::string* key) const { return ""; }
185 virtual std::string eval(const Element_With_Context< Way_Skeleton >& data, const std::string* key) const;
186 virtual std::string eval(const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const;
188private:
189 Owner< Eval_Task > rhs_task;
190};
193class Evaluator_Per_Vertex : public Per_Member_Aggregator_Syntax< Evaluator_Per_Vertex >
195public:
196 struct Statement_Maker : public Generic_Statement_Maker< Evaluator_Per_Vertex >
197 {
1982448612 q Statement_Maker() : Generic_Statement_Maker< Evaluator_Per_Vertex >("eval-per-vertex") {}
199 };
200 static Statement_Maker statement_maker;
201 static Per_Member_Aggregator_Maker< Evaluator_Per_Vertex > evaluator_maker;
2032464612 q static std::string stmt_func_name() { return "per_vertex"; }
204488 q static std::string stmt_name() { return "eval-per-vertex"; }
206 Evaluator_Per_Vertex(int line_number_, const std::map< std::string, std::string >& input_attributes,
207 Parsed_Query& global_settings);
2080 virtual std::string get_result_name() const { return ""; }
2090 virtual void execute(Resource_Manager& rman) {}
2100 virtual ~Evaluator_Per_Vertex() {}
212 virtual Requested_Context request_context() const
21398 q { return (rhs ? rhs->request_context() : Requested_Context()).add_usage(Set_Usage::SKELETON); }
215328 q virtual Statement::Eval_Return_Type return_type() const { return Statement::string; };
216 virtual Eval_Task* get_string_task(Prepare_Task_Context& context, const std::string* key);
217};
220/* === Member Dependend Functions ===
222Member dependend functions can only be used when an element plus a position within the element is in context.
223They then deliver specific information for that member.
225==== Position of the Member ====
227The position function returns for a member its one-based position within the element.
229The syntax is
231 pos()
232*/
234struct Pos_Eval_Task : public Eval_Task
2360 virtual std::string eval(const std::string* key) const { return ""; }
238 virtual std::string eval(uint pos, const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
2397658653 q { return to_string(pos+1); }
240 virtual std::string eval(uint pos, const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
2410 { return to_string(pos+1); }
242 virtual std::string eval(uint pos, const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
24327401 q { return to_string(pos+1); }
244 virtual std::string eval(uint pos, const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
2450 { return to_string(pos+1); }
246};
249class Evaluator_Pos : public Evaluator
251public:
252 struct Statement_Maker : public Generic_Statement_Maker< Evaluator_Pos >
253 {
2542448612 q Statement_Maker() : Generic_Statement_Maker< Evaluator_Pos >("eval-pos") {}
255 };
256 static Statement_Maker statement_maker;
257 static Member_Function_Maker< Evaluator_Pos > evaluator_maker;
2592448612 q static std::string stmt_func_name() { return "pos"; }
260 virtual std::string dump_xml(const std::string& indent) const
26144 q { return indent + "<eval-pos/>\n"; }
26284 q virtual std::string dump_compact_ql(const std::string&) const { return "pos()"; }
264 Evaluator_Pos(int line_number_, const std::map< std::string, std::string >& input_attributes,
265 Parsed_Query& global_settings);
266164 q virtual std::string get_name() const { return "eval-pos"; }
2670 virtual std::string get_result_name() const { return ""; }
2680 virtual void execute(Resource_Manager& rman) {}
2690 virtual ~Evaluator_Pos() {}
27154 q virtual Requested_Context request_context() const { return Requested_Context().add_usage(Set_Usage::SKELETON); }
273164 q virtual Statement::Eval_Return_Type return_type() const { return Statement::string; };
27454 q virtual Eval_Task* get_string_task(Prepare_Task_Context& context, const std::string* key) { return new Pos_Eval_Task(); }
275};
278/* ==== Reference to the Member ====
280The functions mtype and reference return for a member the type resp. id of the referenced object.
282The syntax is
284 mtype()
286resp.
288 ref()
289*/
291struct Membertype_Eval_Task : public Eval_Task
2930 virtual std::string eval(const std::string* key) const { return ""; }
295 virtual std::string eval(uint pos, const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
29680662 q { return "node"; }
297 virtual std::string eval(uint pos, const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
2980 { return "node"; }
299 virtual std::string eval(uint pos, const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
3005561 q { return member_type_name(data.object->members[pos].type); }
301 virtual std::string eval(uint pos, const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
3020 { return member_type_name(data.object->members[pos].type); }
303};
306class Evaluator_Membertype : public Evaluator
308public:
309 struct Statement_Maker : public Generic_Statement_Maker< Evaluator_Membertype >
310 {
3112448612 q Statement_Maker() : Generic_Statement_Maker< Evaluator_Membertype >("eval-membertype") {}
312 };
313 static Statement_Maker statement_maker;
314 static Member_Function_Maker< Evaluator_Membertype > evaluator_maker;
3162448612 q static std::string stmt_func_name() { return "mtype"; }
317 virtual std::string dump_xml(const std::string& indent) const
31844 q { return indent + "<eval-membertype/>\n"; }
31984 q virtual std::string dump_compact_ql(const std::string&) const { return "mtype()"; }
321 Evaluator_Membertype(int line_number_, const std::map< std::string, std::string >& input_attributes,
322 Parsed_Query& global_settings);
323164 q virtual std::string get_name() const { return "eval-membertype"; }
3240 virtual std::string get_result_name() const { return ""; }
3250 virtual void execute(Resource_Manager& rman) {}
3260 virtual ~Evaluator_Membertype() {}
32833 q virtual Requested_Context request_context() const { return Requested_Context().add_usage(Set_Usage::SKELETON); }
330164 q virtual Statement::Eval_Return_Type return_type() const { return Statement::string; };
33133 q virtual Eval_Task* get_string_task(Prepare_Task_Context& context, const std::string* key) { return new Membertype_Eval_Task(); }
332};
335struct Ref_Eval_Task : public Eval_Task
3370 virtual std::string eval(const std::string* key) const { return ""; }
339 virtual std::string eval(uint pos, const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
34080662 q { return to_string(data.object->nds[pos].val()); }
341 virtual std::string eval(uint pos, const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
3420 { return to_string(data.object->nds[pos].val()); }
343 virtual std::string eval(uint pos, const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
34461 q { return to_string(data.object->members[pos].ref.val()); }
345 virtual std::string eval(uint pos, const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
3460 { return to_string(data.object->members[pos].ref.val()); }
347};
350class Evaluator_Ref : public Evaluator
352public:
353 struct Statement_Maker : public Generic_Statement_Maker< Evaluator_Ref >
354 {
3552448612 q Statement_Maker() : Generic_Statement_Maker< Evaluator_Ref >("eval-ref") {}
356 };
357 static Statement_Maker statement_maker;
358 static Member_Function_Maker< Evaluator_Ref > evaluator_maker;
3602448612 q static std::string stmt_func_name() { return "ref"; }
361 virtual std::string dump_xml(const std::string& indent) const
36244 q { return indent + "<eval-ref/>\n"; }
36384 q virtual std::string dump_compact_ql(const std::string&) const { return "ref()"; }
365 Evaluator_Ref(int line_number_, const std::map< std::string, std::string >& input_attributes,
366 Parsed_Query& global_settings);
367164 q virtual std::string get_name() const { return "eval-ref"; }
3680 virtual std::string get_result_name() const { return ""; }
3690 virtual void execute(Resource_Manager& rman) {}
3700 virtual ~Evaluator_Ref() {}
37233 q virtual Requested_Context request_context() const { return Requested_Context().add_usage(Set_Usage::SKELETON); }
374164 q virtual Statement::Eval_Return_Type return_type() const { return Statement::string; };
37533 q virtual Eval_Task* get_string_task(Prepare_Task_Context& context, const std::string* key) { return new Ref_Eval_Task(); }
376};
379/* ==== Role of the Member ====
381The role function returns the role of the member.
383The syntax is
385 role()
386*/
388struct Role_Eval_Task : public Eval_Task
39033 q Role_Eval_Task(const std::map< uint32, std::string >* roles_) : roles(roles_) {}
3920 virtual std::string eval(const std::string* key) const { return ""; }
394 virtual std::string eval(uint pos, const Element_With_Context< Way_Skeleton >& data, const std::string* key) const
39559422 q { return ""; }
396 virtual std::string eval(uint pos, const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const
3970 { return ""; }
398 virtual std::string eval(uint pos, const Element_With_Context< Relation_Skeleton >& data, const std::string* key) const
3993869851 q { return roles ? roles->find(data.object->members[pos].role)->second : std::string(); }
400 virtual std::string eval(uint pos, const Element_With_Context< Attic< Relation_Skeleton > >& data, const std::string* key) const
4010 { return roles ? roles->find(data.object->members[pos].role)->second : std::string(); }
403private:
404 const std::map< uint32, std::string >* roles;
405};
408class Evaluator_Role : public Evaluator
410public:
411 struct Statement_Maker : public Generic_Statement_Maker< Evaluator_Role >
412 {
4132448612 q Statement_Maker() : Generic_Statement_Maker< Evaluator_Role >("eval-role") {}
414 };
415 static Statement_Maker statement_maker;
416 static Member_Function_Maker< Evaluator_Role > evaluator_maker;
4182448612 q static std::string stmt_func_name() { return "role"; }
419 virtual std::string dump_xml(const std::string& indent) const
42033 q { return indent + "<eval-role/>\n"; }
42163 q virtual std::string dump_compact_ql(const std::string&) const { return "role()"; }
423 Evaluator_Role(int line_number_, const std::map< std::string, std::string >& input_attributes,
424 Parsed_Query& global_settings);
425123 q virtual std::string get_name() const { return "eval-role"; }
4260 virtual std::string get_result_name() const { return ""; }
4270 virtual void execute(Resource_Manager& rman) {}
4280 virtual ~Evaluator_Role() {}
430 virtual Requested_Context request_context() const
43133 q { return Requested_Context().add_usage(Set_Usage::SKELETON).add_role_names(); }
433123 q virtual Statement::Eval_Return_Type return_type() const { return Statement::string; }
434 virtual Eval_Task* get_string_task(Prepare_Task_Context& context, const std::string* key)
43533 q { return new Role_Eval_Task(context.get_roles()); }
436};
439/* ==== Angle of a Way at the Position of a Member ====
441This function returns the angle between the segment ending at this member and the segment starting there.
442It is so far only defined for ways.
443If the way is a closed way then for the last member the first segment is used as the starting segment.
444This function is intended to be used within the per_vertex() enumerator.
446The syntax is
448 role()
449*/
451struct Angle_Eval_Task : public Eval_Task
45387 q Angle_Eval_Task() : cache_way_ref(0u), cache_geom_ref(0) {}
4550 virtual std::string eval(const std::string* key) const { return ""; }
457 virtual std::string eval(uint pos, const Element_With_Context< Way_Skeleton >& data, const std::string* key) const;
458 virtual std::string eval(uint pos, const Element_With_Context< Attic< Way_Skeleton > >& data, const std::string* key) const;
460private:
461 mutable Way_Skeleton::Id_Type cache_way_ref;
462 mutable const Opaque_Geometry* cache_geom_ref;
463 mutable std::vector< Cartesian > cached;
465 std::string prettyprinted_angle(uint pos) const;
466 void keep_cartesians_up_to_date(Way_Skeleton::Id_Type, const Opaque_Geometry*) const;
467};
470class Evaluator_Angle : public Evaluator
472public:
473 struct Statement_Maker : public Generic_Statement_Maker< Evaluator_Angle >
474 {
4752448612 q Statement_Maker() : Generic_Statement_Maker< Evaluator_Angle >("eval-angle") {}
476 };
477 static Statement_Maker statement_maker;
478 static Member_Function_Maker< Evaluator_Angle > evaluator_maker;
4802448612 q static std::string stmt_func_name() { return "angle"; }
481 virtual std::string dump_xml(const std::string& indent) const
48277 q { return indent + "<eval-angle/>\n"; }
483147 q virtual std::string dump_compact_ql(const std::string&) const { return "angle()"; }
485 Evaluator_Angle(int line_number_, const std::map< std::string, std::string >& input_attributes,
486 Parsed_Query& global_settings);
487287 q virtual std::string get_name() const { return "eval-angle"; }
4880 virtual std::string get_result_name() const { return ""; }
4890 virtual void execute(Resource_Manager& rman) {}
4900 virtual ~Evaluator_Angle() {}
492 virtual Requested_Context request_context() const
49387 q { return Requested_Context().add_usage(Set_Usage::GEOMETRY); }
495287 q virtual Statement::Eval_Return_Type return_type() const { return Statement::string; }
496 virtual Eval_Task* get_string_task(Prepare_Task_Context& context, const std::string* key)
49787 q { return new Angle_Eval_Task(); }
498};
501#endif