forked from organicmaps/organicmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcities_boundaries_builder.cpp
180 lines (147 loc) · 4.73 KB
/
cities_boundaries_builder.cpp
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 "generator/cities_boundaries_builder.hpp"
#include "generator/utils.hpp"
#include "search/categories_cache.hpp"
#include "search/cbv.hpp"
#include "search/localities_source.hpp"
#include "search/mwm_context.hpp"
#include "indexer/cities_boundaries_serdes.hpp"
#include "indexer/city_boundary.hpp"
#include "indexer/data_source.hpp"
#include "indexer/feature_processor.hpp"
#include "indexer/mwm_set.hpp"
#include "platform/local_country_file.hpp"
#include "coding/file_reader.hpp"
#include "coding/file_writer.hpp"
#include "coding/reader.hpp"
#include "base/assert.hpp"
#include "base/checked_cast.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"
#include <memory>
#include <unordered_map>
#include <utility>
#include <vector>
#include "defines.hpp"
using namespace indexer;
using namespace search;
using namespace std;
namespace generator
{
namespace
{
template <typename BoundariesTable, typename MappingReader>
bool BuildCitiesBoundaries(string const & dataPath, BoundariesTable & table,
MappingReader && reader)
{
auto const localities = GetLocalities(dataPath);
auto mapping = reader();
if (!mapping)
return false;
vector<vector<CityBoundary>> all;
localities.ForEach([&](uint64_t fid) {
vector<CityBoundary> bs;
auto it = mapping->find(base::asserted_cast<uint32_t>(fid));
if (it != mapping->end())
{
auto const & b = table.Get(it->second);
bs.insert(bs.end(), b.begin(), b.end());
}
all.emplace_back(move(bs));
});
FilesContainerW container(dataPath, FileWriter::OP_WRITE_EXISTING);
auto sink = container.GetWriter(CITIES_BOUNDARIES_FILE_TAG);
indexer::CitiesBoundariesSerDes::Serialize(*sink, all);
return true;
}
} // namespace
bool BuildCitiesBoundaries(string const & dataPath, string const & osmToFeaturePath,
OsmIdToBoundariesTable & table)
{
using Mapping = unordered_map<uint32_t, base::GeoObjectId>;
return BuildCitiesBoundaries(dataPath, table, [&]() -> unique_ptr<Mapping> {
Mapping mapping;
// todo(@m) Use osmToFeaturePath?
if (!ParseFeatureIdToOsmIdMapping(dataPath + OSM2FEATURE_FILE_EXTENSION, mapping))
{
LOG(LERROR, ("Can't parse feature id to osm id mapping."));
return {};
}
return make_unique<Mapping>(move(mapping));
});
}
bool BuildCitiesBoundariesForTesting(string const & dataPath, TestIdToBoundariesTable & table)
{
using Mapping = unordered_map<uint32_t, uint64_t>;
return BuildCitiesBoundaries(dataPath, table, [&]() -> unique_ptr<Mapping> {
Mapping mapping;
if (!ParseFeatureIdToTestIdMapping(dataPath, mapping))
{
LOG(LERROR, ("Can't parse feature id to test id mapping."));
return {};
}
return make_unique<Mapping>(move(mapping));
});
}
void SerializeBoundariesTable(std::string const & path, OsmIdToBoundariesTable & table)
{
vector<vector<base::GeoObjectId>> allIds;
vector<vector<CityBoundary>> allBoundaries;
table.ForEachCluster(
[&](vector<base::GeoObjectId> const & ids, vector<CityBoundary> const & boundaries) {
allIds.push_back(ids);
allBoundaries.push_back(boundaries);
});
CHECK_EQUAL(allIds.size(), allBoundaries.size(), ());
FileWriter sink(path);
indexer::CitiesBoundariesSerDes::Serialize(sink, allBoundaries);
for (auto const & ids : allIds)
{
WriteToSink(sink, static_cast<uint64_t>(ids.size()));
for (auto const & id : ids)
WriteToSink(sink, id.GetEncodedId());
}
}
bool DeserializeBoundariesTable(std::string const & path, OsmIdToBoundariesTable & table)
{
vector<vector<base::GeoObjectId>> allIds;
vector<vector<CityBoundary>> allBoundaries;
try
{
FileReader reader(path);
NonOwningReaderSource source(reader);
double precision;
indexer::CitiesBoundariesSerDes::Deserialize(source, allBoundaries, precision);
auto const n = allBoundaries.size();
allIds.resize(n);
for (auto & ids : allIds)
{
auto const m = ReadPrimitiveFromSource<uint64_t>(source);
ids.resize(m);
CHECK(m != 0, ());
for (auto & id : ids)
{
auto const encodedId = ReadPrimitiveFromSource<uint64_t>(source);
id = base::GeoObjectId(encodedId);
}
}
}
catch (Reader::Exception const & e)
{
LOG(LERROR, ("Can't deserialize boundaries table:", e.what()));
return false;
}
CHECK_EQUAL(allBoundaries.size(), allIds.size(), ());
table.Clear();
for (size_t i = 0; i < allBoundaries.size(); ++i)
{
auto const & ids = allIds[i];
CHECK(!ids.empty(), ());
auto const & id = ids.front();
for (auto const & b : allBoundaries[i])
table.Append(id, b);
for (size_t j = 1; j < ids.size(); ++j)
table.Union(id, ids[j]);
}
return true;
}
} // namespace generator