swift
Loading...
Searching...
No Matches
geoobjectlist.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_GEO_GEOOBJECTLIST_H
7#define SWIFT_MISC_GEO_GEOOBJECTLIST_H
8
9#include <tuple>
10
11#include <QList>
12
15#include "misc/pq/length.h"
16#include "misc/sequence.h"
18
19namespace swift::misc::geo
20{
22 template <class OBJ, class CONTAINER>
24 {
25 public:
27 using MinMaxAverageHeight = std::tuple<aviation::CAltitude, aviation::CAltitude, aviation::CAltitude, int>;
28
32 CONTAINER findWithinRange(const ICoordinateGeodetic &coordinate,
33 const physical_quantities::CLength &range) const
34 {
35 return this->container().findBy(
36 [&](const OBJ &geoObj) { return calculateGreatCircleDistance(geoObj, coordinate) <= range; });
37 }
38
42 CONTAINER findOutsideRange(const ICoordinateGeodetic &coordinate,
43 const physical_quantities::CLength &range) const
44 {
45 return this->container().findBy(
46 [&](const OBJ &geoObj) { return calculateGreatCircleDistance(geoObj, coordinate) > range; });
47 }
48
51 const physical_quantities::CLength &range) const
52 {
53 return this->container().findFirstByOrDefault(
54 [&](const OBJ &geoObj) { return calculateGreatCircleDistance(geoObj, coordinate) <= range; });
55 }
56
58 CONTAINER findWithGeodeticMSLHeight() const
59 {
60 return this->container().findBy(&OBJ::hasMSLGeodeticHeight, true);
61 }
62
65 const physical_quantities::CLength &range) const
66 {
67 return this->container().containsBy([&](const OBJ &geoObj) {
69 return d <= range;
70 });
71 }
72
75 const physical_quantities::CLength &range) const
76 {
77 return this->container().containsBy([&](const OBJ &geoObj) {
79 return d > range;
80 });
81 }
82
85 {
86 return this->container().containsBy([&](const ICoordinateGeodetic &geoObj) { return geoObj.isNull(); });
87 }
88
91 {
92 return this->container().containsBy(
93 [&](const ICoordinateGeodetic &geoObj) { return geoObj.isNull() || geoObj.isGeodeticHeightNull(); });
94 }
95
98 {
101 if (this->container().isEmpty())
102 {
103 return stats;
104 } // avoid div by zero
105 int count = 0;
106 double avgFt = 0;
107 for (const OBJ &obj : this->container())
108 {
109 if (!obj.hasMSLGeodeticHeight())
110 {
111 continue;
112 }
113 const aviation::CAltitude alt = obj.geodeticHeight();
114 if (std::get<0>(stats).isNull() || std::get<0>(stats) > alt)
115 {
116 std::get<0>(stats) = alt; // min.
117 }
118 if (std::get<1>(stats).isNull() || std::get<1>(stats) < alt)
119 {
120 std::get<1>(stats) = alt; // max.
121 }
122 avgFt += alt.value(physical_quantities::CLengthUnit::ft()); // add up
123 count++;
124 }
125
126 if (count > 0)
127 {
128 std::get<2>(stats) = aviation::CAltitude(avgFt / count, aviation::CAltitude::MeanSeaLevel,
130 }
131 std::get<3>(stats) = count;
132 return stats;
133 }
134
137 {
138 if (this->container().isEmpty())
139 {
141 }
143 for (const OBJ &obj : this->container())
144 {
145 if (!obj.hasMSLGeodeticHeight())
146 {
147 continue;
148 }
149 const aviation::CAltitude alt = obj.geodeticHeight();
150 if (max.isNull() || alt > max)
151 {
152 max = alt;
153 }
154 }
155 return max;
156 }
157
160 {
161 const int size = this->container().size();
162 const CONTAINER copy = this->container().findOutsideRange(coordinate, range);
163 const int d = size - copy.size();
164 if (d > 0)
165 {
166 *this = copy;
167 }
168 return d;
169 }
170
173 {
174 const int size = this->container().size();
175 const CONTAINER copy = this->container().findWithinRange(coordinate, range);
176 const int d = size - copy.size();
177 if (d > 0)
178 {
179 *this = copy;
180 }
181 return d;
182 }
183
186 {
187 const int size = this->container().size();
188 const CONTAINER copy = this->findWithGeodeticMSLHeight();
189 const int d = size - copy.size();
190 if (d > 0)
191 {
192 *this = copy;
193 }
194 return d;
195 }
196
198 CONTAINER findClosest(int number, const ICoordinateGeodetic &coordinate) const
199 {
200 CONTAINER closest = this->container().partiallySorted(number, [&](const OBJ &a, const OBJ &b) {
201 return calculateEuclideanDistanceSquared(a, coordinate) <
202 calculateEuclideanDistanceSquared(b, coordinate);
203 });
204 closest.truncate(number);
205 return closest;
206 }
207
209 CONTAINER findFarthest(int number, const ICoordinateGeodetic &coordinate) const
210 {
211 CONTAINER farthest = this->container().partiallySorted(number, [&](const OBJ &a, const OBJ &b) {
212 return calculateEuclideanDistanceSquared(a, coordinate) >
213 calculateEuclideanDistanceSquared(b, coordinate);
214 });
215 farthest.truncate(number);
216 return farthest;
217 }
218
221 const physical_quantities::CLength &range) const
222 {
223 OBJ closest;
225 for (const OBJ &obj : this->container())
226 {
228 if (d > range)
229 {
230 continue;
231 }
232 if (distance.isNull() || distance > d)
233 {
234 distance = d;
235 closest = obj;
236 }
237 }
238 return closest;
239 }
240
243 {
244 this->container().sort([&](const OBJ &a, const OBJ &b) {
245 return calculateEuclideanDistanceSquared(a, coordinate) <
246 calculateEuclideanDistanceSquared(b, coordinate);
247 });
248 }
249
251 CONTAINER sortedByEuclideanDistanceSquared(const ICoordinateGeodetic &coordinate) const
252 {
253 CONTAINER copy(this->container());
254 copy.sortByEuclideanDistanceSquared(coordinate);
255 return copy;
256 }
257
258 protected:
260 IGeoObjectList() {} // NOLINT(modernize-use-equals-default)
261
263 const CONTAINER &container() const { return static_cast<const CONTAINER &>(*this); }
264
266 CONTAINER &container() { return static_cast<CONTAINER &>(*this); }
267 };
268
270 template <class OBJ, class CONTAINER>
272 {
273 public:
275 void sortByRange(const ICoordinateGeodetic &position, bool updateValues)
276 {
277 if (updateValues)
278 {
280 }
281 this->container().sort(
282 [&](const OBJ &a, const OBJ &b) { return a.getRelativeDistance() < b.getRelativeDistance(); });
283 }
284
288 {
289 this->container().sort(
290 [&](const OBJ &a, const OBJ &b) { return a.getRelativeDistance() < b.getRelativeDistance(); });
291 }
292
295 {
296 this->container().partiallySort(
297 number, [&](const OBJ &a, const OBJ &b) { return a.getRelativeDistance() < b.getRelativeDistance(); });
298 }
299
301 CONTAINER getClosestObjects(int number) const
302 {
303 if (number < 1)
304 {
305 return CONTAINER();
306 }
307 if (this->container().size() >= number)
308 {
309 return (this->container());
310 }
311 CONTAINER closest(this->container());
312 closest.partiallySortByDistanceToReferencePosition(number);
313 Q_ASSERT_X(closest.size() <= number, Q_FUNC_INFO, "size exceeded");
314 return closest;
315 }
316
319 bool updateValues)
320 {
321 this->container().removeIf([&](OBJ &geoObj) {
322 return updateValues ? geoObj.calculcateAndUpdateRelativeDistanceAndBearing(position) > maxDistance :
323 geoObj.calculateGreatCircleDistance(position) > maxDistance;
324 });
325 }
326
329 {
330 for (OBJ &geoObj : this->container())
331 {
332 geoObj.calculcateAndUpdateRelativeDistanceAndBearing(position);
333 }
334 }
335
336 protected:
338 IGeoObjectWithRelativePositionList() {} // NOLINT(modernize-use-equals-default)
339 };
340} // namespace swift::misc::geo
341
342#endif // SWIFT_MISC_GEO_GEOOBJECTLIST_H
Altitude as used in aviation, can be AGL or MSL altitude.
Definition altitude.h:52
static const CAltitude & null()
Null altitude (MSL).
Definition altitude.cpp:511
Geodetic coordinate, a position in 3D space relative to the reference geoid.
physical_quantities::CLength calculateGreatCircleDistance(const ICoordinateGeodetic &otherCoordinate) const
Great circle distance.
virtual bool isNull() const
Is null, means vector x, y, z == 0.
bool isGeodeticHeightNull() const
Geodetic height null?
CONTAINER findOutsideRange(const ICoordinateGeodetic &coordinate, const physical_quantities::CLength &range) const
Find 0..n objects outside range of given coordinate.
bool containsNullPosition() const
Any NULL position?
CONTAINER sortedByEuclideanDistanceSquared(const ICoordinateGeodetic &coordinate) const
Sorted by distance.
void sortByEuclideanDistanceSquared(const ICoordinateGeodetic &coordinate)
Sort by distance.
int removeWithoutGeodeticHeight()
Remove if there is no geodetic height.
OBJ findFirstWithinRangeOrDefault(const ICoordinateGeodetic &coordinate, const physical_quantities::CLength &range) const
Find first in range.
bool containsObjectInRange(const ICoordinateGeodetic &coordinate, const physical_quantities::CLength &range) const
Any object in range?
CONTAINER & container()
Container.
CONTAINER findWithinRange(const ICoordinateGeodetic &coordinate, const physical_quantities::CLength &range) const
Find 0..n objects within range of given coordinate.
OBJ findClosestWithinRange(const ICoordinateGeodetic &coordinate, const physical_quantities::CLength &range) const
Find closest within range to the given coordinate.
bool containsObjectOutsideRange(const ICoordinateGeodetic &coordinate, const physical_quantities::CLength &range) const
Any object in range?
const CONTAINER & container() const
Container.
int removeInsideRange(const ICoordinateGeodetic &coordinate, const physical_quantities::CLength &range)
Remove inside range.
int removeOutsideRange(const ICoordinateGeodetic &coordinate, const physical_quantities::CLength &range)
Remove outside range.
bool containsNullPositionOrHeight() const
Any NULL position or NULL height.
aviation::CAltitude findMaxHeight() const
Find min/max/average height.
MinMaxAverageHeight findMinMaxAverageHeight() const
Find min/max/average height.
CONTAINER findWithGeodeticMSLHeight() const
Elements with geodetic height (only MSL).
CONTAINER findClosest(int number, const ICoordinateGeodetic &coordinate) const
Find 0..n objects closest to the given coordinate.
CONTAINER findFarthest(int number, const ICoordinateGeodetic &coordinate) const
Find 0..n objects farthest to the given coordinate.
std::tuple< aviation::CAltitude, aviation::CAltitude, aviation::CAltitude, int > MinMaxAverageHeight
For statistics.
void calculcateAndUpdateRelativeDistanceAndBearing(const ICoordinateGeodetic &position)
Calculate distances.
void partiallySortByDistanceToReferencePosition(int number)
Sort the first n closest objects.
void sortByRange(const ICoordinateGeodetic &position, bool updateValues)
Calculate distances, then sort by range.
void sortByDistanceToReferencePosition()
If distance is already set, just sort container.
CONTAINER getClosestObjects(int number) const
Get n closest objects.
void removeIfOutsideRange(const ICoordinateGeodetic &position, const physical_quantities::CLength &maxDistance, bool updateValues)
Calculate distances, remove if outside range.
Physical unit length (length).
Definition length.h:18
static CLengthUnit ft()
Foot ft.
Definition units.h:157
double value(MU unit) const
Value in given unit.