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