src/utilities/geometry/PointLatLon.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "PointLatLon.hpp" | ||
| 7 | #include "Point3d.hpp" | ||
| 8 | |||
| 9 | #include <GeographicLib/Geodesic.hpp> | ||
| 10 | #include <GeographicLib/LocalCartesian.hpp> | ||
| 11 | #include <GeographicLib/UTMUPS.hpp> | ||
| 12 | #include <memory> | ||
| 13 | |||
| 14 | namespace openstudio { | ||
| 15 | |||
| 16 | /// default constructor creates point at 0, 0, 0 | ||
| 17 | ✗ | PointLatLon::PointLatLon() : m_storage(3, 0.0) {} | |
| 18 | |||
| 19 | /// constructor with lat, lon, and height | ||
| 20 | 27 | PointLatLon::PointLatLon(double lat, double lon, double height) : m_storage(3) { | |
| 21 |
1/1✓ Branch 1 taken 27 times.
|
27 | m_storage[0] = lat; |
| 22 |
1/1✓ Branch 1 taken 27 times.
|
27 | m_storage[1] = lon; |
| 23 |
1/1✓ Branch 1 taken 27 times.
|
27 | m_storage[2] = height; |
| 24 | 27 | } | |
| 25 | |||
| 26 | /// copy constructor | ||
| 27 | ✗ | PointLatLon::PointLatLon(const PointLatLon& other) : m_storage(other.m_storage) { | |
| 28 | ✗ | m_localCartesianConverter.reset(); | |
| 29 | ✗ | } | |
| 30 | |||
| 31 | 21 | PointLatLon::PointLatLon(PointLatLon&& other) noexcept : m_storage(std::move(other.m_storage)) { | |
| 32 | 21 | m_localCartesianConverter.reset(); | |
| 33 | 21 | } | |
| 34 | |||
| 35 | ✗ | PointLatLon& PointLatLon::operator=(const openstudio::PointLatLon& other) { | |
| 36 | ✗ | if (this == &other) { | |
| 37 | ✗ | return *this; | |
| 38 | } | ||
| 39 | ✗ | m_storage = other.m_storage; | |
| 40 | ✗ | m_localCartesianConverter.reset(); | |
| 41 | ✗ | return *this; | |
| 42 | } | ||
| 43 | |||
| 44 | 2 | PointLatLon& PointLatLon::operator=(PointLatLon&& other) noexcept { | |
| 45 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (this == &other) { |
| 46 | ✗ | return *this; | |
| 47 | } | ||
| 48 | 2 | m_storage = std::move(other.m_storage); | |
| 49 | 2 | m_localCartesianConverter.reset(); | |
| 50 | 2 | return *this; | |
| 51 | } | ||
| 52 | |||
| 53 | 48 | PointLatLon::~PointLatLon() noexcept = default; | |
| 54 | |||
| 55 | /// get lat | ||
| 56 | 58 | double PointLatLon::lat() const { | |
| 57 | 58 | return m_storage[0]; | |
| 58 | } | ||
| 59 | |||
| 60 | /// get lon | ||
| 61 | 56 | double PointLatLon::lon() const { | |
| 62 | 56 | return m_storage[1]; | |
| 63 | } | ||
| 64 | |||
| 65 | /// get height | ||
| 66 | 33 | double PointLatLon::height() const { | |
| 67 | 33 | return m_storage[2]; | |
| 68 | } | ||
| 69 | |||
| 70 | /// check equality | ||
| 71 | 6 | bool PointLatLon::operator==(const PointLatLon& other) const { | |
| 72 | 6 | return (m_storage == other.m_storage); | |
| 73 | } | ||
| 74 | |||
| 75 | 11 | double PointLatLon::operator-(const PointLatLon& other) const { | |
| 76 | 11 | double s12 = 0.0; | |
| 77 |
1/1✓ Branch 1 taken 11 times.
|
11 | const GeographicLib::Geodesic& geod = GeographicLib::Geodesic::WGS84(); |
| 78 |
5/5✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✓ Branch 10 taken 11 times.
✓ Branch 13 taken 11 times.
|
11 | geod.Inverse(lat(), lon(), other.lat(), other.lon(), s12); |
| 79 | 11 | return s12; | |
| 80 | } | ||
| 81 | |||
| 82 | 12 | Point3d PointLatLon::toLocalCartesian(const PointLatLon& point) const { | |
| 83 |
1/1✓ Branch 1 taken 12 times.
|
12 | initLocalCartesianConverter(); |
| 84 | 12 | double x = 0.0; | |
| 85 | 12 | double y = 0.0; | |
| 86 | 12 | double z = 0.0; | |
| 87 |
4/4✓ Branch 2 taken 12 times.
✓ Branch 5 taken 12 times.
✓ Branch 8 taken 12 times.
✓ Branch 11 taken 12 times.
|
12 | m_localCartesianConverter->Forward(point.lat(), point.lon(), point.height(), x, y, z); |
| 88 |
1/1✓ Branch 1 taken 12 times.
|
24 | return {x, y, z}; |
| 89 | } | ||
| 90 | |||
| 91 | 1 | std::vector<Point3d> PointLatLon::toLocalCartesian(const std::vector<PointLatLon>& points) const { | |
| 92 | 1 | std::vector<Point3d> result; | |
| 93 |
1/1✓ Branch 2 taken 1 time.
|
1 | result.reserve(points.size()); |
| 94 |
2/2✓ Branch 4 taken 7 times.
✓ Branch 5 taken 1 time.
|
8 | for (const auto& point : points) { |
| 95 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
|
7 | result.push_back(toLocalCartesian(point)); |
| 96 | } | ||
| 97 | 1 | return result; | |
| 98 | ✗ | } | |
| 99 | |||
| 100 | 8 | PointLatLon PointLatLon::fromLocalCartesian(const Point3d& point) const { | |
| 101 |
1/1✓ Branch 1 taken 8 times.
|
8 | initLocalCartesianConverter(); |
| 102 | 8 | double lat = 0.0; | |
| 103 | 8 | double lon = 0.0; | |
| 104 | 8 | double h = 0.0; | |
| 105 |
4/4✓ Branch 2 taken 8 times.
✓ Branch 5 taken 8 times.
✓ Branch 8 taken 8 times.
✓ Branch 11 taken 8 times.
|
8 | m_localCartesianConverter->Reverse(point.x(), point.y(), point.z(), lat, lon, h); |
| 106 |
1/1✓ Branch 1 taken 8 times.
|
16 | return {lat, lon, h}; |
| 107 | } | ||
| 108 | |||
| 109 | 1 | std::vector<PointLatLon> PointLatLon::fromLocalCartesian(const std::vector<Point3d>& points) const { | |
| 110 | 1 | std::vector<PointLatLon> result; | |
| 111 |
1/1✓ Branch 2 taken 1 time.
|
1 | result.reserve(points.size()); |
| 112 |
2/2✓ Branch 4 taken 7 times.
✓ Branch 5 taken 1 time.
|
8 | for (const auto& point : points) { |
| 113 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
|
7 | result.push_back(fromLocalCartesian(point)); |
| 114 | } | ||
| 115 | 1 | return result; | |
| 116 | ✗ | } | |
| 117 | |||
| 118 | ✗ | int PointLatLon::utmZone() const { | |
| 119 | ✗ | return GeographicLib::UTMUPS::StandardZone(lat(), lon()); | |
| 120 | } | ||
| 121 | |||
| 122 | ✗ | Point3d PointLatLon::toUTM(const PointLatLon& point) const { | |
| 123 | ✗ | int zone = 0; | |
| 124 | ✗ | bool northp = false; | |
| 125 | ✗ | double x = 0.0; | |
| 126 | ✗ | double y = 0.0; | |
| 127 | ✗ | double gamma = 0.0; | |
| 128 | ✗ | double k = 0.0; | |
| 129 | ✗ | GeographicLib::UTMUPS::Forward(point.lat(), point.lon(), zone, northp, x, y, gamma, k, utmZone()); | |
| 130 | ✗ | return {x, y, point.height()}; | |
| 131 | } | ||
| 132 | |||
| 133 | ✗ | std::vector<Point3d> PointLatLon::toUTM(const std::vector<PointLatLon>& points) const { | |
| 134 | ✗ | std::vector<Point3d> result; | |
| 135 | ✗ | result.reserve(points.size()); | |
| 136 | ✗ | for (const auto& point : points) { | |
| 137 | ✗ | result.push_back(toUTM(point)); | |
| 138 | } | ||
| 139 | ✗ | return result; | |
| 140 | ✗ | } | |
| 141 | |||
| 142 | ✗ | PointLatLon PointLatLon::fromUTM(const Point3d& point) const { | |
| 143 | ✗ | int zone = 0; | |
| 144 | ✗ | bool northp = false; | |
| 145 | { | ||
| 146 | ✗ | double x = 0.0; | |
| 147 | ✗ | double y = 0.0; | |
| 148 | ✗ | double gamma = 0.0; | |
| 149 | ✗ | double k = 0.0; | |
| 150 | ✗ | GeographicLib::UTMUPS::Forward(lat(), lon(), zone, northp, x, y, gamma, k); | |
| 151 | } | ||
| 152 | |||
| 153 | ✗ | double lat = 0.0; | |
| 154 | ✗ | double lon = 0.0; | |
| 155 | ✗ | double gamma = 0.0; | |
| 156 | ✗ | double k = 0.0; | |
| 157 | ✗ | GeographicLib::UTMUPS::Reverse(zone, northp, point.x(), point.y(), lat, lon, gamma, k); | |
| 158 | ✗ | return {lat, lon, point.z()}; | |
| 159 | } | ||
| 160 | |||
| 161 | ✗ | std::vector<PointLatLon> PointLatLon::fromUTM(const std::vector<Point3d>& points) const { | |
| 162 | ✗ | std::vector<PointLatLon> result; | |
| 163 | ✗ | result.reserve(points.size()); | |
| 164 | ✗ | for (const auto& point : points) { | |
| 165 | ✗ | result.push_back(fromUTM(point)); | |
| 166 | } | ||
| 167 | ✗ | return result; | |
| 168 | ✗ | } | |
| 169 | |||
| 170 | 20 | void PointLatLon::initLocalCartesianConverter() const { | |
| 171 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 18 times.
|
20 | if (!m_localCartesianConverter) { |
| 172 |
4/4✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
|
2 | m_localCartesianConverter = std::make_unique<GeographicLib::LocalCartesian>(lat(), lon(), height()); |
| 173 | } | ||
| 174 | 20 | } | |
| 175 | |||
| 176 | /// ostream operator | ||
| 177 | ✗ | std::ostream& operator<<(std::ostream& os, const PointLatLon& point) { | |
| 178 | ✗ | os << "[" << point.lat() << ", " << point.lon() << ", " << point.height() << "]"; | |
| 179 | ✗ | return os; | |
| 180 | } | ||
| 181 | |||
| 182 | /// ostream operator | ||
| 183 | ✗ | std::ostream& operator<<(std::ostream& os, const std::vector<PointLatLon>& pointVector) { | |
| 184 | ✗ | os << "["; | |
| 185 | ✗ | for (unsigned i = 0; i < pointVector.size(); ++i) { | |
| 186 | ✗ | os << pointVector[i]; | |
| 187 | ✗ | if (i < pointVector.size() - 1) { | |
| 188 | ✗ | os << ", "; | |
| 189 | } | ||
| 190 | } | ||
| 191 | ✗ | os << "]"; | |
| 192 | ✗ | return os; | |
| 193 | } | ||
| 194 | |||
| 195 | 3 | double getDistanceLatLon(double lat1, double lon1, double lat2, double lon2) { | |
| 196 |
1/1✓ Branch 1 taken 3 times.
|
3 | const PointLatLon p1(lat1, lon1); |
| 197 |
1/1✓ Branch 1 taken 3 times.
|
3 | const PointLatLon p2(lat2, lon2); |
| 198 |
1/1✓ Branch 1 taken 3 times.
|
6 | return (p1 - p2); |
| 199 | 3 | } | |
| 200 | |||
| 201 | } // namespace openstudio | ||
| 202 |