summaryrefslogtreecommitdiffstats
path: root/server/src/geo.cppm
blob: 9101af823d1eb5dc4678a082849c50f81a848e85 (about) (plain) (blame)
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
module;

#include <boost/geometry.hpp>

export module routemon:geo;

export namespace bgeo = boost::geometry;

export namespace routemon::geo {

using point =
  bgeo::model::point<double, 2, bgeo::cs::spherical_equatorial<bgeo::degree>>;
using linestring = bgeo::model::linestring<point>;
using box = bgeo::model::box<point>;
using stype = bgeo::srs::spheroid<double>;
using vincenty_strategy = bgeo::strategy::distance::vincenty<stype>;

auto split_linestring_with_overlap_segments(
  linestring const& ls, double max_split_distance_m,
  std::vector<linestring>& append_to) -> void
{
  if (bgeo::is_empty(ls))
    return;

  auto current_ls = linestring{};
  auto current_ls_length = 0.0;
  auto previous = std::optional<point>{};
  bgeo::for_each_point(
    ls,
    [&](point p) -> void
    {
      bgeo::append(current_ls, p);
      if (previous)
      {
        auto d = bgeo::distance(*previous, p, vincenty_strategy());
        current_ls_length += d;
        if (current_ls_length > max_split_distance_m)
        {
          append_to.push_back(std::move(current_ls));
          current_ls = linestring{*previous, p};
          current_ls_length = d;
        }
      }
      previous = p;
    });
  append_to.emplace_back(std::move(current_ls));
}

} // namespace routemon::geo