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

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

#include "generator/maxspeeds_parser.hpp"
#include "generator/routing_helpers.hpp"

#include "routing/maxspeeds_serialization.hpp"
#include "routing/routing_helpers.hpp"

#include "routing_common/maxspeed_conversion.hpp"

#include "indexer/feature.hpp"
#include "indexer/feature_data.hpp"
#include "indexer/feature_processor.hpp"

#include "coding/file_container.hpp"
#include "coding/file_writer.hpp"

#include "platform/measurement_utils.hpp"

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

#include <algorithm>
#include <fstream>
#include <sstream>
#include <utility>

#include "defines.hpp"

using namespace feature;
using namespace generator;
using namespace routing;
using namespace std;

namespace
{
char const kDelim[] = ", \t\r\n";

bool ParseOneSpeedValue(strings::SimpleTokenizer & iter, uint16_t & value)
{
  if (!iter)
    return false;

  uint64_t parsedSpeed = 0;
  if (!strings::to_uint64(*iter, parsedSpeed))
    return false;
  if (parsedSpeed > numeric_limits<uint16_t>::max())
    return false;
  value = static_cast<uint16_t>(parsedSpeed);
  ++iter;
  return true;
}

FeatureMaxspeed ToFeatureMaxspeed(uint32_t featureId, Maxspeed const & maxspeed)
{
  return FeatureMaxspeed(featureId, maxspeed.GetUnits(), maxspeed.GetForward(),
                         maxspeed.GetBackward());
}

/// \brief Collects all maxspeed tag values of specified mwm based on maxspeeds.csv file.
class MaxspeedsMwmCollector
{
public:
  MaxspeedsMwmCollector(string const & dataPath,
                       map<uint32_t, base::GeoObjectId> const & featureIdToOsmId,
                       string const & maxspeedCsvPath);

  vector<FeatureMaxspeed> && StealMaxspeeds();

private:
  vector<FeatureMaxspeed> m_maxspeeds;
};

MaxspeedsMwmCollector::MaxspeedsMwmCollector(
    string const & dataPath, map<uint32_t, base::GeoObjectId> const & featureIdToOsmId,
    string const & maxspeedCsvPath)
{
  OsmIdToMaxspeed osmIdToMaxspeed;
  CHECK(ParseMaxspeeds(maxspeedCsvPath, osmIdToMaxspeed), (maxspeedCsvPath));

  ForEachFromDat(dataPath, [&](FeatureType & ft, uint32_t fid) {
    if (!routing::IsCarRoad(TypesHolder(ft)))
      return;

    auto const osmIdIt = featureIdToOsmId.find(fid);
    if (osmIdIt == featureIdToOsmId.cend())
      return;

    // @TODO Consider adding check here. |fid| should be in |featureIdToOsmId| anyway.
    auto const maxspeedIt = osmIdToMaxspeed.find(osmIdIt->second);
    if (maxspeedIt == osmIdToMaxspeed.cend())
      return;

    auto const & maxspeed = maxspeedIt->second;
    m_maxspeeds.push_back(ToFeatureMaxspeed(fid, maxspeed));
  });
}

vector<FeatureMaxspeed> && MaxspeedsMwmCollector::StealMaxspeeds()
{
  CHECK(is_sorted(m_maxspeeds.cbegin(), m_maxspeeds.cend(), IsFeatureIdLess), ());
  return move(m_maxspeeds);
}
}  // namespace

namespace routing
{
bool ParseMaxspeeds(string const & maxspeedsFilename, OsmIdToMaxspeed & osmIdToMaxspeed)
{
  osmIdToMaxspeed.clear();

  ifstream stream(maxspeedsFilename);
  if (!stream)
    return false;

  string line;
  while (getline(stream, line))
  {
    strings::SimpleTokenizer iter(line, kDelim);

    if (!iter)  // the line is empty
      return false;
    // @TODO(bykoianko) strings::to_uint64 returns not-zero value if |*iter| is equal to
    // a too long string of numbers. But ParseMaxspeeds() should return false in this case.
    uint64_t osmId = 0;
    if (!strings::to_uint64(*iter, osmId))
      return false;
    ++iter;

    if (!iter)
      return false;
    Maxspeed speed;
    speed.SetUnits(StringToUnits(*iter));
    ++iter;

    uint16_t forward = 0;
    if (!ParseOneSpeedValue(iter, forward))
      return false;

    speed.SetForward(forward);

    if (iter)
    {
      // There's backward maxspeed limit.
      uint16_t backward = 0;
      if (!ParseOneSpeedValue(iter, backward))
        return false;

      speed.SetBackward(backward);

      if (iter)
        return false;
    }

    auto const res = osmIdToMaxspeed.insert(make_pair(base::MakeOsmWay(osmId), speed));
    if (!res.second)
      return false;
  }
  return true;
}

void SerializeMaxspeeds(string const & dataPath, vector<FeatureMaxspeed> && speeds)
{
  if (speeds.empty())
    return;

  FilesContainerW cont(dataPath, FileWriter::OP_WRITE_EXISTING);
  FileWriter writer = cont.GetWriter(MAXSPEEDS_FILE_TAG);

  MaxspeedsSerializer::Serialize(speeds, writer);
  LOG(LINFO, ("SerializeMaxspeeds(", dataPath, ", ...) serialized:", speeds.size(), "maxspeed tags."));
}

void BuildMaxspeedsSection(string const & dataPath,
                           map<uint32_t, base::GeoObjectId> const & featureIdToOsmId,
                           string const & maxspeedsFilename)
{
  MaxspeedsMwmCollector collector(dataPath, featureIdToOsmId, maxspeedsFilename);
  SerializeMaxspeeds(dataPath, collector.StealMaxspeeds());
}

void BuildMaxspeedsSection(string const & dataPath, string const & osmToFeaturePath,
                           string const & maxspeedsFilename)
{
  LOG(LINFO, ("BuildMaxspeedsSection(", dataPath, ",", osmToFeaturePath, ",", maxspeedsFilename, ")"));

  map<uint32_t, base::GeoObjectId> featureIdToOsmId;
  CHECK(ParseRoadsFeatureIdToOsmIdMapping(osmToFeaturePath, featureIdToOsmId), ());
  BuildMaxspeedsSection(dataPath, featureIdToOsmId, maxspeedsFilename);
}
}  // namespace routing