|
16 | 16 | * Low level geometry functions and types. |
17 | 17 | */ |
18 | 18 |
|
| 19 | +#include "reprojection.hpp" |
| 20 | + |
19 | 21 | #include <osmium/geom/coordinates.hpp> |
| 22 | +#include <osmium/osm/node_ref_list.hpp> |
| 23 | + |
| 24 | +#include <initializer_list> |
| 25 | +#include <ostream> |
| 26 | +#include <vector> |
20 | 27 |
|
21 | 28 | namespace geom { |
22 | 29 |
|
| 30 | +/// Calculate the Euclidean distance between two coordinates |
23 | 31 | double distance(osmium::geom::Coordinates p1, |
24 | 32 | osmium::geom::Coordinates p2) noexcept; |
25 | 33 |
|
26 | 34 | osmium::geom::Coordinates interpolate(osmium::geom::Coordinates p1, |
27 | 35 | osmium::geom::Coordinates p2, |
28 | 36 | double frac) noexcept; |
29 | 37 |
|
| 38 | +class linestring_t |
| 39 | +{ |
| 40 | +public: |
| 41 | + linestring_t() = default; |
| 42 | + |
| 43 | + linestring_t(std::initializer_list<osmium::geom::Coordinates> coords); |
| 44 | + |
| 45 | + /** |
| 46 | + * Construct a linestring from a way node list. Nodes without location |
| 47 | + * are ignored. Consecutive nodes with the same location will only end |
| 48 | + * up once in the linestring. |
| 49 | + * |
| 50 | + * The resulting linestring will either be empty or have at least two |
| 51 | + * points in it. |
| 52 | + * |
| 53 | + * \param nodes The input nodes. |
| 54 | + * \param proj The projection used to project all coordinates. |
| 55 | + */ |
| 56 | + linestring_t(osmium::NodeRefList const &nodes, reprojection const &proj); |
| 57 | + |
| 58 | + using iterator = std::vector<osmium::geom::Coordinates>::iterator; |
| 59 | + |
| 60 | + using const_iterator = |
| 61 | + std::vector<osmium::geom::Coordinates>::const_iterator; |
| 62 | + |
| 63 | + bool empty() const noexcept { return m_coordinates.empty(); } |
| 64 | + |
| 65 | + std::size_t size() const noexcept { return m_coordinates.size(); } |
| 66 | + |
| 67 | + void clear() noexcept { m_coordinates.clear(); } |
| 68 | + |
| 69 | + void add_point(osmium::geom::Coordinates coordinates) |
| 70 | + { |
| 71 | + m_coordinates.emplace_back(std::move(coordinates)); |
| 72 | + } |
| 73 | + |
| 74 | + iterator begin() noexcept { return m_coordinates.begin(); } |
| 75 | + |
| 76 | + iterator end() noexcept { return m_coordinates.end(); } |
| 77 | + |
| 78 | + const_iterator begin() const noexcept { return m_coordinates.cbegin(); } |
| 79 | + |
| 80 | + const_iterator end() const noexcept { return m_coordinates.cend(); } |
| 81 | + |
| 82 | + const_iterator cbegin() const noexcept { return m_coordinates.cbegin(); } |
| 83 | + |
| 84 | + const_iterator cend() const noexcept { return m_coordinates.cend(); } |
| 85 | + |
| 86 | + osmium::geom::Coordinates &operator[](std::size_t n) noexcept |
| 87 | + { |
| 88 | + return m_coordinates[n]; |
| 89 | + } |
| 90 | + |
| 91 | + osmium::geom::Coordinates operator[](std::size_t n) const noexcept |
| 92 | + { |
| 93 | + return m_coordinates[n]; |
| 94 | + } |
| 95 | + |
| 96 | +private: |
| 97 | + std::vector<osmium::geom::Coordinates> m_coordinates; |
| 98 | + |
| 99 | +}; // class linestring_t |
| 100 | + |
| 101 | +/// Output a linestring in WKT format. Used for debugging. |
| 102 | +template <typename TChar, typename TTraits> |
| 103 | +std::basic_ostream<TChar, TTraits> & |
| 104 | +operator<<(std::basic_ostream<TChar, TTraits> &out, const linestring_t &line) |
| 105 | +{ |
| 106 | + out << "LINESTRING("; |
| 107 | + |
| 108 | + bool first = true; |
| 109 | + for (auto const coord : line) { |
| 110 | + if (first) { |
| 111 | + first = false; |
| 112 | + } else { |
| 113 | + out << ','; |
| 114 | + } |
| 115 | + out << coord.x << ' ' << coord.y; |
| 116 | + } |
| 117 | + |
| 118 | + return out << ')'; |
| 119 | +} |
| 120 | + |
30 | 121 | } // namespace geom |
31 | 122 |
|
32 | 123 | #endif // OSM2PGSQL_GEOM_HPP |
0 commit comments