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
#include <kooling/component/classifier/rail.h>
#include <kooling/datamodel/sensor.h>

#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/algorithm/find_if.hpp>
#include <boost/range/algorithm/max_element.hpp>
#include <boost/range/join.hpp>

#include <unordered_set>

namespace kooling::component::classifier {

using bbox_t              = kooling::geo::bounding_box;
using bboxes_t            = kooling::geo::bounding_boxes;
using distance_t          = kooling::datamodel::distance;
using multi_line_string_t = kooling::geo::multi_line_string;
using multi_point_t       = kooling::geo::multi_point;
using segment_t           = kooling::datamodel::segment;
using sensor_data_point_t = kooling::datamodel::sensor_data_point;
using transport_t         = kooling::datamodel::transport;

// TODO - find a place for this
static int percent(const size_t num, double percent)
{
    return std::ceil(num * percent / 100.);
}

template<typename DPS>
multi_point_t check_stations(
    const segment_t&     segment,
    const DPS&           dps,
    const multi_point_t& geom,
    const distance_t&    vicinity,
    int                  min_found,
    transport_t          what)
{
    if (const auto found{ kooling::geo::near(geom, dps, vicinity, min_found * 10) };
        found.points.size() >= static_cast<std::size_t>(min_found))
    {
        PLOG_DEBUG
            << segment.id << ": is_rail (" << what << ") station - yes: "
            << "found: " << found.points.size()
            << " out of: " << boost::size(dps);
        return found;
    }
    else
    {
        PLOG_DEBUG
            << segment.id << ": is_rail (" << what << ") station - no: "
            << "found: " << found.points.size() << ", required: " << min_found;
        return {};
    }
}

template<typename Coords>
multi_point_t check_ways(
    const segment_t&           segment,
    const Coords&              coords,
    const multi_line_string_t& geom,
    const distance_t&          vicinity,
    int                        min_found_dp,
    int                        min_found_interpolated,
    transport_t                what)
{
    if (const auto found{ kooling::geo::near(geom, coords, vicinity, min_found_dp * 10) };<--- Shadowed declaration
        found.points.size() >= static_cast<std::size_t>(min_found_dp))
    {
        PLOG_DEBUG
            << segment.id << ": is_rail (" << what << ") ways - yes: "
            << "found: " << found.points.size()
            << " out of: " << segment.nof_dps
            << " DPs";
        return found;
    }
    else if (const auto found{ kooling::geo::near(geom, segment.interpolated.points, vicinity, min_found_interpolated * 10) };<--- Shadow variable
        found.points.size() >= static_cast<std::size_t>(min_found_interpolated))
    {
        PLOG_DEBUG
            << segment.id << ": is_rail (" << what << ") ways - yes: "
            << "found: " << found.points.size()
            << " out of: " << segment.interpolated.points.size()
            << " interpolated DPs";
        return found;
    }
    else
    {
        PLOG_DEBUG
            << segment.id << ": is_rail (" << what << ") ways - no: "
            << "found: " << found.points.size() << ", required: " << min_found_interpolated
            << " out of: " << segment.interpolated.points.size();
        return {};
    }
}

static std::size_t count_hits(const bboxes_t& bboxes, const multi_point_t& stations, const multi_point_t& ways)
{
    if (stations.points.size() == 0)
    {
        return 0;
    }

    std::unordered_set<int> hits;
    for (const auto& s : boost::join(stations.points, ways.points))
    {
        const auto it{ boost::range::find_if(bboxes, [&s](const bbox_t& bbox) { return contains(bbox, s); }) };
        const auto idx{ std::distance(begin(bboxes), it) };
        hits.insert(idx);
    }
    return hits.size();
}

std::optional<kooling::datamodel::transport> rail::detect(const segment_t& segment, const osm_t& osm) const
{
    using namespace boost::adaptors;

    const auto segment_coords{ segment.range | transformed([](const auto& dp) { return dp.coord; }) };

    PLOG_DEBUG << segment.id << ": is_rail - station and way osm query start";
    osm::interface::rail_station_features stations;
    osm::interface::rail_way_features ways;
    for (const auto& bbox : kooling::geo::make_bounding_box(segment_coords, d_bbox_max_diag))
    {
        stations += osm->check_rail_stations(add_border(bbox, d_params.vicinity));
        ways += osm->check_rail_ways(bbox);
    }
    PLOG_DEBUG << segment.id << ": is_rail - station and way osm query end";

    const auto zones{ kooling::geo::make_bounding_box(boost::make_iterator_range(segment.interpolated.points), distance_t::from_m(100)) };

    const auto min_found_dp{ percent(segment.nof_dps, d_params.vicinity_pct) };
    const auto min_found_interpolated{ percent(segment.interpolated.points.size(), d_params.vicinity_pct) };
    const auto slow_dps{ segment.range
        | filtered([&speed = d_params.low_speed](const sensor_data_point_t& dp) { return dp.speed <= speed; })
        | transformed([](const auto& dp) { return dp.coord; }) };

    int train_hits{};
    if (const auto train_stations{ check_stations(segment, slow_dps, stations.train, d_params.train.vicinity, d_params.train.min_found, +transport_t::train) };
        !train_stations.points.empty())
    {
        if (const auto train_ways{ check_ways(segment, segment_coords, ways.train, d_params.vicinity, min_found_dp, min_found_interpolated, +transport_t::train) };
            !train_ways.points.empty())
        {
            train_hits = count_hits(zones, train_stations, train_ways);
        }
    }

    int tram_hits{};
    if (const auto tram_stations{ check_stations(segment, slow_dps, stations.tram, d_params.tram.vicinity, d_params.tram.min_found, +transport_t::tram) };
        !tram_stations.points.empty())
    {
        if (const auto tram_ways{ check_ways(segment, segment_coords, ways.tram, d_params.vicinity, min_found_dp, min_found_interpolated, +transport_t::tram) };
            !tram_ways.points.empty())
        {
            tram_hits = count_hits(zones, tram_stations, tram_ways);
        }
    }

    int subway_hits{};
    if (const auto subway_stations{ check_stations(segment, segment.interpolated.points, stations.subway, d_params.subway.vicinity, d_params.subway.min_found, +transport_t::subway) };
        !subway_stations.points.empty())
    {
        if (const auto subway_ways{ check_ways(segment, segment_coords, ways.subway, d_params.vicinity, min_found_dp, min_found_interpolated, +transport_t::subway) };
            !subway_ways.points.empty())
        {
            subway_hits = count_hits(zones, subway_stations, subway_ways);
        }
    }

    std::array<std::pair<std::size_t, transport_t>, 3> all_results{{
        { train_hits,  +transport_t::train  },
        { tram_hits,   +transport_t::tram   },
        { subway_hits, +transport_t::subway }
    }};

    const auto& best_match{ *boost::max_element(all_results) };
    if (best_match.first == 0)
    {
        PLOG_DEBUG << segment.id << ": is_rail - no";
        return {};
    }

    for (const auto& r : all_results)
    {
        PLOG_DEBUG
            << segment.id << ": is_rail (" << r.second._to_string() << ") - "
            << r.first << " (" << 100. * r.first / zones.size() << "%) zones with features";
    }

    const auto transport{
        best_match.second == +transport_t::train && segment.integrated.max_speed >= d_params.high_speed
        ? +transport_t::highspeedtrain
        : best_match.second };

    PLOG_DEBUG << segment.id << ": is_rail (" << transport << ") - yes";
    return transport;
}

} // namespace kooling::component::classifier