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

cities_boundaries_builder.cpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8da3152b42e748253f2b904c3eebc9d014b5b3c1 (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
#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/stl_add.hpp"
#include "base/string_utils.hpp"

#include <map>
#include <memory>
#include <utility>
#include <vector>

#include "defines.hpp"

using namespace indexer;
using namespace search;
using namespace std;

namespace generator
{
namespace
{
bool ParseFeatureIdToOsmIdMapping(string const & path, map<uint32_t, vector<osm::Id>> & mapping)
{
  return ForEachOsmId2FeatureId(path, [&](osm::Id const & osmId, uint32_t const featureId) {
    mapping[featureId].push_back(osmId);
  });
}

bool ParseFeatureIdToTestIdMapping(string const & path, map<uint32_t, vector<uint64_t>> & mapping)
{
  bool success = true;
  feature::ForEachFromDat(path, [&](FeatureType & feature, uint32_t fid) {
    auto const & metatada = feature.GetMetadata();
    auto const sid = metatada.Get(feature::Metadata::FMD_TEST_ID);
    uint64_t tid;
    if (!strings::to_uint64(sid, tid))
    {
      LOG(LERROR, ("Can't parse test id from:", sid, "for a feature", fid));
      success = false;
      return;
    }
    mapping[fid].push_back(tid);
  });
  return success;
}

CBV GetLocalities(string const & dataPath)
{
  DataSource dataSource(make_unique<FeatureSourceFactory>());
  auto const result = dataSource.Register(platform::LocalCountryFile::MakeTemporary(dataPath));
  CHECK_EQUAL(result.second, MwmSet::RegResult::Success, ("Can't register", dataPath));

  search::MwmContext context(dataSource.GetMwmHandleById(result.first));
  return search::CategoriesCache(LocalitiesSource{}, base::Cancellable{}).Get(context);
}

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())
    {
      for (auto const & id : it->second)
      {
        auto const & b = table.Get(id);
        bs.insert(bs.end(), b.begin(), b.end());
      }
    }

    all.emplace_back(move(bs));
  });

  FilesContainerW container(dataPath, FileWriter::OP_WRITE_EXISTING);
  FileWriter 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 = map<uint32_t, vector<osm::Id>>;

  return BuildCitiesBoundaries(dataPath, table, [&]() -> unique_ptr<Mapping> {
    Mapping mapping;
    if (!ParseFeatureIdToOsmIdMapping(dataPath + OSM2FEATURE_FILE_EXTENSION, mapping))
    {
      LOG(LERROR, ("Can't parse feature id to osm id mapping."));
      return {};
    }
    return my::make_unique<Mapping>(move(mapping));
  });
}

bool BuildCitiesBoundariesForTesting(string const & dataPath, TestIdToBoundariesTable & table)
{
  using Mapping = map<uint32_t, vector<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 my::make_unique<Mapping>(move(mapping));
  });
}

bool SerializeBoundariesTable(std::string const & path, OsmIdToBoundariesTable & table)
{
  vector<vector<osm::Id>> allIds;
  vector<vector<CityBoundary>> allBoundaries;
  table.ForEachCluster([&](vector<osm::Id> const & ids, vector<CityBoundary> const & boundaries) {
    allIds.push_back(ids);
    allBoundaries.push_back(boundaries);
  });

  CHECK_EQUAL(allIds.size(), allBoundaries.size(), ());

  try
  {
    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.EncodedId());
    }

    return true;
  }
  catch (Writer::Exception const & e)
  {
    LOG(LERROR, ("Can't serialize boundaries table:", e.what()));
    return false;
  }
}

bool DeserializeBoundariesTable(std::string const & path, OsmIdToBoundariesTable & table)
{
  vector<vector<osm::Id>> 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 = osm::Id(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