Ocean
Loading...
Searching...
No Matches
GPSTracker.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_DEVICES_GPS_TRACKER_H
9#define META_OCEAN_DEVICES_GPS_TRACKER_H
10
13
15
16#include "ocean/math/Vector2.h"
17
18namespace Ocean
19{
20
21namespace Devices
22{
23
24// Forward declaration.
25class GPSTracker;
26
27/**
28 * Definition of a smart object reference for a GPS tracker.
29 * @see GPSTracker.
30 * @ingroup devices
31 */
33
34/**
35 * This class is the base class for all gps tracker objects.
36 * @ingroup devices
37 */
38class OCEAN_DEVICES_EXPORT GPSTracker : virtual public Tracker
39{
40 public:
41
42 /**
43 * This class implements a container for a GPS location.
44 */
46 {
47 public:
48
49 /**
50 * Creates an invalid location.
51 */
52 inline Location();
53
54 /**
55 * Creates a new GPS location object.
56 * @param latitude The position's latitude, in degree, always valid, with range [-90, 90]
57 * @param longitude The position's longitude, in degree, must be valid, with range [-180, 180]
58 * @param altitude The position's altitude, in meter, NumericF::minValue() if unknown
59 * @param direction The travel direction of the device, relative to north, in degree, north is 0 degree, east is 90 degree, with range [0, 360], -1 if unknown
60 * @param speed The device's speed, in meter per second, with range [0, infinity), -1 if unknown
61 * @param accuracy The horizontal accuracy as radius, in meter, with range [0, infinity), -1 if unknown
62 * @param altitudeAccuracy The vertical accuracy in meter, with range [0, infinity), -1 if unknown
63 * @param directionAccuracy The direction accuracy, in degree, with range [0, 360], -1 if unknown
64 * @param speedAccuracy The speed accuracy, in meter per second, with range [0, infinity), -1 if unknown
65 */
66 inline Location(const double latitude, const double longitude, const float altitude = NumericF::minValue(), const float direction = -1.0f, const float speed = -1.0f, const float accuracy = -1.0f, const float altitudeAccuracy = -1.0f, const float directionAccuracy = -1.0f, const float speedAccuracy = -1.0f);
67
68 /**
69 * Returns the latitude of the GPS position.
70 * @return The position's latitude, in degree, always valid, with range [-90, 90].
71 */
72 inline double latitude() const;
73
74 /**
75 * Returns the longitude of the GPS position.
76 * @return The position's longitude, in degree, always valid, with range [-180, 180].
77 */
78 inline double longitude() const;
79
80 /**
81 * Returns the altitude of the GPS position.
82 * @return The position's altitude, in meter, NumericF::minValue() if unknown
83 */
84 inline float altitude() const;
85
86 /**
87 * Returns the device's travel direction not the orientation of the device.
88 * @return The travel direction of the device, relative to north, in degree, north is 0 degree, east is 90 degree, with range [0, 360], -1 if unknown.
89 */
90 inline float direction() const;
91
92 /**
93 * Returns the speed of the device.
94 * @return The device's speed, in meter per second, with range [0, infinity), -1 if unknown.
95 */
96 inline float speed() const;
97
98 /**
99 * Returns the accuracy of latitude and longitude.
100 * @return The horizontal accuracy as radius, in meter, with range [0, infinity), -1 if unknown.
101 */
102 inline float accuracy() const;
103
104 /**
105 * Returns the accuracy of the altitude.
106 * @return The vertical accuracy in meter, with range [0, infinity), -1 if unknown
107 */
108 inline float altitudeAccuracy() const;
109
110 /**
111 * Returns the accuracy of the direction.
112 * @return The direction accuracy, in degree, with range [0, 180], -1 if unknown
113 */
114 inline float directionAccuracy() const;
115
116 /**
117 * Returns the accuracy of the speed value.
118 * @return The speed accuracy, in meter per second, with range [0, infinity), -1 if unknown.
119 */
120 inline float speedAccuracy() const;
121
122 /**
123 * Returns whether this location holds valid data.
124 * @return True, if so
125 */
126 inline bool isValid() const;
127
128 protected:
129
130 /// The latitude of the GPS position, in degree, always valid, with range [-90, 90], NumericD::minValue() if invalid
131 double latitude_ = NumericD::minValue();
132
133 /// The longitude of the GPS position, in degree, always valid, with range [-180, 180], NumericD::minValue() if invalid
134 double longitude_ = NumericD::minValue();
135
136 /// The altitude/height of the GPS position, in meter, NumericF::minValue() if unknown
137 float altitude_ = NumericF::minValue();
138
139 /// The device's travel direction not the orientation of the device, relative to north, in degree, north is 0 degree, east is 90 degree, with range [0, 360], -1 if unknown.
140 float direction_ = -1.0f;
141
142 /// The speed of the device, in meter per second, with range [0, infinity), -1 if unknown.
143 float speed_ = -1.0f;
144
145 /// The horizontal position accuracy as radius, in meter, with range [0, infinity), -1 if unknown.
146 float accuracy_ = -1.0f;
147
148 /// The altitude accuracy, in meter, with range [0, infinity), -1 if unknown
149 float altitudeAccuracy_ = -1.0f;
150
151 /// The direction accuracy, in degree, with range [0, 180], -1 if unknown
152 float directionAccuracy_ = -1.0f;
153
154 /// The accuracy of the speed value, in meter per second, with range [0, infinity), -1 if unknown.
155 float speedAccuracy_ = -1.0f;
156 };
157
158 /**
159 * Definition of a vector holding GPS data values.
160 */
162
163 /**
164 * Definition of a sample holding GPS measurements.
165 */
166 class OCEAN_DEVICES_EXPORT GPSTrackerSample : virtual public Tracker::TrackerSample
167 {
168 public:
169
170 /**
171 * Creates a new GPS sample.
172 * @param timestamp Sample timestamp
173 * @param referenceSystem Tracking reference system used by the underlying tracker
174 * @param objectIds The ids of the individual GPS measurements
175 * @param locations The GPS locations, one for each object id
176 * @param metadata Optional metadata of the new sample
177 */
178 GPSTrackerSample(const Timestamp& timestamp, const ReferenceSystem referenceSystem, const ObjectIds& objectIds, const Locations& locations, const Metadata& metadata = Metadata());
179
180 /**
181 * Creates a new GPS sample.
182 * @param timestamp Sample timestamp
183 * @param referenceSystem Tracking reference system used by the underlying tracker
184 * @param objectIds The ids of the individual GPS measurements
185 * @param locations The GPS locations, one for each object id
186 * @param metadata Optional metadata of the new sample
187 */
188 GPSTrackerSample(const Timestamp& timestamp, const ReferenceSystem referenceSystem, ObjectIds&& objectIds, Locations&& locations, Metadata&& metadata = Metadata());
189
190 /**
191 * Returns the GPS locations.
192 * @return The samples GPS locations, one for each object id.
193 */
194 inline const Locations& locations() const;
195
196 protected:
197
198 /// The GPS locations, one for each object id.
200 };
201
202 /**
203 * Definition of a smart object reference for GPS tracker samples.
204 */
206
207 public:
208
209 /**
210 * Returns the device type of this tracker.
211 * @return Device type
212 */
213 static inline DeviceType deviceTypeGPSTracker();
214
215 /**
216 * Parses a string with a GPS location.
217 * The given string must have one of the following patterns:
218 * <pre>
219 * GPS Location {latitude}, {longitude}
220 * GPS Location {latitude}, {longitude}, {altitude}
221 * e.g.,
222 * GPS Location 37.48507, -122.14829
223 * GPS Location 37.48507, -122.14829, 10.0
224 * </pre>
225 * @param gpsString The string to be parsed
226 * @param latitude The resulting latitude value
227 * @param longitude The resulting longitude value
228 * @param altitude Optional resulting altitude; nullptr to not allow an altitude value in a valid input string
229 * @return True, if succeeded; Also True, if the 'altitude' parameter is not nullptr but no altitude value is present in 'gpsString'
230 */
231 static bool parseGPSLocation(const std::string& gpsString, double& latitude, double& longitude, double* altitude = nullptr);
232
233 /**
234 * Decodes all GPS locations from a polyline.
235 * Polyline encoding is a lossy compression algorithm that allows you to store a series of coordinates as a single string.<br>
236 * More details: https://developers.google.com/maps/documentation/utilities/polylinealgorithm
237 * @param polyline The polyline to decode, must be valid
238 * @param precision The precision of decimal places the given polyline has, with range [1, 8]
239 * @param locations The resulting individual locations
240 * @param unescapeBackslash True, to unescape two consecutive backslash in the polyline to one backslash; False, to use the polyline as it is
241 * @return True, if succeeded
242 */
243 static bool decodePolyline(const std::string& polyline, const unsigned int precision, std::vector<Location>& locations, const bool unescapeBackslash = false);
244
245 /**
246 * Calculates the distance between two GPS locations in meters, the location's latitude is not considered.
247 * The resulting distance is an approximation based on a perfect sphere.
248 * @param locationA The first GPS location, must be valid
249 * @param locationB The second GPS location, must be valid
250 * @param earthRadius The radius of the earth in meter, with range (0, infinity)
251 * @return The approximated distance between both locations, in meter, with range [0, earthRadius * PI]
252 */
253 static double approximatedDistanceBetweenLocations(const Location& locationA, const Location& locationB, const double earthRadius = 6378135.0);
254
255 protected:
256
257 /**
258 * Creates a new GPS tracker object.
259 * @param name The name of the gps tracker
260 */
261 explicit GPSTracker(const std::string& name);
262
263 /**
264 * Destructs a GPS tracker object.
265 */
266 ~GPSTracker() override;
267
268 /**
269 * Decodes one value from a polyline.
270 * @param polyline The polyline from which the value will be decoded, must be valid
271 * @param position The current position within the polyline, will be moved to the next value position, with range [0, polyline.length() - 1]
272 * @param integerValue The resulting value with integer precision (not yet normalized)
273 * @param unescapeBackslash True, to unescape two consecutive backslash in the polyline to one backslash; False, to use the polyline as it is
274 * @return True, if succeeded
275 * @see decodePolyline().
276 */
277 static bool decodePolylineValue(const std::string& polyline, size_t& position, int32_t& integerValue, const bool unescapeBackslash);
278};
279
281{
282 ocean_assert(!isValid());
283}
284
285inline GPSTracker::Location::Location(const double latitude, const double longitude, const float altitude, const float direction, const float speed, const float accuracy, const float altitudeAccuracy, const float directionAccuracy, const float speedAccuracy) :
286 latitude_(latitude),
287 longitude_(longitude),
288 altitude_(altitude),
289 direction_(direction),
290 speed_(speed),
291 accuracy_(accuracy),
292 altitudeAccuracy_(altitudeAccuracy),
293 directionAccuracy_(directionAccuracy),
294 speedAccuracy_(speedAccuracy)
295{
296 ocean_assert(latitude_ >= -90.0 && latitude_ <= 90.0);
297 ocean_assert(longitude_ >= -180.0 && longitude_ <= 180.0);
298 ocean_assert((direction_ >= 0.0f && direction_ <= 360.0f) || direction_ == -1.0f);
299 ocean_assert((directionAccuracy_ >= 0.0f && directionAccuracy_ <= 180.0f) || directionAccuracy_ == -1.0f);
300
301 ocean_assert(isValid());
302}
303
305{
306 return latitude_;
307}
308
310{
311 return longitude_;
312}
313
315{
316 return altitude_;
317}
318
320{
321 return direction_;
322}
323
324inline float GPSTracker::Location::speed() const
325{
326 return speed_;
327}
328
330{
331 return accuracy_;
332}
333
335{
336 return altitudeAccuracy_;
337}
338
340{
341 return directionAccuracy_;
342}
343
345{
346 return speedAccuracy_;
347}
348
350{
351 ocean_assert((direction_ >= 0.0f && direction_ <= 360.0f) || direction_ == -1.0f);
352 ocean_assert((directionAccuracy_ >= 0.0f && directionAccuracy_ <= 180.0f) || directionAccuracy_ == -1.0f);
353
354 return NumericD::isInsideRange(-90.0, latitude_, 90.0) && NumericD::isInsideRange(-180.0, longitude_, 180.0);
355}
356
358{
359 return locations_;
360}
361
366
367}
368
369}
370
371#endif // META_OCEAN_DEVICES_GPS_TRACKER_H
Definition of a class holding the major and minor device type.
Definition devices/Device.h:62
@ DEVICE_TRACKER
Tracker device.
Definition devices/Device.h:46
Definition of a sample holding GPS measurements.
Definition GPSTracker.h:167
GPSTrackerSample(const Timestamp &timestamp, const ReferenceSystem referenceSystem, const ObjectIds &objectIds, const Locations &locations, const Metadata &metadata=Metadata())
Creates a new GPS sample.
Locations locations_
The GPS locations, one for each object id.
Definition GPSTracker.h:199
GPSTrackerSample(const Timestamp &timestamp, const ReferenceSystem referenceSystem, ObjectIds &&objectIds, Locations &&locations, Metadata &&metadata=Metadata())
Creates a new GPS sample.
const Locations & locations() const
Returns the GPS locations.
Definition GPSTracker.h:357
This class implements a container for a GPS location.
Definition GPSTracker.h:46
float accuracy() const
Returns the accuracy of latitude and longitude.
Definition GPSTracker.h:329
double latitude_
The latitude of the GPS position, in degree, always valid, with range [-90, 90], NumericD::minValue()...
Definition GPSTracker.h:131
Location()
Creates an invalid location.
Definition GPSTracker.h:280
float altitudeAccuracy() const
Returns the accuracy of the altitude.
Definition GPSTracker.h:334
float speedAccuracy() const
Returns the accuracy of the speed value.
Definition GPSTracker.h:344
bool isValid() const
Returns whether this location holds valid data.
Definition GPSTracker.h:349
float direction() const
Returns the device's travel direction not the orientation of the device.
Definition GPSTracker.h:319
float direction_
The device's travel direction not the orientation of the device, relative to north,...
Definition GPSTracker.h:140
float directionAccuracy() const
Returns the accuracy of the direction.
Definition GPSTracker.h:339
float altitude() const
Returns the altitude of the GPS position.
Definition GPSTracker.h:314
double longitude() const
Returns the longitude of the GPS position.
Definition GPSTracker.h:309
double longitude_
The longitude of the GPS position, in degree, always valid, with range [-180, 180],...
Definition GPSTracker.h:134
double latitude() const
Returns the latitude of the GPS position.
Definition GPSTracker.h:304
float directionAccuracy_
The direction accuracy, in degree, with range [0, 180], -1 if unknown.
Definition GPSTracker.h:152
float speed() const
Returns the speed of the device.
Definition GPSTracker.h:324
This class is the base class for all gps tracker objects.
Definition GPSTracker.h:39
~GPSTracker() override
Destructs a GPS tracker object.
static bool parseGPSLocation(const std::string &gpsString, double &latitude, double &longitude, double *altitude=nullptr)
Parses a string with a GPS location.
GPSTracker(const std::string &name)
Creates a new GPS tracker object.
static bool decodePolyline(const std::string &polyline, const unsigned int precision, std::vector< Location > &locations, const bool unescapeBackslash=false)
Decodes all GPS locations from a polyline.
static bool decodePolylineValue(const std::string &polyline, size_t &position, int32_t &integerValue, const bool unescapeBackslash)
Decodes one value from a polyline.
static double approximatedDistanceBetweenLocations(const Location &locationA, const Location &locationB, const double earthRadius=6378135.0)
Calculates the distance between two GPS locations in meters, the location's latitude is not considere...
static DeviceType deviceTypeGPSTracker()
Returns the device type of this tracker.
Definition GPSTracker.h:362
std::unordered_map< std::string, Value > Metadata
Definition of an unordered map mapping keys to values.
Definition Measurement.h:62
Definition of a sample holding one measurement values of a tracker.
Definition devices/Tracker.h:90
This class implements the base class for all tracker devices.
Definition devices/Tracker.h:39
@ TRACKER_GPS
GPS tracker.
Definition devices/Tracker.h:58
ReferenceSystem
Definition of different tracking reference system.
Definition devices/Tracker.h:74
static constexpr bool isInsideRange(const T lower, const T value, const T upper, const T epsilon=NumericT< T >::eps())
Returns whether a value lies between a given range up to a provided epsilon border.
Definition Numeric.h:2881
This template class implements a smart object reference which is a specialization of an ObjectRef obj...
Definition SmartObjectRef.h:90
This class implements a timestamp.
Definition Timestamp.h:63
The namespace covering the entire Ocean framework.
Definition Accessor.h:15