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
|
#include "generator/feature_sorter.hpp"
#include "generator/borders.hpp"
#include "generator/feature_builder.hpp"
#include "generator/feature_generator.hpp"
#include "generator/gen_mwm_info.hpp"
#include "generator/geometry_holder.hpp"
#include "generator/region_meta.hpp"
#include "generator/tesselator.hpp"
#include "routing/routing_helpers.hpp"
#include "routing/speed_camera_prohibition.hpp"
#include "indexer/classificator.hpp"
#include "indexer/data_header.hpp"
#include "indexer/feature_algo.hpp"
#include "indexer/feature_impl.hpp"
#include "indexer/feature_processor.hpp"
#include "indexer/feature_visibility.hpp"
#include "indexer/scales.hpp"
#include "indexer/scales_patch.hpp"
#include "platform/country_file.hpp"
#include "platform/mwm_version.hpp"
#include "coding/file_container.hpp"
#include "coding/internal/file_data.hpp"
#include "coding/point_coding.hpp"
#include "geometry/polygon.hpp"
#include "base/assert.hpp"
#include "base/file_name_utils.hpp"
#include "base/logging.hpp"
#include "base/scope_guard.hpp"
#include "base/string_utils.hpp"
#include "defines.hpp"
#include <list>
#include <limits>
#include <memory>
#include <vector>
using namespace std;
namespace feature
{
class FeaturesCollector2 : public FeaturesCollector
{
public:
FeaturesCollector2(string const & fName, DataHeader const & header, RegionData const & regionData,
uint32_t versionDate)
: FeaturesCollector(fName + DATA_FILE_TAG)
, m_writer(fName)
, m_header(header)
, m_regionData(regionData)
, m_versionDate(versionDate)
{
for (size_t i = 0; i < m_header.GetScalesCount(); ++i)
{
string const postfix = strings::to_string(i);
m_geoFile.push_back(make_unique<TmpFile>(fName + GEOMETRY_FILE_TAG + postfix));
m_trgFile.push_back(make_unique<TmpFile>(fName + TRIANGLE_FILE_TAG + postfix));
}
m_helperFile.resize(FILES_COUNT);
m_helperFile[METADATA] = make_unique<TmpFile>(fName + METADATA_FILE_TAG);
m_helperFile[SEARCH_TOKENS] = make_unique<TmpFile>(fName + SEARCH_TOKENS_FILE_TAG);
}
~FeaturesCollector2()
{
// Check file size.
auto const unused = CheckedFilePosCast(m_datFile);
UNUSED_VALUE(unused);
}
void Finish()
{
// write version information
{
FileWriter w = m_writer.GetWriter(VERSION_FILE_TAG);
version::WriteVersion(w, m_versionDate);
}
// write own mwm header
m_header.SetBounds(m_bounds);
{
FileWriter w = m_writer.GetWriter(HEADER_FILE_TAG);
m_header.Save(w);
}
// write region info
{
FileWriter w = m_writer.GetWriter(REGION_INFO_FILE_TAG);
m_regionData.Serialize(w);
}
// assume like we close files
Flush();
m_writer.Write(m_datFile.GetName(), DATA_FILE_TAG);
// File Writer finalization function with appending to the main mwm file.
auto const finalizeFn = [this](unique_ptr<TmpFile> w, string const & tag,
string const & postfix = string()) {
w->Flush();
m_writer.Write(w->GetName(), tag + postfix);
};
for (size_t i = 0; i < m_header.GetScalesCount(); ++i)
{
string const postfix = strings::to_string(i);
finalizeFn(move(m_geoFile[i]), GEOMETRY_FILE_TAG, postfix);
finalizeFn(move(m_trgFile[i]), TRIANGLE_FILE_TAG, postfix);
}
{
/// @todo Replace this mapping vector with succint structure.
FileWriter w = m_writer.GetWriter(METADATA_INDEX_FILE_TAG);
for (auto const & v : m_metadataOffset)
{
WriteToSink(w, v.first);
WriteToSink(w, v.second);
}
}
finalizeFn(move(m_helperFile[METADATA]), METADATA_FILE_TAG);
finalizeFn(move(m_helperFile[SEARCH_TOKENS]), SEARCH_TOKENS_FILE_TAG);
m_writer.Finish();
if (m_header.GetType() == DataHeader::country || m_header.GetType() == DataHeader::world)
{
FileWriter osm2ftWriter(m_writer.GetFileName() + OSM2FEATURE_FILE_EXTENSION);
m_osm2ft.Flush(osm2ftWriter);
}
}
void SetBounds(m2::RectD bounds) { m_bounds = bounds; }
uint32_t operator()(FeatureBuilder & fb)
{
GeometryHolder holder([this](int i) -> FileWriter & { return *m_geoFile[i]; },
[this](int i) -> FileWriter & { return *m_trgFile[i]; }, fb, m_header);
bool const isLine = fb.IsLine();
bool const isArea = fb.IsArea();
int const scalesStart = static_cast<int>(m_header.GetScalesCount()) - 1;
for (int i = scalesStart; i >= 0; --i)
{
int const level = m_header.GetScale(i);
if (fb.IsDrawableInRange(i > 0 ? m_header.GetScale(i - 1) + 1 : 0, PatchScaleBound(level)))
{
bool const isCoast = fb.IsCoastCell();
m2::RectD const rect = fb.GetLimitRect();
// Simplify and serialize geometry.
Points points;
// Do not change linear geometry for the upper scale.
if (isLine && i == scalesStart && IsCountry() && routing::IsRoad(fb.GetTypes()))
points = holder.GetSourcePoints();
else
SimplifyPoints(level, isCoast, rect, holder.GetSourcePoints(), points);
if (isLine)
holder.AddPoints(points, i);
if (isArea && holder.NeedProcessTriangles())
{
// simplify and serialize triangles
bool const good = isCoast || IsGoodArea(points, level);
// At this point we don't need last point equal to first.
CHECK_GREATER(points.size(), 0, ());
points.pop_back();
Polygons const & polys = fb.GetGeometry();
if (polys.size() == 1 && good && holder.TryToMakeStrip(points))
continue;
Polygons simplified;
if (good)
{
simplified.push_back({});
simplified.back().swap(points);
}
auto iH = polys.begin();
for (++iH; iH != polys.end(); ++iH)
{
simplified.push_back({});
SimplifyPoints(level, isCoast, rect, *iH, simplified.back());
// Increment level check for coastline polygons for the first scale level.
// This is used for better coastlines quality.
if (IsGoodArea(simplified.back(), (isCoast && i == 0) ? level + 1 : level))
{
// At this point we don't need last point equal to first.
CHECK_GREATER(simplified.back().size(), 0, ());
simplified.back().pop_back();
}
else
{
// Remove small polygon.
simplified.pop_back();
}
}
if (!simplified.empty())
holder.AddTriangles(simplified, i);
}
}
}
uint32_t featureId = kInvalidFeatureId;
auto & buffer = holder.GetBuffer();
if (fb.PreSerializeAndRemoveUselessNamesForMwm(buffer))
{
fb.SerializeForMwm(buffer, m_header.GetDefGeometryCodingParams());
featureId = WriteFeatureBase(buffer.m_buffer, fb);
fb.GetAddressData().Serialize(*(m_helperFile[SEARCH_TOKENS]));
if (!fb.GetMetadata().Empty())
{
auto const & w = m_helperFile[METADATA];
uint64_t const offset = w->Pos();
ASSERT_LESS_OR_EQUAL(offset, numeric_limits<uint32_t>::max(), ());
m_metadataOffset.emplace_back(featureId, static_cast<uint32_t>(offset));
fb.GetMetadata().Serialize(*w);
}
if (!fb.GetOsmIds().empty())
{
base::GeoObjectId const osmId = fb.GetMostGenericOsmId();
m_osm2ft.Add(make_pair(osmId, featureId));
}
};
return featureId;
}
private:
using Points = vector<m2::PointD>;
using Polygons = list<Points>;
class TmpFile : public FileWriter
{
public:
explicit TmpFile(string const & filePath) : FileWriter(filePath) {}
~TmpFile() { DeleteFileX(GetName()); }
};
using TmpFiles = vector<unique_ptr<TmpFile>>;
enum
{
METADATA = 0,
SEARCH_TOKENS = 1,
FILES_COUNT = 2
};
static bool IsGoodArea(Points const & poly, int level)
{
// Area has the same first and last points. That's why minimal number of points for
// area is 4.
if (poly.size() < 4)
return false;
m2::RectD r;
CalcRect(poly, r);
return scales::IsGoodForLevel(level, r);
}
bool IsCountry() const { return m_header.GetType() == feature::DataHeader::country; }
void SimplifyPoints(int level, bool isCoast, m2::RectD const & rect, Points const & in,
Points & out)
{
if (isCoast)
{
DistanceToSegmentWithRectBounds fn(rect);
feature::SimplifyPoints(fn, level, in, out);
}
else
{
feature::SimplifyPoints(m2::SquaredDistanceFromSegmentToPoint<m2::PointD>(), level, in, out);
}
}
FilesContainerW m_writer;
TmpFiles m_helperFile;
TmpFiles m_geoFile, m_trgFile;
// Mapping from feature id to offset in file section with the correspondent metadata.
vector<pair<uint32_t, uint32_t>> m_metadataOffset;
DataHeader m_header;
RegionData m_regionData;
uint32_t m_versionDate;
gen::OsmID2FeatureID m_osm2ft;
DISALLOW_COPY_AND_MOVE(FeaturesCollector2);
};
bool GenerateFinalFeatures(feature::GenerateInfo const & info, string const & name, int mapType)
{
string const srcFilePath = info.GetTmpFileName(name);
string const datFilePath = info.GetTargetFileName(name);
// stores cellIds for middle points
CalculateMidPoints midPoints;
platform::CountryFile const country(name);
bool const speedCamerasProhibitedMwm = routing::AreSpeedCamerasProhibited(country);
uint32_t const speedCameraType = classif().GetTypeByPath({"highway", "speed_camera"});
ForEachFromDatRawFormat(srcFilePath, [&speedCamerasProhibitedMwm, &speedCameraType, &midPoints](
FeatureBuilder const & ft, uint64_t pos) {
// Removing point features with speed cameras type from geometry index for some countries.
if (speedCamerasProhibitedMwm && ft.IsPoint() && ft.HasType(speedCameraType))
return;
midPoints(ft, pos);
});
// sort features by their middle point
midPoints.Sort();
// store sorted features
{
FileReader reader(srcFilePath);
bool const isWorld = (mapType != DataHeader::country);
// Fill mwm header.
DataHeader header;
uint8_t coordBits = kFeatureSorterPointCoordBits;
if (isWorld)
coordBits -= ((scales::GetUpperScale() - scales::GetUpperWorldScale()) / 2);
// coding params
header.SetGeometryCodingParams(serial::GeometryCodingParams(coordBits, midPoints.GetCenter()));
// scales
if (isWorld)
header.SetScales(g_arrWorldScales);
else
header.SetScales(g_arrCountryScales);
// type
header.SetType(static_cast<DataHeader::MapType>(mapType));
// region data
RegionData regionData;
if (!ReadRegionData(name, regionData))
LOG(LWARNING, ("No extra data for country:", name));
// Transform features from raw format to optimized format.
try
{
FeaturesCollector2 collector(datFilePath, header, regionData, info.m_versionDate);
for (auto const & point : midPoints.GetVector())
{
ReaderSource<FileReader> src(reader);
src.Skip(point.second);
FeatureBuilder fb;
ReadFromSourceRawFormat(src, fb);
// emit the feature
collector(fb);
}
// Update bounds with the limit rect corresponding to region borders.
// Bounds before update can be too big because of big invisible features like a
// relation that contains an entire country's border.
// Borders file may be unavailable when building test mwms.
m2::RectD bordersRect;
if (borders::GetBordersRect(info.m_targetDir, name, bordersRect))
collector.SetBounds(bordersRect);
collector.Finish();
}
catch (RootException const & ex)
{
LOG(LCRITICAL, ("MWM writing error:", ex.Msg()));
}
// at this point files should be closed
}
// remove old not-sorted dat file
FileWriter::DeleteFileX(datFilePath + DATA_FILE_TAG);
return true;
}
} // namespace feature
|