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

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

#include "generator/restriction_collector.hpp"

#include "routing/index_graph_loader.hpp"

#include "routing_common/car_model.cpp"
#include "routing_common/vehicle_model.hpp"

#include "platform/country_file.hpp"
#include "platform/local_country_file.hpp"

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

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

#include "defines.hpp"

#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include <vector>

namespace
{
using namespace routing;

std::unique_ptr<IndexGraph>
CreateIndexGraph(std::string const & targetPath,
                 std::string const & mwmPath,
                 std::string const & country,
                 CountryParentNameGetterFn const & countryParentNameGetterFn)
{
  std::shared_ptr<VehicleModelInterface> vehicleModel =
      CarModelFactory(countryParentNameGetterFn).GetVehicleModelForCountry(country);

  MwmValue mwmValue(
      platform::LocalCountryFile(targetPath, platform::CountryFile(country), 0 /* version */));

  auto graph = std::make_unique<IndexGraph>(
      std::make_shared<Geometry>(GeometryLoader::CreateFromFile(mwmPath, vehicleModel)),
      EdgeEstimator::Create(VehicleType::Car, *vehicleModel, nullptr /* trafficStash */));

  DeserializeIndexGraph(mwmValue, VehicleType::Car, *graph);

  return graph;
}
}  // namespace

namespace routing
{
std::unique_ptr<RestrictionCollector>
CreateRestrictionCollectorAndParse(
    std::string const & targetPath, std::string const & mwmPath, std::string const & country,
    std::string const & restrictionPath, std::string const & osmIdsToFeatureIdsPath,
    CountryParentNameGetterFn const & countryParentNameGetterFn)
{
  LOG(LDEBUG, ("BuildRoadRestrictions(", targetPath, ", ", restrictionPath, ", ",
                                         osmIdsToFeatureIdsPath, ");"));

  std::unique_ptr<IndexGraph> graph =
      CreateIndexGraph(targetPath, mwmPath, country, countryParentNameGetterFn);

  auto restrictionCollector =
      std::make_unique<RestrictionCollector>(osmIdsToFeatureIdsPath, std::move(graph));

  if (!restrictionCollector->Process(restrictionPath))
    return {};

  if (!restrictionCollector->HasRestrictions())
  {
    LOG(LINFO, ("No restrictions for", targetPath, "It's necessary to check that",
                restrictionPath, "and", osmIdsToFeatureIdsPath, "are available."));
    return {};
  }

  return restrictionCollector;
}

void SerializeRestrictions(RestrictionCollector const & restrictionCollector,
                           std::string const & mwmPath)
{
  auto const & restrictions = restrictionCollector.GetRestrictions();

  auto const firstOnlyIt =
      std::lower_bound(restrictions.cbegin(), restrictions.cend(),
                       Restriction(Restriction::Type::Only, {} /* links */),
                       base::LessBy(&Restriction::m_type));

  RestrictionHeader header;
  header.m_noRestrictionCount = base::checked_cast<uint32_t>(std::distance(restrictions.cbegin(), firstOnlyIt));
  header.m_onlyRestrictionCount = base::checked_cast<uint32_t>(restrictions.size() - header.m_noRestrictionCount);

  LOG(LINFO, ("Header info. There are", header.m_noRestrictionCount, "restrictions of type No and",
              header.m_onlyRestrictionCount, "restrictions of type Only"));

  FilesContainerW cont(mwmPath, FileWriter::OP_WRITE_EXISTING);
  FileWriter w = cont.GetWriter(RESTRICTIONS_FILE_TAG);
  header.Serialize(w);

  RestrictionSerializer::Serialize(header, restrictions.cbegin(), restrictions.cend(), w);
}

bool BuildRoadRestrictions(std::string const & targetPath,
                           std::string const & mwmPath,
                           std::string const & country,
                           std::string const & restrictionPath,
                           std::string const & osmIdsToFeatureIdsPath,
                           CountryParentNameGetterFn const & countryParentNameGetterFn)
{
  auto const collector =
      CreateRestrictionCollectorAndParse(targetPath, mwmPath, country, restrictionPath,
                                         osmIdsToFeatureIdsPath, countryParentNameGetterFn);

  if (!collector)
    return false;

  SerializeRestrictions(*collector, mwmPath);
  return true;
}
}  // namespace routing