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
|
#include "generator/locality_sorter.hpp"
#include "generator/geometry_holder.hpp"
#include "generator/utils.hpp"
#include "indexer/data_header.hpp"
#include "indexer/scales.hpp"
#include "indexer/scales_patch.hpp"
#include "coding/file_container.hpp"
#include "coding/file_name_utils.hpp"
#include "coding/internal/file_data.hpp"
#include "geometry/convex_hull.hpp"
#include "platform/platform.hpp"
#include "base/assert.hpp"
#include "base/logging.hpp"
#include "base/scope_guard.hpp"
#include "base/string_utils.hpp"
#include "base/timer.hpp"
#include "defines.hpp"
#include <cstdint>
#include <fstream>
#include <functional>
#include <limits>
#include <set>
#include <vector>
using namespace feature;
using namespace std;
namespace
{
class BordersCollector : public FeaturesCollector
{
public:
BordersCollector(string const & filename)
: FeaturesCollector(filename + EXTENSION_TMP), m_writer(filename, FileWriter::OP_WRITE_EXISTING)
{
}
// FeaturesCollector overrides:
uint32_t operator()(FeatureBuilder1 & fb) override
{
if (fb.IsArea())
{
FeatureBuilder1::Buffer buffer;
fb.SerializeBorder(serial::GeometryCodingParams(), buffer);
WriteFeatureBase(buffer, fb);
}
return 0;
}
void Finish() override
{
Flush();
m_writer.Write(m_datFile.GetName(), BORDERS_FILE_TAG);
m_writer.Finish();
}
private:
FilesContainerW m_writer;
DataHeader m_header;
DISALLOW_COPY_AND_MOVE(BordersCollector);
};
class LocalityCollector : public FeaturesCollector
{
public:
LocalityCollector(string const & filename, DataHeader const & header, uint32_t versionDate)
: FeaturesCollector(filename + EXTENSION_TMP)
, m_writer(filename)
, m_header(header)
, m_versionDate(versionDate)
{
}
// FeaturesCollector overrides:
void Finish() override
{
{
FileWriter w = m_writer.GetWriter(VERSION_FILE_TAG);
version::WriteVersion(w, m_versionDate);
}
m_header.SetBounds(m_bounds);
{
FileWriter w = m_writer.GetWriter(HEADER_FILE_TAG);
m_header.Save(w);
}
Flush();
m_writer.Write(m_datFile.GetName(), LOCALITY_DATA_FILE_TAG);
m_writer.Finish();
}
uint32_t operator()(FeatureBuilder1 & fb1) override
{
auto & fb2 = static_cast<FeatureBuilder2 &>(fb1);
// Do not limit inner triangles number to save all geometry without additional sections.
GeometryHolder holder(fb2, m_header, numeric_limits<uint32_t>::max() /* maxTrianglesNumber */);
// Simplify and serialize geometry.
vector<m2::PointD> points;
m2::SquaredDistanceFromSegmentToPoint<m2::PointD> distFn;
SimplifyPoints(distFn, scales::GetUpperScale(), holder.GetSourcePoints(), points);
// For areas we save outer geometry only.
if (fb2.IsArea() && holder.NeedProcessTriangles())
{
// At this point we don't need last point equal to first.
points.pop_back();
auto const & polys = fb2.GetGeometry();
if (polys.size() != 1)
{
points.clear();
for (auto const & poly : polys)
points.insert(points.end(), poly.begin(), poly.end());
}
if (points.size() > 2)
{
if (!holder.TryToMakeStrip(points))
{
m2::ConvexHull hull(points, 1e-16);
vector<m2::PointD> hullPoints = hull.Points();
holder.SetInner();
auto const id = fb2.GetMostGenericOsmId();
CHECK(holder.TryToMakeStrip(hullPoints),
("Error while building tringles for object with OSM Id:", id.GetSerialId(),
"Type:", id.GetType(), "points:", points, "hull:", hull.Points()));
}
}
}
auto & buffer = holder.GetBuffer();
if (fb2.PreSerializeAndRemoveUselessNames(buffer))
{
fb2.SerializeLocalityObject(serial::GeometryCodingParams(), buffer);
WriteFeatureBase(buffer.m_buffer, fb2);
}
return 0;
}
private:
FilesContainerW m_writer;
DataHeader m_header;
uint32_t m_versionDate;
DISALLOW_COPY_AND_MOVE(LocalityCollector);
};
bool ParseNodes(string nodesFile, set<uint64_t> & nodeIds)
{
if (nodesFile.empty())
return true;
ifstream stream(nodesFile);
if (!stream)
{
LOG(LERROR, ("Could not open", nodesFile));
return false;
}
string line;
size_t lineNumber = 1;
while (getline(stream, line))
{
strings::SimpleTokenizer iter(line, " ");
uint64_t nodeId;
if (!iter || !strings::to_uint64(*iter, nodeId))
{
LOG(LERROR, ("Error while parsing node id at line", lineNumber, "Line contents:", line));
return false;
}
nodeIds.insert(nodeId);
++lineNumber;
}
return true;
}
using NeedSerialize = function<bool(FeatureBuilder1 & fb1)>;
bool GenerateLocalityDataImpl(FeaturesCollector & collector, NeedSerialize const & needSerialize,
string const & featuresFile, string const & dataFile)
{
// Transform features from raw format to LocalityObject format.
try
{
LOG(LINFO, ("Processing", featuresFile));
CalculateMidPoints midPoints;
ForEachFromDatRawFormat(featuresFile, midPoints);
// Sort features by their middle point.
midPoints.Sort();
FileReader reader(featuresFile);
for (auto const & point : midPoints.GetVector())
{
ReaderSource<FileReader> src(reader);
src.Skip(point.second);
FeatureBuilder1 f;
ReadFromSourceRawFormat(src, f);
// Emit object.
if (needSerialize(f))
collector(f);
}
collector.Finish();
}
catch (RootException const & ex)
{
LOG(LCRITICAL, ("Locality data writing error:", ex.Msg()));
return false;
}
return true;
}
} // namespace
namespace feature
{
bool GenerateGeoObjectsData(string const & featuresFile, string const & nodesFile,
string const & dataFile)
{
set<uint64_t> nodeIds;
if (!ParseNodes(nodesFile, nodeIds))
return false;
auto const needSerialize = [&nodeIds](FeatureBuilder1 & fb) {
auto & fb2 = static_cast<FeatureBuilder2 &>(fb);
return fb2.IsPoint() || fb2.IsArea() ||
(!fb.GetOsmIds().empty() && nodeIds.count(fb.GetMostGenericOsmId().GetEncodedId()) != 0);
};
DataHeader header;
header.SetGeometryCodingParams(serial::GeometryCodingParams());
header.SetScales({scales::GetUpperScale()});
LocalityCollector localityCollector(dataFile, header,
static_cast<uint32_t>(base::SecondsSinceEpoch()));
return GenerateLocalityDataImpl(localityCollector, needSerialize, featuresFile, dataFile);
}
bool GenerateRegionsData(string const & featuresFile, string const & dataFile)
{
DataHeader header;
header.SetGeometryCodingParams(serial::GeometryCodingParams());
header.SetScales({scales::GetUpperScale()});
LocalityCollector regionsCollector(dataFile, header,
static_cast<uint32_t>(base::SecondsSinceEpoch()));
auto const needSerialize = [](FeatureBuilder1 const & fb) { return fb.IsArea(); };
return GenerateLocalityDataImpl(regionsCollector, needSerialize, featuresFile, dataFile);
}
bool GenerateBorders(string const & featuresFile, string const & dataFile)
{
BordersCollector bordersCollector(dataFile);
auto const needSerialize = [](FeatureBuilder1 const & fb) { return fb.IsArea(); };
return GenerateLocalityDataImpl(bordersCollector, needSerialize, featuresFile, dataFile);
}
} // namespace feature
|