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
|
#include "openlr/decoded_path.hpp"
#include "indexer/data_source.hpp"
#include "platform/country_file.hpp"
#include "geometry/latlon.hpp"
#include "geometry/mercator.hpp"
#include "base/assert.hpp"
#include "base/scope_guard.hpp"
#include "base/string_utils.hpp"
#include <sstream>
#define THROW_IF_NODE_IS_EMPTY(node, exc, msg) \
if (!node) \
MYTHROW(exc, msg)
namespace
{
uint32_t UintFromXML(pugi::xml_node const & node)
{
THROW_IF_NODE_IS_EMPTY(node, openlr::DecodedPathLoadError, ("Can't parse uint"));
return node.text().as_uint();
}
bool IsForwardFromXML(pugi::xml_node const & node)
{
THROW_IF_NODE_IS_EMPTY(node, openlr::DecodedPathLoadError, ("Can't parse IsForward"));
return node.text().as_bool();
}
void LatLonToXML(ms::LatLon const & latLon, pugi::xml_node & node)
{
auto const kDigitsAfterComma = 5;
node.append_child("lat").text() = strings::to_string_dac(latLon.m_lat, kDigitsAfterComma).data();
node.append_child("lon").text() = strings::to_string_dac(latLon.m_lon, kDigitsAfterComma).data();
}
void LatLonFromXML(pugi::xml_node const & node, ms::LatLon & latLon)
{
THROW_IF_NODE_IS_EMPTY(node, openlr::DecodedPathLoadError, ("Can't parse latLon"));
latLon.m_lat = node.child("lat").text().as_double();
latLon.m_lon = node.child("lon").text().as_double();
}
void FeatureIdFromXML(pugi::xml_node const & node, DataSource const & dataSource, FeatureID & fid)
{
THROW_IF_NODE_IS_EMPTY(node, openlr::DecodedPathLoadError, ("Can't parse CountryName"));
auto const countryName = node.child("CountryName").text().as_string();
fid.m_mwmId = dataSource.GetMwmIdByCountryFile(platform::CountryFile(countryName));
CHECK(fid.m_mwmId.IsAlive(), ("Can't get mwm id for country", countryName));
fid.m_index = node.child("Index").text().as_uint();
}
void FeatureIdToXML(FeatureID const & fid, pugi::xml_node & node)
{
node.append_child("CountryName").text() = fid.m_mwmId.GetInfo()->GetCountryName().data();
node.append_child("Index").text() = fid.m_index;
}
} // namespace
namespace openlr
{
uint32_t UintValueFromXML(pugi::xml_node const & node)
{
auto const value = node.child("olr:value");
if (!value)
return 0;
return UintFromXML(value);
}
void WriteAsMappingForSpark(std::string const & fileName, std::vector<DecodedPath> const & paths)
{
std::ofstream ofs(fileName);
if (!ofs.is_open())
MYTHROW(DecodedPathSaveError, ("Can't write to file", fileName, strerror(errno)));
WriteAsMappingForSpark(ofs, paths);
if (ofs.fail())
{
MYTHROW(DecodedPathSaveError,
("An error occured while writing file", fileName, strerror(errno)));
}
}
void WriteAsMappingForSpark(std::ostream & ost, std::vector<DecodedPath> const & paths)
{
auto const flags = ost.flags();
ost << std::fixed; // Avoid scientific notation cause '-' is used as fields separator.
SCOPE_GUARD(guard, ([&ost, &flags] { ost.flags(flags); }));
for (auto const & p : paths)
{
if (p.m_path.empty())
continue;
ost << p.m_segmentId.Get() << '\t';
auto const kFieldSep = '-';
auto const kSegmentSep = '=';
for (auto it = begin(p.m_path); it != end(p.m_path); ++it)
{
auto const & fid = it->GetFeatureId();
ost << fid.m_mwmId.GetInfo()->GetCountryName()
<< kFieldSep << fid.m_index
<< kFieldSep << it->GetSegId()
<< kFieldSep << (it->IsForward() ? "fwd" : "bwd")
<< kFieldSep << MercatorBounds::DistanceOnEarth(GetStart(*it), GetEnd(*it));
if (next(it) != end(p.m_path))
ost << kSegmentSep;
}
ost << std::endl;
}
}
void PathFromXML(pugi::xml_node const & node, DataSource const & dataSource, Path & p)
{
auto const edges = node.select_nodes("RoadEdge");
for (auto const xmlE : edges)
{
auto e = xmlE.node();
FeatureID fid;
FeatureIdFromXML(e.child("FeatureID"), dataSource, fid);
auto const isForward = IsForwardFromXML(e.child("IsForward"));
auto const segmentId = UintFromXML(e.child("SegmentId"));
ms::LatLon start, end;
LatLonFromXML(e.child("StartJunction"), start);
LatLonFromXML(e.child("EndJunction"), end);
p.push_back(Edge::MakeReal(
fid, isForward, segmentId,
routing::Junction(MercatorBounds::FromLatLon(start), feature::kDefaultAltitudeMeters),
routing::Junction(MercatorBounds::FromLatLon(end), feature::kDefaultAltitudeMeters)));
}
}
void PathToXML(Path const & path, pugi::xml_node & node)
{
for (auto const e : path)
{
auto edge = node.append_child("RoadEdge");
{
auto fid = edge.append_child("FeatureID");
FeatureIdToXML(e.GetFeatureId(), fid);
}
edge.append_child("IsForward").text() = e.IsForward();
edge.append_child("SegmentId").text() = e.GetSegId();
{
auto start = edge.append_child("StartJunction");
auto end = edge.append_child("EndJunction");
LatLonToXML(MercatorBounds::ToLatLon(GetStart(e)), start);
LatLonToXML(MercatorBounds::ToLatLon(GetEnd(e)), end);
}
}
}
} // namespace openlr
namespace routing
{
std::vector<m2::PointD> GetPoints(openlr::Path const & p)
{
CHECK(!p.empty(), ("Path should not be empty"));
std::vector<m2::PointD> points;
points.push_back(GetStart(p.front()));
for (auto const & e : p)
points.push_back(GetEnd(e));
return points;
}
} // namespace routing
|