forked from organicmaps/organicmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector_mini_roundabout.cpp
154 lines (125 loc) · 4.56 KB
/
collector_mini_roundabout.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
#include "generator/collector_mini_roundabout.hpp"
#include "generator/feature_builder.hpp"
#include "generator/intermediate_data.hpp"
#include "routing/routing_helpers.hpp"
#include "platform/platform.hpp"
#include "coding/internal/file_data.hpp"
#include "coding/reader_writer_ops.hpp"
#include "coding/write_to_sink.hpp"
#include "base/assert.hpp"
#include <algorithm>
#include <iterator>
using namespace feature;
namespace generator
{
MiniRoundaboutProcessor::MiniRoundaboutProcessor(std::string const & filename)
: m_waysFilename(filename + MINI_ROUNDABOUT_ROADS_EXTENSION)
, m_waysWriter(std::make_unique<FileWriter>(m_waysFilename))
{
}
MiniRoundaboutProcessor::~MiniRoundaboutProcessor()
{
CHECK(Platform::RemoveFileIfExists(m_waysFilename), ());
}
void MiniRoundaboutProcessor::ProcessWay(OsmElement const & element)
{
WriteToSink(*m_waysWriter, element.m_id);
rw::WriteVectorOfPOD(*m_waysWriter, element.m_nodes);
}
void MiniRoundaboutProcessor::FillMiniRoundaboutsInWays()
{
FileReader reader(m_waysFilename);
ReaderSource<FileReader> src(reader);
while (src.Size() > 0)
{
uint64_t const wayId = ReadPrimitiveFromSource<uint64_t>(src);
std::vector<uint64_t> nodes;
rw::ReadVectorOfPOD(src, nodes);
for (auto const & node : nodes)
{
auto const itMiniRoundabout = m_miniRoundabouts.find(node);
if (itMiniRoundabout != m_miniRoundabouts.end())
itMiniRoundabout->second.m_ways.push_back(wayId);
}
}
ForEachMiniRoundabout([](auto & rb) { rb.Normalize(); });
}
void MiniRoundaboutProcessor::ProcessNode(OsmElement const & element)
{
m_miniRoundabouts.emplace(element.m_id, MiniRoundaboutInfo(element));
}
void MiniRoundaboutProcessor::ProcessRestriction(uint64_t osmId)
{
m_miniRoundaboutsExceptions.insert(osmId);
}
void MiniRoundaboutProcessor::Finish() { m_waysWriter.reset(); }
void MiniRoundaboutProcessor::Merge(MiniRoundaboutProcessor const & miniRoundaboutProcessor)
{
auto const & otherMiniRoundabouts = miniRoundaboutProcessor.m_miniRoundabouts;
m_miniRoundabouts.insert(otherMiniRoundabouts.begin(), otherMiniRoundabouts.end());
auto const & otherMiniRoundaboutsExceptions = miniRoundaboutProcessor.m_miniRoundaboutsExceptions;
m_miniRoundaboutsExceptions.insert(otherMiniRoundaboutsExceptions.begin(),
otherMiniRoundaboutsExceptions.end());
base::AppendFileToFile(miniRoundaboutProcessor.m_waysFilename, m_waysFilename);
}
// MiniRoundaboutCollector -------------------------------------------------------------------------
MiniRoundaboutCollector::MiniRoundaboutCollector(std::string const & filename)
: generator::CollectorInterface(filename), m_processor(GetTmpFilename())
{
}
std::shared_ptr<generator::CollectorInterface> MiniRoundaboutCollector::Clone(
std::shared_ptr<generator::cache::IntermediateDataReaderInterface> const &) const
{
return std::make_shared<MiniRoundaboutCollector>(GetFilename());
}
void MiniRoundaboutCollector::Collect(OsmElement const & element)
{
if (element.IsNode() && element.HasTag("highway", "mini_roundabout"))
{
m_processor.ProcessNode(element);
return;
}
// Skip mini_roundabouts with role="via" in restrictions
if (element.IsRelation())
{
for (auto const & member : element.m_members)
{
if (member.m_type == OsmElement::EntityType::Node && member.m_role == "via")
{
m_processor.ProcessRestriction(member.m_ref);
return;
}
}
}
}
void MiniRoundaboutCollector::CollectFeature(FeatureBuilder const & feature,
OsmElement const & element)
{
if (feature.IsLine() && routing::IsCarRoad(feature.GetTypes()))
m_processor.ProcessWay(element);
}
void MiniRoundaboutCollector::Finish() { m_processor.Finish(); }
void MiniRoundaboutCollector::Save()
{
m_processor.FillMiniRoundaboutsInWays();
FileWriter writer(GetFilename());
m_processor.ForEachMiniRoundabout(
[&](auto const & miniRoundabout) { WriteMiniRoundabout(writer, miniRoundabout); });
}
void MiniRoundaboutCollector::OrderCollectedData()
{
auto collectedData = ReadMiniRoundabouts(GetFilename());
std::sort(std::begin(collectedData), std::end(collectedData));
FileWriter writer(GetFilename());
for (auto const & miniRoundabout : collectedData)
WriteMiniRoundabout(writer, miniRoundabout);
}
void MiniRoundaboutCollector::Merge(generator::CollectorInterface const & collector)
{
collector.MergeInto(*this);
}
void MiniRoundaboutCollector::MergeInto(MiniRoundaboutCollector & collector) const
{
collector.m_processor.Merge(m_processor);
}
} // namespace generator