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
|
#include "generator/collector_camera.hpp"
#include "generator/feature_builder.hpp"
#include "generator/osm_element.hpp"
#include "generator/maxspeeds_parser.hpp"
#include "routing/routing_helpers.hpp"
#include "routing_common/maxspeed_conversion.hpp"
#include "indexer/ftypes_matcher.hpp"
#include "platform/measurement_utils.hpp"
#include "coding/point_coding.hpp"
#include "coding/write_to_sink.hpp"
#include "geometry/latlon.hpp"
#include "base/assert.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"
using namespace feature;
namespace routing
{
size_t const CameraProcessor::kMaxSpeedSpeedStringLength = 32;
std::string ValidateMaxSpeedString(std::string const & maxSpeedString)
{
routing::SpeedInUnits speed;
if (!generator::ParseMaxspeedTag(maxSpeedString, speed) || !speed.IsNumeric())
return std::string();
return strings::to_string(measurement_utils::ToSpeedKmPH(speed.GetSpeed(), speed.GetUnits()));
}
CameraProcessor::CameraInfo::CameraInfo(OsmElement const & element)
: m_id(element.m_id)
, m_lon(element.m_lon)
, m_lat(element.m_lat)
{
auto const maxspeed = element.GetTag("maxspeed");
if (!maxspeed.empty())
m_speed = ValidateMaxSpeedString(maxspeed);
}
void CameraProcessor::ForEachCamera(Fn && toDo) const
{
std::vector<uint64_t> empty;
for (auto const & p : m_speedCameras)
{
auto const & ways = m_cameraToWays.count(p.first) != 0 ? m_cameraToWays.at(p.first) : empty;
toDo(p.second, ways);
}
}
void CameraProcessor::ProcessWay(OsmElement const & element)
{
for (auto const node : element.m_nodes)
{
if (m_speedCameras.find(node) == m_speedCameras.cend())
continue;
auto & ways = m_cameraToWays[node];
ways.push_back(element.m_id);
}
}
void CameraProcessor::ProcessNode(OsmElement const & element)
{
CameraInfo camera(element);
CHECK_LESS(camera.m_speed.size(), kMaxSpeedSpeedStringLength, ());
m_speedCameras.emplace(element.m_id, std::move(camera));
}
CameraCollector::CameraCollector(std::string const & writerFile) :
m_fileWriter(writerFile) {}
void CameraCollector::CollectFeature(FeatureBuilder const & feature, OsmElement const & element)
{
switch (element.m_type)
{
case OsmElement::EntityType::Node:
{
if (ftypes::IsSpeedCamChecker::Instance()(feature.GetTypes()))
m_processor.ProcessNode(element);
break;
}
case OsmElement::EntityType::Way:
{
if (routing::IsCarRoad(feature.GetTypes()))
m_processor.ProcessWay(element);
break;
}
default:
break;
}
}
void CameraCollector::Write(CameraProcessor::CameraInfo const & camera, std::vector<uint64_t> const & ways)
{
std::string maxSpeedStringKmPH = camera.m_speed;
int32_t maxSpeedKmPH = 0;
if (!strings::to_int(maxSpeedStringKmPH.c_str(), maxSpeedKmPH))
LOG(LWARNING, ("Bad speed format of camera:", maxSpeedStringKmPH, ", osmId:", camera.m_id));
CHECK_GREATER_OR_EQUAL(maxSpeedKmPH, 0, ());
uint32_t const lat =
DoubleToUint32(camera.m_lat, ms::LatLon::kMinLat, ms::LatLon::kMaxLat, kPointCoordBits);
WriteToSink(m_fileWriter, lat);
uint32_t const lon =
DoubleToUint32(camera.m_lon, ms::LatLon::kMinLon, ms::LatLon::kMaxLon, kPointCoordBits);
WriteToSink(m_fileWriter, lon);
WriteToSink(m_fileWriter, static_cast<uint32_t>(maxSpeedKmPH));
auto const size = static_cast<uint32_t>(ways.size());
WriteToSink(m_fileWriter, size);
for (auto wayId : ways)
WriteToSink(m_fileWriter, wayId);
}
void CameraCollector::Save()
{
using namespace std::placeholders;
m_processor.ForEachCamera(std::bind(&CameraCollector::Write, this, _1, _2));
}
} // namespace routing
|