1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QLOCATIONUTILS_P_H
#define QLOCATIONUTILS_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtCore/QtGlobal>
#include <math.h> // needed for non-std:: versions of functions
#include <qmath.h>
#include <QtPositioning/QGeoCoordinate>
#include <QtPositioning/QNmeaSatelliteInfoSource>
#include <QtPositioning/private/qpositioningglobal_p.h>
static const double offsetEpsilon = 1e-12; // = 0.000000000001
static const double leftOffset = -180.0 + offsetEpsilon;
static const double rightOffset = 180.0 - offsetEpsilon;
QT_BEGIN_NAMESPACE
class QTime;
class QByteArray;
class QGeoPositionInfo;
class QGeoSatelliteInfo;
class Q_POSITIONING_EXPORT QLocationUtils
{
public:
enum CardinalDirection {
CardinalN,
CardinalE,
CardinalS,
CardinalW,
CardinalNE,
CardinalSE,
CardinalSW,
CardinalNW,
CardinalNNE,
CardinalENE,
CardinalESE,
CardinalSSE,
CardinalSSW,
CardinalWSW,
CardinalWNW,
CardinalNNW
};
enum NmeaSentence {
NmeaSentenceInvalid,
NmeaSentenceGGA, // Fix information
NmeaSentenceGSA, // Overall Satellite data, such as HDOP and VDOP
NmeaSentenceGLL, // Lat/Lon data
NmeaSentenceRMC, // Recommended minimum data for gps
NmeaSentenceVTG, // Vector track an Speed over the Ground
NmeaSentenceZDA, // Date and Time
NmeaSentenceGSV // Per-Satellite Info
};
inline static bool isValidLat(double lat) {
return lat >= -90.0 && lat <= 90.0;
}
inline static bool isValidLong(double lng) {
return lng >= -180.0 && lng <= 180.0;
}
inline static double clipLat(double lat, double clipValue = 90.0) {
if (lat > clipValue)
lat = clipValue;
else if (lat < -clipValue)
lat = -clipValue;
return lat;
}
inline static double wrapLong(double lng) {
if (lng > 180.0)
lng -= 360.0;
else if (lng < -180.0)
lng += 360.0;
return lng;
}
inline static CardinalDirection azimuthToCardinalDirection4(double azimuth)
{
azimuth = fmod(azimuth, 360.0);
if (azimuth < 45.0 || azimuth > 315.0 )
return CardinalN;
else if (azimuth < 135.0)
return CardinalE;
else if (azimuth < 225.0)
return CardinalS;
else
return CardinalW;
}
inline static CardinalDirection azimuthToCardinalDirection8(double azimuth)
{
azimuth = fmod(azimuth, 360.0);
if (azimuth < 22.5 || azimuth > 337.5 )
return CardinalN;
else if (azimuth < 67.5)
return CardinalNE;
else if (azimuth < 112.5)
return CardinalE;
else if (azimuth < 157.5)
return CardinalSE;
else if (azimuth < 202.5)
return CardinalS;
else if (azimuth < 247.5)
return CardinalSW;
else if (azimuth < 292.5)
return CardinalW;
else
return CardinalNW;
}
inline static CardinalDirection azimuthToCardinalDirection16(double azimuth)
{
azimuth = fmod(azimuth, 360.0);
if (azimuth < 11.5 || azimuth > 348.75 )
return CardinalN;
else if (azimuth < 33.75)
return CardinalNNE;
else if (azimuth < 56.25)
return CardinalNE;
else if (azimuth < 78.75)
return CardinalENE;
else if (azimuth < 101.25)
return CardinalE;
else if (azimuth < 123.75)
return CardinalESE;
else if (azimuth < 146.25)
return CardinalSE;
else if (azimuth < 168.75)
return CardinalSSE;
else if (azimuth < 191.25)
return CardinalS;
else if (azimuth < 213.75)
return CardinalSSW;
else if (azimuth < 236.25)
return CardinalSW;
else if (azimuth < 258.75)
return CardinalWSW;
else if (azimuth < 281.25)
return CardinalW;
else if (azimuth < 303.75)
return CardinalWNW;
else if (azimuth < 326.25)
return CardinalNW;
else
return CardinalNNW;
}
// For values exceeding +- 720.0
inline static double wrapLongExt(double lng) {
double remainder = fmod(lng + 180.0, 360.0);
return fmod(remainder + 360.0, 360.0) - 180.0;
}
// Mirrors the azimuth against the X axis. Azimuth assumed to be in [0,360[
inline static double mirrorAzimuthX(double azimuth) {
if (azimuth <= 90.0)
return 180.0 - azimuth;
else
return 180.0 + (360.0 - azimuth);
}
// Mirrors the azimuth against the Y axis. Azimuth assumed to be in [0,360[
inline static double mirrorAzimuthY(double azimuth) {
if (azimuth == 0.0)
return 0.0;
return 360.0 - azimuth;
}
inline static double radians(double degrees)
{
return qDegreesToRadians(degrees);
}
inline static double degrees(double radians)
{
return qRadiansToDegrees(radians);
}
inline static double earthMeanRadius()
{
return 6371007.2;
}
inline static double earthMeanCircumference()
{
return earthMeanRadius() * 2.0 * M_PI;
}
inline static double mercatorMaxLatitude()
{
return 85.05113;
}
inline static QGeoCoordinate antipodalPoint(const QGeoCoordinate &p)
{
return QGeoCoordinate(-p.latitude(), wrapLong(p.longitude() + 180.0));
}
// Leftmost longitude before wrapping kicks in
inline static double mapLeftLongitude(double centerLongitude)
{
return wrapLong(centerLongitude + leftOffset);
}
// Rightmost longitude before wrapping kicks in
inline static double mapRightLongitude(double centerLongitude)
{
return wrapLong(centerLongitude - leftOffset);
}
inline static void split_double(double input, float *hipart, float *lopart)
{
*hipart = (float) input;
double delta = input - ((double) *hipart);
*lopart = (float) delta;
}
static qreal metersPerPixel(qreal zoomLevel, const QGeoCoordinate &coordinate)
{
const qreal metersPerTile = earthMeanCircumference() * std::cos(radians(coordinate.latitude())) / std::pow(2, zoomLevel);
return metersPerTile / 256.0;
}
/*
returns the NMEA sentence type.
*/
static NmeaSentence getNmeaSentenceType(QByteArrayView bv);
/*
Returns the satellite system type based on the message type.
See https://gpsd.gitlab.io/gpsd/NMEA.html#_talker_ids for reference
*/
static QGeoSatelliteInfo::SatelliteSystem getSatelliteSystem(QByteArrayView bv);
/*
Returns the satellite system type based on the satellite id.
See https://gpsd.gitlab.io/gpsd/NMEA.html#_satellite_ids for reference
*/
static QGeoSatelliteInfo::SatelliteSystem getSatelliteSystemBySatelliteId(int satId);
/*
Creates a QGeoPositionInfo from a GGA, GLL, RMC, VTG or ZDA sentence.
Note:
- GGA and GLL sentences have time but not date so the update's
QDateTime object will have an invalid date.
- RMC reports date with a two-digit year so in this case the year
is assumed to be after the year 2000.
*/
static bool getPosInfoFromNmea(QByteArrayView bv,
QGeoPositionInfo *info, double uere,
bool *hasFix = nullptr);
/*
Retruns a list of QGeoSatelliteInfo in the view.
Note: this function has to be called repeatedly until it returns
QNmeaSatelliteInfoSource::FullyParsed.
Reason being that GSV sentences can be split into multiple samples, so
getting the full data requires parsing multiple sentences.
*/
static QNmeaSatelliteInfoSource::SatelliteInfoParseStatus
getSatInfoFromNmea(QByteArrayView bv, QList<QGeoSatelliteInfo> &infos, QGeoSatelliteInfo::SatelliteSystem &system);
/*
Parses GSA for satellites in use.
Returns satellite system type or QGeoSatelliteInfo::Undefined if parsing
failed
*/
static QGeoSatelliteInfo::SatelliteSystem getSatInUseFromNmea(QByteArrayView bv,
QList<int> &pnrsInUse);
/*
Returns true if the given NMEA sentence has a valid checksum.
*/
static bool hasValidNmeaChecksum(QByteArrayView bv);
/*
Returns time from a string in hhmmss or hhmmss.z+ format.
*/
static bool getNmeaTime(const QByteArray &bytes, QTime *time);
/*
Accepts for example ("2734.7964", 'S', "15306.0124", 'E') and returns the
lat-long values. Fails if lat or long fail isValidLat() or isValidLong().
*/
static bool getNmeaLatLong(const QByteArray &latString,
char latDirection,
const QByteArray &lngString,
char lngDirection,
double *lat,
double *lon);
};
QT_END_NAMESPACE
#endif
|