Welcome to mirror list, hosted at ThFree Co, Russian Federation.

openlr_model_xml.cpp « openlr - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9570becffb386cc963eba836b6195984eeecd837 (plain)
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
#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 "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;
}

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();
}

bool NoLocationReferenceButCoordinates(pugi::xml_node const & node)
{
  if (node.select_node(".//olr:locationReference").node())
    return false;
  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.lat = ((lat - my::Sign(lat) * 0.5) * 360) / (1 << 24);
  latLon.lon = ((lon - my::Sign(lon) * 0.5) * 360) / (1 << 24);

  return true;
}

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.lat = prevCoord.lat + static_cast<double>(lat) / kOpenlrDeltaFactor;
  latLon.lon = prevCoord.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 loaction 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;
}
}  // namespace

namespace openlr
{
bool ParseOpenlr(pugi::xml_document const & document, vector<LinearSegment> & segments)
{
  for (auto const segmentXpathNode : document.select_nodes("//reportSegments"))
  {
    LinearSegment segment;
    if (NoLocationReferenceButCoordinates(segmentXpathNode.node()))
    {
      LOG(LWARNING, ("A segment with <coordinates> instead of <optionLinearLocationReference> "
                     "encounted, skipping..."));
      continue;
    }

    if (!SegmentFromXML(segmentXpathNode.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;
  }

  auto const locRefNode = GetLinearLocationReference(segmentNode);
  return LinearLocationReferenceFromXML(locRefNode, segment.m_locationReference);
}
}  // namespace openlr