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

metalines_builder.cpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1b97d48ead664cbb10d18b5aaa97ca07ef01ad14 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "generator/metalines_builder.hpp"

#include "generator/routing_helpers.hpp"

#include "indexer/classificator.hpp"

#include "coding/file_container.hpp"
#include "coding/varint.hpp"
#include "coding/write_to_sink.hpp"

#include "base/checked_cast.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"

#include "defines.hpp"

#include <cstdint>
#include <map>

namespace
{
uint8_t const kMetaLinesSectionVersion = 1;

using Ways = std::vector<int32_t>;

/// A string of connected ways.
class LineString
{
  Ways m_ways;
  uint64_t m_start;
  uint64_t m_end;
  bool m_oneway;

public:
  explicit LineString(OsmElement const & way)
  {
    std::string const oneway = way.GetTag("oneway");
    m_oneway = !oneway.empty() && oneway != "no";
    int32_t const wayId = base::checked_cast<int32_t>(way.m_id);
    m_ways.push_back(oneway == "-1" ? -wayId : wayId);
    CHECK_GREATER_OR_EQUAL(way.Nodes().size(), 2, ());
    m_start = way.Nodes().front();
    m_end = way.Nodes().back();
  }

  Ways const & GetWays() const { return m_ways; }

  void Reverse()
  {
    ASSERT(!m_oneway, ("Trying to reverse a one-way road."));
    std::swap(m_start, m_end);
    std::reverse(m_ways.begin(), m_ways.end());
    for (auto & p : m_ways)
      p = -p;
  }

  bool Add(LineString & line)
  {
    if (m_start == line.m_start || m_end == line.m_end)
    {
      if (!line.m_oneway)
        line.Reverse();
      else if (!m_oneway)
        Reverse();
      else
        return false;
    }
    if (m_end == line.m_start)
    {
      m_ways.insert(m_ways.end(), line.m_ways.begin(), line.m_ways.end());
      m_end = line.m_end;
      m_oneway = m_oneway || line.m_oneway;
    }
    else if (m_start == line.m_end)
    {
      m_ways.insert(m_ways.begin(), line.m_ways.begin(), line.m_ways.end());
      m_start = line.m_start;
      m_oneway = m_oneway || line.m_oneway;
    }
    else
    {
      return false;
    }
    return true;
  }
};
}  // namespace

namespace feature
{
/// A list of segments, that is, LineStrings, sharing the same attributes.
class Segments
{
  std::list<LineString> m_parts;

public:
  explicit Segments(OsmElement const & way) { m_parts.emplace_back(way); }

  void Add(OsmElement const & way)
  {
    LineString line(way);
    auto found = m_parts.end();
    for (auto i = m_parts.begin(); i != m_parts.end(); ++i)
    {
      if (i->Add(line))
      {
        found = i;
        break;
      }
    }
    // If no LineString accepted the way in its Add method, create a new LineString with it.
    if (found == m_parts.cend())
    {
      m_parts.push_back(line);
      return;
    }
    // Otherwise check if the extended LineString can be merged with some other LineString.
    for (LineString & part : m_parts)
    {
      if (part.Add(*found))
      {
        m_parts.erase(found);
        break;
      }
    }
  }

  std::vector<Ways> GetLongWays() const
  {
    std::vector<Ways> result;
    for (LineString const & line : m_parts)
    {
      if (line.GetWays().size() > 1)
      {
        result.push_back(line.GetWays());
      }
    }
    return result;
  }
};

// MetalinesBuilder --------------------------------------------------------------------------------
void MetalinesBuilder::CollectFeature(FeatureBuilder const & feature, OsmElement const & element)
{
  if (!feature.IsLine())
    return;

  auto const & params = feature.GetParams();
  static uint32_t const highwayType = classif().GetTypeByPath({"highway"});
  if (params.FindType(highwayType, 1) == ftype::GetEmptyValue() ||
      element.Nodes().front() == element.Nodes().back())
  {
    return;
  }

  std::string name;
  params.name.GetString(StringUtf8Multilang::kDefaultCode, name);
  if (name.empty() && params.ref.empty())
    return;

  size_t const key = std::hash<std::string>{}(name + '\0' + params.ref);
  auto segment = m_data.find(key);
  if (segment == m_data.cend())
    m_data.emplace(key, std::make_shared<Segments>(element));
  else
    segment->second->Add(element);
}

void MetalinesBuilder::Save()
{
  Flush();
}

void MetalinesBuilder::Flush()
{
  try
  {
    uint32_t count = 0;
    FileWriter writer(m_filePath);
    for (auto const & seg : m_data)
    {
      auto const & longWays = seg.second->GetLongWays();
      for (Ways const & ways : longWays)
      {
        uint16_t size = base::checked_cast<uint16_t>(ways.size());
        WriteToSink(writer, size);
        for (int32_t const way : ways)
          WriteToSink(writer, way);
        ++count;
      }
    }
    LOG_SHORT(LINFO, ("Wrote", count, "metalines with OSM IDs for the entire planet to", m_filePath));
  }
  catch (RootException const & e)
  {
    LOG(LERROR, ("An exception happened while saving metalines to", m_filePath, ":", e.what()));
  }
}

// Functions --------------------------------------------------------------------------------
bool WriteMetalinesSection(std::string const & mwmPath, std::string const & metalinesPath,
                           std::string const & osmIdsToFeatureIdsPath)
{
  std::map<base::GeoObjectId, uint32_t> osmIdToFeatureId;
  if (!routing::ParseRoadsOsmIdToFeatureIdMapping(osmIdsToFeatureIdsPath, osmIdToFeatureId))
    return false;

  FileReader reader(metalinesPath);
  ReaderSource<FileReader> src(reader);
  std::vector<uint8_t> buffer;
  MemWriter<std::vector<uint8_t>> memWriter(buffer);
  uint32_t count = 0;

  while (src.Size() > 0)
  {
    std::vector<int32_t> featureIds;
    uint16_t size = ReadPrimitiveFromSource<uint16_t>(src);
    std::vector<int32_t> ways(size);
    src.Read(ways.data(), size * sizeof(int32_t));
    for (auto const wayId : ways)
    {
      // We get a negative wayId when a feature direction should be reversed.
      auto fid = osmIdToFeatureId.find(base::MakeOsmWay(std::abs(wayId)));
      if (fid == osmIdToFeatureId.cend())
        break;

      // Keeping the sign for the feature direction.
      int32_t const featureId = static_cast<int32_t>(fid->second);
      featureIds.push_back(wayId > 0 ? featureId : -featureId);
    }

    if (featureIds.size() > 1)
    {
      // Write vector to buffer if we have got at least two segments.
      WriteVarUint(memWriter, featureIds.size());
      for (auto const & fid : featureIds)
        WriteVarInt(memWriter, fid);
      ++count;
    }
  }

  // Write buffer to section.
  FilesContainerW cont(mwmPath, FileWriter::OP_WRITE_EXISTING);
  FileWriter writer = cont.GetWriter(METALINES_FILE_TAG);
  WriteToSink(writer, kMetaLinesSectionVersion);
  WriteVarUint(writer, count);
  writer.Write(buffer.data(), buffer.size());
  LOG(LINFO, ("Finished writing metalines section, found", count, "metalines."));
  return true;
}
}  // namespace feature