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
|
#include "openlr/openlr_model_xml.hpp"
#include "openlr/openlr_model.hpp"
#include "geometry/mercator.hpp"
#include "base/logging.hpp"
#include "std/cstring.hpp"
#include "std/type_traits.hpp"
#include "boost/optional.hpp"
#include "3party/pugixml/src/pugixml.hpp"
namespace // Primitive utilities to handle simple OpenLR-like XML data.
{
template <typename Value>
bool IntegerFromXML(pugi::xml_node const & node, Value & value)
{
if (!node)
return false;
value = static_cast<Value>(is_signed<Value>::value
? node.text().as_int()
: node.text().as_uint());
return true;
}
boost::optional<double> DoubleFromXML(pugi::xml_node const & node)
{
return node ? node.text().as_double() : boost::optional<double>();
}
bool GetLatLon(pugi::xml_node const & node, int32_t & lat, int32_t & lon)
{
if (!IntegerFromXML(node.child("olr:latitude"), lat) ||
!IntegerFromXML(node.child("olr:longitude"), lon))
{
return false;
}
return true;
}
// This helper is used to parse records like this:
// <olr:lfrcnp olr:table="olr001_FunctionalRoadClass" olr:code="4"/>
template <typename Value>
bool TableValueFromXML(pugi::xml_node const & node, Value & value)
{
if (!node)
return false;
value = static_cast<Value>(is_signed<Value>::value
? node.attribute("olr:code").as_int()
: node.attribute("olr:code").as_uint());
return true;
}
pugi::xml_node GetLinearLocationReference(pugi::xml_node const & node)
{
// There should be only one linear location reference child of a location reference.
return node.select_node(".//olr:locationReference/olr:optionLinearLocationReference").node();
}
pugi::xml_node GetCoordinates(pugi::xml_node const & node)
{
return node.select_node(".//coordinates").node();
}
bool IsLocationReferenceTag(pugi::xml_node const & node)
{
return node.select_node(".//olr:locationReference").node();
}
bool IsCoordinatesTag(pugi::xml_node const & node)
{
return node.select_node("coordinates").node();
}
// This helper is used do deal with xml nodes of the form
// <node>
// <value>integer<value>
// </node>
template <typename Value>
bool ValueFromXML(pugi::xml_node const & node, Value & value)
{
auto const valueNode = node.child("olr:value");
return IntegerFromXML(valueNode, value);
}
template <typename Value>
bool ParseValueIfExists(pugi::xml_node const & node, Value & value)
{
if (!node)
return true;
return ValueFromXML(node, value);
}
} // namespace
namespace // OpenLR tools and abstractions
{
bool FirstCoordinateFromXML(pugi::xml_node const & node, ms::LatLon & latLon)
{
int32_t lat, lon;
if (!GetLatLon(node.child("olr:coordinate"), lat, lon))
return false;
latLon.m_lat = ((lat - base::Sign(lat) * 0.5) * 360) / (1 << 24);
latLon.m_lon = ((lon - base::Sign(lon) * 0.5) * 360) / (1 << 24);
return true;
}
boost::optional<ms::LatLon> LatLonFormXML(pugi::xml_node const & node)
{
auto const lat = DoubleFromXML(node.child("latitude"));
auto const lon = DoubleFromXML(node.child("longitude"));
return lat && lon ? ms::LatLon(lat.get(), lon.get()) : ms::LatLon::Zero();
}
bool CoordinateFromXML(pugi::xml_node const & node, ms::LatLon const & prevCoord,
ms::LatLon & latLon)
{
int32_t lat, lon;
if (!GetLatLon(node.child("olr:coordinate"), lat, lon))
return false;
// This constant is provided by the given OpenLR variant
// with no special meaning and is used as a factor to store doubles as ints.
auto const kOpenlrDeltaFactor = 100000;
latLon.m_lat = prevCoord.m_lat + static_cast<double>(lat) / kOpenlrDeltaFactor;
latLon.m_lon = prevCoord.m_lon + static_cast<double>(lon) / kOpenlrDeltaFactor;
return true;
}
bool LinePropertiesFromXML(pugi::xml_node const & linePropNode,
openlr::LocationReferencePoint & locPoint)
{
if (!linePropNode)
{
LOG(LERROR, ("linePropNode is NULL"));
return false;
}
if (!TableValueFromXML(linePropNode.child("olr:frc"), locPoint.m_functionalRoadClass))
{
LOG(LERROR, ("Can't parse functional road class"));
return false;
}
if (!TableValueFromXML(linePropNode.child("olr:fow"), locPoint.m_formOfWay))
{
LOG(LERROR, ("Can't parse form of a way"));
return false;
}
if (!ValueFromXML(linePropNode.child("olr:bearing"), locPoint.m_bearing))
{
LOG(LERROR, ("Can't parse bearing"));
return false;
}
return true;
}
bool PathPropertiesFromXML(pugi::xml_node const & locPointNode,
openlr::LocationReferencePoint & locPoint)
{
// Last point does not contain path properties.
if (strcmp(locPointNode.name(), "olr:last") == 0)
return true;
auto const propNode = locPointNode.child("olr:pathProperties");
if (!propNode)
{
LOG(LERROR, ("Can't parse path properties"));
return false;
}
if (!ValueFromXML(propNode.child("olr:dnp"), locPoint.m_distanceToNextPoint))
{
LOG(LERROR, ("Can't parse dnp"));
return false;
}
if (!TableValueFromXML(propNode.child("olr:lfrcnp"), locPoint.m_lfrcnp))
{
LOG(LERROR, ("Can't parse lfrcnp"));
return false;
}
auto const directionNode = propNode.child("olr:againstDrivingDirection");
if (!directionNode)
{
LOG(LERROR, ("Can't parse driving direction"));
return false;
}
locPoint.m_againstDrivingDirection = directionNode.text().as_bool();
return true;
}
bool LocationReferencePointFromXML(pugi::xml_node const & locPointNode,
openlr::LocationReferencePoint & locPoint)
{
if (!FirstCoordinateFromXML(locPointNode, locPoint.m_latLon))
{
LOG(LERROR, ("Can't get first coordinate"));
return false;
}
return LinePropertiesFromXML(locPointNode.child("olr:lineProperties"), locPoint) &&
PathPropertiesFromXML(locPointNode, locPoint);
}
bool LocationReferencePointFromXML(pugi::xml_node const & locPointNode,
ms::LatLon const & firstPoint,
openlr::LocationReferencePoint & locPoint)
{
if (!CoordinateFromXML(locPointNode, firstPoint, locPoint.m_latLon))
{
LOG(LERROR, ("Can't get last coordinate"));
return false;
}
return LinePropertiesFromXML(locPointNode.child("olr:lineProperties"), locPoint) &&
PathPropertiesFromXML(locPointNode, locPoint);
}
bool LinearLocationReferenceFromXML(pugi::xml_node const & locRefNode,
openlr::LinearLocationReference & locRef)
{
if (!locRefNode)
{
LOG(LERROR, ("Can't get location reference."));
return false;
}
{
openlr::LocationReferencePoint point;
if (!LocationReferencePointFromXML(locRefNode.child("olr:first"), point))
return false;
locRef.m_points.push_back(point);
}
for (auto const pointNode : locRefNode.select_nodes("olr:intermediates"))
{
openlr::LocationReferencePoint point;
if (!LocationReferencePointFromXML(pointNode.node(), locRef.m_points.back().m_latLon, point))
return false;
locRef.m_points.push_back(point);
}
{
openlr::LocationReferencePoint point;
if (!LocationReferencePointFromXML(locRefNode.child("olr:last"),
locRef.m_points.back().m_latLon, point))
return false;
locRef.m_points.push_back(point);
}
if (!ParseValueIfExists(locRefNode.child("olr:positiveOffset"), locRef.m_positiveOffsetMeters))
{
LOG(LERROR, ("Can't parse positive offset"));
return false;
}
if(!ParseValueIfExists(locRefNode.child("olr:negativeOffset"), locRef.m_negativeOffsetMeters))
{
LOG(LERROR, ("Can't parse negative offset"));
return false;
}
return true;
}
bool CoordinatesFromXML(pugi::xml_node const & coordsNode, openlr::LinearLocationReference & locRef)
{
if (!coordsNode)
{
LOG(LERROR, ("Can't get <coordinates>."));
return false;
}
auto const latLonStart = LatLonFormXML(coordsNode.child("start"));
auto const latLonEnd = LatLonFormXML(coordsNode.child("end"));
if (!latLonStart || !latLonEnd)
{
LOG(LERROR, ("Can't get <start> or <end> of <coordinates>."));
return false;
}
LOG(LINFO, ("from:", latLonStart.get(), "to:", latLonEnd.get()));
locRef.m_points.resize(2);
locRef.m_points[0].m_latLon = latLonStart.get();
locRef.m_points[1].m_latLon = latLonEnd.get();
return true;
}
} // namespace
namespace openlr
{
bool ParseOpenlr(pugi::xml_document const & document, vector<LinearSegment> & segments)
{
for (auto const segmentXpathNode : document.select_nodes("//reportSegments"))
{
LinearSegment segment;
auto const & node = segmentXpathNode.node();
if (!IsLocationReferenceTag(node) && !IsCoordinatesTag(node))
{
LOG(LWARNING, ("A segment with a strange tag. It is not <coordinates>"
" or <optionLinearLocationReference>, skipping..."));
continue;
}
if (!SegmentFromXML(node, segment))
return false;
segments.push_back(segment);
}
return true;
}
bool SegmentFromXML(pugi::xml_node const & segmentNode, LinearSegment & segment)
{
CHECK(segmentNode, ());
if (!IntegerFromXML(segmentNode.child("ReportSegmentID"), segment.m_segmentId))
{
LOG(LERROR, ("Can't parse segment id"));
return false;
}
if (!IntegerFromXML(segmentNode.child("segmentLength"), segment.m_segmentLengthMeters))
{
LOG(LERROR, ("Can't parse segment length"));
return false;
}
if (IsLocationReferenceTag(segmentNode))
{
auto const locRefNode = GetLinearLocationReference(segmentNode);
auto const result = LinearLocationReferenceFromXML(locRefNode, segment.m_locationReference);
if (result)
segment.m_source = LinearSegmentSource::FromLocationReferenceTag;
return result;
}
CHECK(IsCoordinatesTag(segmentNode), ());
auto const coordsNode = GetCoordinates(segmentNode);
if (!CoordinatesFromXML(coordsNode, segment.m_locationReference))
return false;
CHECK_EQUAL(segment.m_locationReference.m_points.size(), 2, ());
segment.m_locationReference.m_points[0].m_distanceToNextPoint = segment.m_segmentLengthMeters;
segment.m_source = LinearSegmentSource::FromCoordinatesTag;
return true;
}
} // namespace openlr
|