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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
#include "generator/road_access_generator.hpp"
#include "generator/routing_helpers.hpp"
#include "routing/road_access.hpp"
#include "routing/road_access_serialization.hpp"
#include "indexer/classificator.hpp"
#include "indexer/feature.hpp"
#include "indexer/feature_data.hpp"
#include "indexer/features_vector.hpp"
#include "coding/file_container.hpp"
#include "coding/file_writer.hpp"
#include "base/assert.hpp"
#include "base/geo_object_id.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"
#include <initializer_list>
#include "defines.hpp"
#include <algorithm>
#include <unordered_map>
#include <utility>
#include "boost/optional.hpp"
using namespace feature;
using namespace routing;
using namespace std;
namespace
{
char constexpr kDelim[] = " \t\r\n";
using TagMapping = routing::RoadAccessTagProcessor::TagMapping;
TagMapping const kMotorCarTagMapping = {
{OsmElement::Tag("motorcar", "yes"), RoadAccess::Type::Yes},
{OsmElement::Tag("motorcar", "designated"), RoadAccess::Type::Yes},
{OsmElement::Tag("motorcar", "permissive"), RoadAccess::Type::Yes},
{OsmElement::Tag("motorcar", "no"), RoadAccess::Type::No},
{OsmElement::Tag("motorcar", "private"), RoadAccess::Type::Private},
{OsmElement::Tag("motorcar", "destination"), RoadAccess::Type::Destination},
};
TagMapping const kMotorVehicleTagMapping = {
{OsmElement::Tag("motor_vehicle", "yes"), RoadAccess::Type::Yes},
{OsmElement::Tag("motor_vehicle", "designated"), RoadAccess::Type::Yes},
{OsmElement::Tag("motor_vehicle", "permissive"), RoadAccess::Type::Yes},
{OsmElement::Tag("motor_vehicle", "no"), RoadAccess::Type::No},
{OsmElement::Tag("motor_vehicle", "private"), RoadAccess::Type::Private},
{OsmElement::Tag("motor_vehicle", "destination"), RoadAccess::Type::Destination},
};
TagMapping const kVehicleTagMapping = {
{OsmElement::Tag("vehicle", "yes"), RoadAccess::Type::Yes},
{OsmElement::Tag("vehicle", "designated"), RoadAccess::Type::Yes},
{OsmElement::Tag("vehicle", "permissive"), RoadAccess::Type::Yes},
{OsmElement::Tag("vehicle", "no"), RoadAccess::Type::No},
{OsmElement::Tag("vehicle", "private"), RoadAccess::Type::Private},
{OsmElement::Tag("vehicle", "destination"), RoadAccess::Type::Destination},
};
TagMapping const kCarBarriersTagMapping = {
{OsmElement::Tag("barrier", "block"), RoadAccess::Type::No},
{OsmElement::Tag("barrier", "bollard"), RoadAccess::Type::No},
{OsmElement::Tag("barrier", "cycle_barrier"), RoadAccess::Type::No},
{OsmElement::Tag("barrier", "gate"), RoadAccess::Type::Private},
{OsmElement::Tag("barrier", "lift_gate"), RoadAccess::Type::Private},
// TODO (@gmoryes) The types below should be added.
// {OsmElement::Tag("barrier", "chain"), RoadAccess::Type::No},
// {OsmElement::Tag("barrier", "swing_gate"), RoadAccess::Type::Private}
// {OsmElement::Tag("barrier", "log"), RoadAccess::Type::No},
// {OsmElement::Tag("barrier", "motorcycle_barrier"), RoadAccess::Type::No},
};
TagMapping const kPedestrianTagMapping = {
{OsmElement::Tag("foot", "yes"), RoadAccess::Type::Yes},
{OsmElement::Tag("foot", "designated"), RoadAccess::Type::Yes},
{OsmElement::Tag("foot", "permissive"), RoadAccess::Type::Yes},
{OsmElement::Tag("foot", "no"), RoadAccess::Type::No},
{OsmElement::Tag("foot", "private"), RoadAccess::Type::Private},
{OsmElement::Tag("foot", "destination"), RoadAccess::Type::Destination},
};
TagMapping const kBicycleTagMapping = {
{OsmElement::Tag("bicycle", "yes"), RoadAccess::Type::Yes},
{OsmElement::Tag("bicycle", "designated"), RoadAccess::Type::Yes},
{OsmElement::Tag("bicycle", "permissive"), RoadAccess::Type::Yes},
{OsmElement::Tag("bicycle", "no"), RoadAccess::Type::No},
{OsmElement::Tag("bicycle", "private"), RoadAccess::Type::Private},
{OsmElement::Tag("bicycle", "destination"), RoadAccess::Type::Destination},
};
TagMapping const kBicycleBarriersTagMapping = {
{OsmElement::Tag("barrier", "cycle_barrier"), RoadAccess::Type::No},
{OsmElement::Tag("barrier", "gate"), RoadAccess::Type::Private},
// TODO (@gmoryes) The types below should be added.
// {OsmElement::Tag("barrier", "kissing_gate"), RoadAccess::Type::Private},
};
// Allow everything to keep transit section empty. We'll use pedestrian section for
// transit + pedestrian combination.
// Empty mapping leads to default RoadAccess::Type::Yes access type for all roads.
TagMapping const kTransitTagMapping = {};
TagMapping const kDefaultTagMapping = {
{OsmElement::Tag("access", "yes"), RoadAccess::Type::Yes},
{OsmElement::Tag("access", "permissive"), RoadAccess::Type::Yes},
{OsmElement::Tag("access", "no"), RoadAccess::Type::No},
{OsmElement::Tag("access", "private"), RoadAccess::Type::Private},
{OsmElement::Tag("access", "destination"), RoadAccess::Type::Destination},
};
set<OsmElement::Tag> const kHighwaysWhereIgnorePrivateAccessForCar = {
{OsmElement::Tag("highway", "motorway")},
{OsmElement::Tag("highway", "motorway_link")},
{OsmElement::Tag("highway", "primary")},
{OsmElement::Tag("highway", "primary_link")},
{OsmElement::Tag("highway", "secondary")},
{OsmElement::Tag("highway", "secondary_link")},
{OsmElement::Tag("highway", "tertiary")},
{OsmElement::Tag("highway", "tertiary_link")},
{OsmElement::Tag("highway", "trunk")},
{OsmElement::Tag("highway", "trunk_link")}
};
set<OsmElement::Tag> const kHighwaysWhereIgnorePrivateAccessEmpty = {};
bool ParseRoadAccess(string const & roadAccessPath,
map<base::GeoObjectId, uint32_t> const & osmIdToFeatureId,
FeaturesVector const & featuresVector,
RoadAccessCollector::RoadAccessByVehicleType & roadAccessByVehicleType)
{
ifstream stream(roadAccessPath);
if (!stream)
{
LOG(LWARNING, ("Could not open", roadAccessPath));
return false;
}
unordered_map<uint32_t, RoadAccess::Type> featureType[static_cast<size_t>(VehicleType::Count)];
unordered_map<RoadPoint, RoadAccess::Type, RoadPoint::Hash> pointType[static_cast<size_t>(VehicleType::Count)];
auto addFeature = [&](uint32_t featureId, VehicleType vehicleType,
RoadAccess::Type roadAccessType, uint64_t osmId) {
auto & m = featureType[static_cast<size_t>(vehicleType)];
auto const emplaceRes = m.emplace(featureId, roadAccessType);
if (!emplaceRes.second && emplaceRes.first->second != roadAccessType)
{
LOG(LDEBUG, ("Duplicate road access info for OSM way", osmId, "vehicle:", vehicleType,
"access is:", emplaceRes.first->second, "tried:", roadAccessType));
}
};
auto addPoint = [&](RoadPoint const & point, VehicleType vehicleType,
RoadAccess::Type roadAccessType) {
auto & m = pointType[static_cast<size_t>(vehicleType)];
auto const emplaceRes = m.emplace(point, roadAccessType);
if (!emplaceRes.second && emplaceRes.first->second != roadAccessType)
{
LOG(LDEBUG, ("Duplicate road access info for road point", point, "vehicle:", vehicleType,
"access is:", emplaceRes.first->second, "tried:", roadAccessType));
}
};
string line;
for (uint32_t lineNo = 1;; ++lineNo)
{
if (!getline(stream, line))
break;
strings::SimpleTokenizer iter(line, kDelim);
if (!iter)
{
LOG(LERROR, ("Error when parsing road access: empty line", lineNo));
return false;
}
VehicleType vehicleType;
FromString(*iter, vehicleType);
++iter;
if (!iter)
{
LOG(LERROR, ("Error when parsing road access: no road access type at line", lineNo, "Line contents:", line));
return false;
}
RoadAccess::Type roadAccessType;
FromString(*iter, roadAccessType);
++iter;
uint64_t osmId;
if (!iter || !strings::to_uint64(*iter, osmId))
{
LOG(LERROR, ("Error when parsing road access: bad osm id at line", lineNo, "Line contents:", line));
return false;
}
++iter;
uint32_t pointIdx;
if (!iter || !strings::to_uint(*iter, pointIdx))
{
LOG(LERROR, ("Error when parsing road access: bad pointIdx at line", lineNo, "Line contents:", line));
return false;
}
++iter;
auto const it = osmIdToFeatureId.find(base::MakeOsmWay(osmId));
// Even though this osm element has a tag that is interesting for us,
// we have not created a feature from it. Possible reasons:
// no primary tag, unsupported type, etc.
if (it == osmIdToFeatureId.cend())
continue;
uint32_t const featureId = it->second;
if (pointIdx == 0)
addFeature(featureId, vehicleType, roadAccessType, osmId);
else
addPoint(RoadPoint(featureId, pointIdx - 1), vehicleType, roadAccessType);
}
for (size_t i = 0; i < static_cast<size_t>(VehicleType::Count); ++i)
roadAccessByVehicleType[i].SetAccessTypes(move(featureType[i]), move(pointType[i]));
return true;
}
// If |elem| has access tag from |mapping|, returns corresponding RoadAccess::Type.
// Tags in |mapping| should be mutually exclusive. Caller is responsible for that. If there are
// multiple access tags from |mapping| in |elem|, returns RoadAccess::Type for any of them.
// Returns RoadAccess::Type::Count if |elem| has no access tags from |mapping|.
RoadAccess::Type GetAccessTypeFromMapping(OsmElement const & elem, TagMapping const * mapping)
{
for (auto const & tag : elem.m_tags)
{
auto const it = mapping->find(tag);
if (it != mapping->cend())
return it->second;
}
return RoadAccess::Type::Count;
}
} // namespace
namespace routing
{
// RoadAccessTagProcessor --------------------------------------------------------------------------
RoadAccessTagProcessor::RoadAccessTagProcessor(VehicleType vehicleType)
: m_vehicleType(vehicleType)
{
switch (vehicleType)
{
case VehicleType::Car:
m_accessMappings.push_back(&kMotorCarTagMapping);
m_accessMappings.push_back(&kMotorVehicleTagMapping);
m_accessMappings.push_back(&kVehicleTagMapping);
m_accessMappings.push_back(&kDefaultTagMapping);
m_barrierMappings.push_back(&kCarBarriersTagMapping);
m_hwIgnoreBarriersWithoutAccess = &kHighwaysWhereIgnorePrivateAccessForCar;
break;
case VehicleType::Pedestrian:
m_accessMappings.push_back(&kPedestrianTagMapping);
m_accessMappings.push_back(&kDefaultTagMapping);
m_hwIgnoreBarriersWithoutAccess = &kHighwaysWhereIgnorePrivateAccessEmpty;
break;
case VehicleType::Bicycle:
m_accessMappings.push_back(&kBicycleTagMapping);
m_accessMappings.push_back(&kVehicleTagMapping);
m_accessMappings.push_back(&kDefaultTagMapping);
m_barrierMappings.push_back(&kBicycleBarriersTagMapping);
m_hwIgnoreBarriersWithoutAccess = &kHighwaysWhereIgnorePrivateAccessEmpty;
break;
case VehicleType::Transit:
// Use kTransitTagMapping to keep transit section empty. We'll use pedestrian section for
// transit + pedestrian combination.
m_accessMappings.push_back(&kTransitTagMapping);
m_hwIgnoreBarriersWithoutAccess = &kHighwaysWhereIgnorePrivateAccessEmpty;
break;
case VehicleType::Count:
CHECK(false, ("Bad vehicle type"));
break;
}
}
void RoadAccessTagProcessor::Process(OsmElement const & elem, ofstream & oss)
{
// We will process all nodes before ways because of o5m format:
// all nodes are first, then all ways, then all relations.
if (elem.m_type == OsmElement::EntityType::Node)
{
RoadAccess::Type accessType = GetAccessType(elem);
if (accessType != RoadAccess::Type::Yes)
m_barriers[elem.m_id] = accessType;
return;
}
if (elem.m_type != OsmElement::EntityType::Way)
return;
// All feature tags.
auto const accessType = GetAccessType(elem);
if (accessType != RoadAccess::Type::Yes)
oss << ToString(m_vehicleType) << " " << ToString(accessType) << " " << elem.m_id << " "
<< 0 /* wildcard segment Idx */ << endl;
// Barrier tags.
for (size_t pointIdx = 0; pointIdx < elem.m_nodes.size(); ++pointIdx)
{
auto const it = m_barriers.find(elem.m_nodes[pointIdx]);
if (it == m_barriers.cend())
continue;
RoadAccess::Type const roadAccessType = it->second;
// idx == 0 used as wildcard segment Idx, for nodes we store |pointIdx + 1| instead of |pointIdx|.
oss << ToString(m_vehicleType) << " " << ToString(roadAccessType) << " " << elem.m_id << " "
<< pointIdx + 1 << endl;
}
}
bool RoadAccessTagProcessor::ShouldIgnoreBarrierWithoutAccess(OsmElement const & osmElement) const
{
CHECK(m_hwIgnoreBarriersWithoutAccess, ());
for (auto const & tag : osmElement.m_tags)
{
if (m_hwIgnoreBarriersWithoutAccess->count(tag) != 0)
return true;
}
return false;
}
RoadAccess::Type RoadAccessTagProcessor::GetAccessType(OsmElement const & elem) const
{
auto const getType = [&](vector<TagMapping const *> const & mapping)
{
for (auto const tagMapping : mapping)
{
auto const accessType = GetAccessTypeFromMapping(elem, tagMapping);
if (accessType != RoadAccess::Type::Count)
return boost::optional<RoadAccess::Type>(accessType);
}
return boost::optional<RoadAccess::Type>();
};
if (auto op = getType(m_accessMappings))
return *op;
// Apply barrier tags if we have no {vehicle = ...}, {access = ...} etc.
if (auto op = getType(m_barrierMappings))
{
if (!ShouldIgnoreBarrierWithoutAccess(elem))
return *op;
}
return RoadAccess::Type::Yes;
}
// RoadAccessWriter ------------------------------------------------------------
RoadAccessWriter::RoadAccessWriter(string const & filePath)
{
for (size_t i = 0; i < static_cast<size_t>(VehicleType::Count); ++i)
m_tagProcessors.emplace_back(static_cast<VehicleType>(i));
Open(filePath);
}
void RoadAccessWriter::Open(string const & filePath)
{
LOG(LINFO,
("Saving information about barriers and road access classes in osm id terms to", filePath));
m_stream.open(filePath, ofstream::out);
if (!IsOpened())
LOG(LINFO, ("Cannot open file", filePath));
}
void RoadAccessWriter::CollectFeature(FeatureBuilder const &, OsmElement const & elem)
{
if (!IsOpened())
{
LOG(LWARNING, ("Tried to write to a closed barriers writer"));
return;
}
for (auto & p : m_tagProcessors)
p.Process(elem, m_stream);
}
bool RoadAccessWriter::IsOpened() const { return m_stream && m_stream.is_open(); }
// RoadAccessCollector ----------------------------------------------------------
RoadAccessCollector::RoadAccessCollector(string const & dataFilePath, string const & roadAccessPath,
string const & osmIdsToFeatureIdsPath)
{
map<base::GeoObjectId, uint32_t> osmIdToFeatureId;
if (!ParseRoadsOsmIdToFeatureIdMapping(osmIdsToFeatureIdsPath, osmIdToFeatureId))
{
LOG(LWARNING, ("An error happened while parsing feature id to osm ids mapping from file:",
osmIdsToFeatureIdsPath));
m_valid = false;
return;
}
FeaturesVectorTest featuresVector(dataFilePath);
RoadAccessCollector::RoadAccessByVehicleType roadAccessByVehicleType;
if (!ParseRoadAccess(roadAccessPath, osmIdToFeatureId, featuresVector.GetVector(),
roadAccessByVehicleType))
{
LOG(LWARNING, ("An error happened while parsing road access from file:", roadAccessPath));
m_valid = false;
return;
}
m_valid = true;
m_roadAccessByVehicleType.swap(roadAccessByVehicleType);
}
// Functions ------------------------------------------------------------------
void BuildRoadAccessInfo(string const & dataFilePath, string const & roadAccessPath,
string const & osmIdsToFeatureIdsPath)
{
LOG(LINFO, ("Generating road access info for", dataFilePath));
RoadAccessCollector collector(dataFilePath, roadAccessPath, osmIdsToFeatureIdsPath);
if (!collector.IsValid())
{
LOG(LWARNING, ("Unable to parse road access in osm terms"));
return;
}
FilesContainerW cont(dataFilePath, FileWriter::OP_WRITE_EXISTING);
FileWriter writer = cont.GetWriter(ROAD_ACCESS_FILE_TAG);
RoadAccessSerializer::Serialize(writer, collector.GetRoadAccessAllTypes());
}
} // namespace routing
|