-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
restriction_parser.cpp
285 lines (255 loc) · 9.21 KB
/
restriction_parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "extractor/restriction_parser.hpp"
#include "extractor/profile_properties.hpp"
#include "util/conditional_restrictions.hpp"
#include "util/integer_range.hpp"
#include "util/log.hpp"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/optional/optional.hpp>
#include <boost/ref.hpp>
#include <osmium/osm.hpp>
#include <algorithm>
namespace osrm::extractor
{
RestrictionParser::RestrictionParser(bool use_turn_restrictions_,
bool parse_conditionals_,
const std::vector<std::string> &restrictions_)
: use_turn_restrictions(use_turn_restrictions_), parse_conditionals(parse_conditionals_),
restrictions(restrictions_.begin(), restrictions_.end()), filter(false)
{
if (use_turn_restrictions)
{
const unsigned count = restrictions.size();
if (count > 0)
{
util::Log() << "Found " << count << " turn restriction tags:";
for (const std::string &str : restrictions)
{
util::Log() << " " << str;
}
}
else
{
util::Log() << "Found no turn restriction tags";
}
}
filter.add(true, "restriction");
if (parse_conditionals)
{
filter.add(true, "restriction:conditional");
for (const auto &namespaced : restrictions_)
{
filter.add(true, "restriction:" + namespaced + ":conditional");
}
}
// Not only use restriction= but also e.g. restriction:motorcar=
// Include restriction:{mode}:conditional if flagged
for (const auto &namespaced : restrictions_)
{
filter.add(true, "restriction:" + namespaced);
}
}
/**
* Tries to parse a relation as a turn restriction. This can fail for a number of
* reasons. The return type is a std::vector<T>.
*
* Some restrictions can also be ignored: See the ```get_restrictions``` function
* in the corresponding profile. We use it for both namespacing restrictions, as in
* restriction:motorcar as well as whitelisting if its in except:motorcar.
*/
std::vector<InputTurnRestriction>
RestrictionParser::TryParse(const osmium::Relation &relation) const
{
// return if turn restrictions should be ignored
if (!use_turn_restrictions)
{
return {};
}
const osmium::TagList &tag_list = relation.tags();
osmium::tags::KeyFilter::iterator fi_begin(filter, tag_list.begin(), tag_list.end());
osmium::tags::KeyFilter::iterator fi_end(filter, tag_list.end(), tag_list.end());
// if it's not a restriction, continue;
if (fi_begin == fi_end)
{
return {};
}
// check if the restriction should be ignored
const char *except = relation.get_value_by_key("except");
if (except != nullptr && ShouldIgnoreRestriction(except))
{
return {};
}
bool is_only_restriction = false;
bool is_multi_from = false;
bool is_multi_to = false;
std::vector<util::OpeningHours> condition;
for (; fi_begin != fi_end; ++fi_begin)
{
auto value = fi_begin->value();
// documented OSM restriction tags start either with only_* or no_*;
// check and return on these values, and ignore no_*_on_red or unrecognized values
if (boost::algorithm::starts_with(value, "only_"))
{
is_only_restriction = true;
}
else if (boost::algorithm::starts_with(value, "no_") &&
!boost::algorithm::ends_with(value, "_on_red"))
{
is_only_restriction = false;
if (boost::algorithm::starts_with(value, "no_exit"))
{
is_multi_to = true;
}
else if (boost::algorithm::starts_with(value, "no_entry"))
{
is_multi_from = true;
}
}
else // unrecognized value type
{
return {};
}
if (parse_conditionals)
{
// Parse condition and add independent value/condition pairs
const auto &parsed = osrm::util::ParseConditionalRestrictions(value);
if (parsed.empty())
continue;
for (const auto &p : parsed)
{
std::vector<util::OpeningHours> hours = util::ParseOpeningHours(p.condition);
// found unrecognized condition, continue
if (hours.empty())
return {};
condition = std::move(hours);
}
}
}
constexpr auto INVALID_OSM_ID = std::numeric_limits<std::uint64_t>::max();
std::vector<OSMWayID> from_ways;
auto via_node = INVALID_OSM_ID;
std::vector<OSMWayID> via_ways;
std::vector<OSMWayID> to_ways;
bool is_node_restriction = true;
for (const auto &member : relation.members())
{
const char *role = member.role();
const bool is_from_role = strcmp("from", role) == 0;
const bool is_to_role = strcmp("to", role) == 0;
const bool is_via_role = strcmp("via", role) == 0;
if (!is_from_role && !is_to_role && !is_via_role)
{
continue;
}
switch (member.type())
{
case osmium::item_type::node:
{
// Make sure nodes appear only in the role if a via node
if (is_from_role || is_to_role)
{
continue;
}
BOOST_ASSERT(is_via_role);
via_node = static_cast<std::uint64_t>(member.ref());
is_node_restriction = true;
// set via node id
break;
}
case osmium::item_type::way:
BOOST_ASSERT(is_from_role || is_to_role || is_via_role);
if (is_from_role)
{
from_ways.push_back({static_cast<std::uint64_t>(member.ref())});
}
else if (is_to_role)
{
to_ways.push_back({static_cast<std::uint64_t>(member.ref())});
}
else if (is_via_role)
{
via_ways.push_back({static_cast<std::uint64_t>(member.ref())});
is_node_restriction = false;
}
break;
case osmium::item_type::relation:
// not yet supported, but who knows what the future holds...
break;
default:
// shouldn't ever happen
break;
}
}
std::vector<InputTurnRestriction> restriction_containers;
if (!from_ways.empty() && (via_node != INVALID_OSM_ID || !via_ways.empty()) && !to_ways.empty())
{
if (from_ways.size() > 1 && !is_multi_from)
{
util::Log(logDEBUG) << "Parsed restriction " << relation.id()
<< " unexpectedly contains " << from_ways.size()
<< " from ways, skipping...";
return {};
}
if (to_ways.size() > 1 && !is_multi_to)
{
util::Log(logDEBUG) << "Parsed restriction " << relation.id()
<< " unexpectedly contains " << to_ways.size()
<< " to ways, skipping...";
return {};
}
// Internally restrictions are represented with one 'from' and one 'to' way.
// Therefore, we need to convert a multi from/to restriction into multiple restrictions.
for (const auto &from : from_ways)
{
for (const auto &to : to_ways)
{
InputTurnRestriction restriction;
restriction.is_only = is_only_restriction;
restriction.condition = condition;
if (is_node_restriction)
{
// template struct requires bracket for ID initialisation :(
restriction.turn_path.node_or_way = InputViaNodePath{{from}, {via_node}, {to}};
}
else
{
// template struct requires bracket for ID initialisation :(
restriction.turn_path.node_or_way = InputViaWayPath{{from}, via_ways, {to}};
}
restriction_containers.push_back(std::move(restriction));
}
}
}
return restriction_containers;
}
bool RestrictionParser::ShouldIgnoreRestriction(const std::string &except_tag_string) const
{
// should this restriction be ignored? yes if there's an overlap between:
// a) the list of modes in the except tag of the restriction
// (except_tag_string), eg: except=bus;bicycle
// b) the lua profile defines a hierarchy of modes,
// eg: [access, vehicle, bicycle]
if (except_tag_string.empty())
{
return false;
}
// split `except_tag_string` by semicolon and check if any of items is in `restrictions`
std::string current_string;
for (auto index : util::irange<size_t>(0, except_tag_string.size()))
{
const auto ch = except_tag_string[index];
if (ch != ';')
{
current_string += ch;
}
else
{
if (restrictions.find(current_string) != restrictions.end())
{
return true;
}
current_string.clear();
}
}
return restrictions.find(current_string) != restrictions.end();
}
} // namespace osrm::extractor