30 const QList<QByteArray> parts = QByteArray::fromRawData(bv.data(), bv.size()).split(
',');
33 if (hasFix && parts.size() > 6 && !parts[6].isEmpty())
34 *hasFix = parts[6].toInt() > 0;
36 if (parts.size() > 1 && !parts[1].isEmpty()) {
38 if (QLocationUtils::getNmeaTime(parts[1], &time))
39 info->setTimestamp(QDateTime(QDate(), time, QTimeZone::UTC));
42 if (parts.size() > 5 && parts[3].size() == 1 && parts[5].size() == 1) {
45 if (QLocationUtils::getNmeaLatLong(parts[2], parts[3][0], parts[4], parts[5][0], &lat, &lng)) {
46 coord.setLatitude(lat);
47 coord.setLongitude(lng);
51 if (parts.size() > 8 && !parts[8].isEmpty()) {
53 double hdop = parts[8].toDouble(&hasHdop);
55 info->setAttribute(QGeoPositionInfo::HorizontalAccuracy, 2 * hdop * uere);
58 if (parts.size() > 9 && !parts[9].isEmpty()) {
60 double alt = parts[9].toDouble(&hasAlt);
62 coord.setAltitude(alt);
65 if (coord.type() != QGeoCoordinate::InvalidCoordinate)
66 info->setCoordinate(coord);
72 const QList<QByteArray> parts = QByteArray::fromRawData(bv.data(), bv.size()).split(
',');
74 if (hasFix && parts.size() > 2 && !parts[2].isEmpty())
75 *hasFix = parts[2].toInt() > 0;
77 if (parts.size() > 16 && !parts[16].isEmpty()) {
79 double hdop = parts[16].toDouble(&hasHdop);
81 info->setAttribute(QGeoPositionInfo::HorizontalAccuracy, 2 * hdop * uere);
84 if (parts.size() > 17 && !parts[17].isEmpty()) {
86 double vdop = parts[17].toDouble(&hasVdop);
88 info->setAttribute(QGeoPositionInfo::VerticalAccuracy, 2 * vdop * uere);
94 const QList<QByteArray> parts = QByteArray::fromRawData(bv.data(), bv.size()).split(
',');
96 if (parts.size() <= 2)
99 for (qsizetype i = 3; i < qMin(15, parts.size()); ++i) {
101 if (pnrString.isEmpty())
103 int pnr = pnrString.toInt(&ok);
105 pnrsInUse.append(pnr);
111 const QList<QByteArray> parts = QByteArray::fromRawData(bv.data(), bv.size()).split(
',');
112 QGeoCoordinate coord;
114 if (hasFix && parts.size() > 6 && !parts[6].isEmpty())
115 *hasFix = (parts[6][0] ==
'A');
117 if (parts.size() > 5 && !parts[5].isEmpty()) {
119 if (QLocationUtils::getNmeaTime(parts[5], &time))
120 info->setTimestamp(QDateTime(QDate(), time, QTimeZone::UTC));
123 if (parts.size() > 4 && parts[2].size() == 1 && parts[4].size() == 1) {
126 if (QLocationUtils::getNmeaLatLong(parts[1], parts[2][0], parts[3], parts[4][0], &lat, &lng)) {
127 coord.setLatitude(lat);
128 coord.setLongitude(lng);
132 if (coord.type() != QGeoCoordinate::InvalidCoordinate)
133 info->setCoordinate(coord);
138 const QList<QByteArray> parts = QByteArray::fromRawData(bv.data(), bv.size()).split(
',');
139 QGeoCoordinate coord;
143 if (hasFix && parts.size() > 2 && !parts[2].isEmpty())
144 *hasFix = (parts[2][0] ==
'A');
146 if (parts.size() > 9 && parts[9].size() == 6) {
147 date = QDate::fromString(QString::fromLatin1(parts[9]), QStringLiteral(
"ddMMyy"));
149 date = date.addYears(100);
152 if (parts.size() > 1 && !parts[1].isEmpty())
153 QLocationUtils::getNmeaTime(parts[1], &time);
155 if (parts.size() > 6 && parts[4].size() == 1 && parts[6].size() == 1) {
158 if (QLocationUtils::getNmeaLatLong(parts[3], parts[4][0], parts[5], parts[6][0], &lat, &lng)) {
159 coord.setLatitude(lat);
160 coord.setLongitude(lng);
166 if (parts.size() > 7 && !parts[7].isEmpty()) {
167 value = parts[7].toDouble(&parsed);
169 info->setAttribute(QGeoPositionInfo::GroundSpeed, qreal(value * 1.852 / 3.6));
171 if (parts.size() > 8 && !parts[8].isEmpty()) {
172 value = parts[8].toDouble(&parsed);
174 info->setAttribute(QGeoPositionInfo::Direction, qreal(value));
176 if (parts.size() > 11 && parts[11].size() == 1
177 && (parts[11][0] ==
'E' || parts[11][0] ==
'W')) {
178 value = parts[10].toDouble(&parsed);
180 if (parts[11][0] ==
'W')
182 info->setAttribute(QGeoPositionInfo::MagneticVariation, qreal(value));
186 if (coord.type() != QGeoCoordinate::InvalidCoordinate)
187 info->setCoordinate(coord);
189 info->setTimestamp(QDateTime(date, time, QTimeZone::UTC));
197 const QList<QByteArray> parts = QByteArray::fromRawData(bv.data(), bv.size()).split(
',');
201 if (parts.size() > 1 && !parts[1].isEmpty()) {
202 value = parts[1].toDouble(&parsed);
204 info->setAttribute(QGeoPositionInfo::Direction, qreal(value));
206 if (parts.size() > 7 && !parts[7].isEmpty()) {
207 value = parts[7].toDouble(&parsed);
209 info->setAttribute(QGeoPositionInfo::GroundSpeed, qreal(value / 3.6));
218 const QList<QByteArray> parts = QByteArray::fromRawData(bv.data(), bv.size()).split(
',');
222 if (parts.size() > 1 && !parts[1].isEmpty())
223 QLocationUtils::getNmeaTime(parts[1], &time);
225 if (parts.size() > 4 && !parts[2].isEmpty() && !parts[3].isEmpty()
226 && parts[4].size() == 4) {
227 int day = parts[2].toInt();
228 int month = parts[3].toInt();
229 int year = parts[4].toInt();
230 if (day > 0 && month > 0 && year > 0)
231 date.setDate(year, month, day);
234 info->setTimestamp(QDateTime(date, time, QTimeZone::UTC));
268QGeoSatelliteInfo::SatelliteSystem QLocationUtils::getSatelliteSystem(QByteArrayView bv)
270 if (bv.size() < 6 || bv[0] !=
'$' || !hasValidNmeaChecksum(bv))
271 return QGeoSatelliteInfo::Undefined;
273 QByteArrayView key = bv.sliced(1);
276 if (key.startsWith(
"GP"))
277 return QGeoSatelliteInfo::GPS;
280 if (key.startsWith(
"GL"))
281 return QGeoSatelliteInfo::GLONASS;
284 if (key.startsWith(
"GA"))
285 return QGeoSatelliteInfo::GALILEO;
288 if (key.startsWith(
"BD") || key.startsWith(
"GB"))
289 return QGeoSatelliteInfo::BEIDOU;
292 if (key.startsWith(
"GQ") || key.startsWith(
"PQ") || key.startsWith(
"QZ"))
293 return QGeoSatelliteInfo::QZSS;
296 if (key.startsWith(
"GN"))
297 return QGeoSatelliteInfo::Multiple;
299 return QGeoSatelliteInfo::Undefined;
302QGeoSatelliteInfo::SatelliteSystem QLocationUtils::getSatelliteSystemBySatelliteId(
int satId)
304 if (satId >= 1 && satId <= 32)
305 return QGeoSatelliteInfo::GPS;
307 if (satId >= 65 && satId <= 96)
308 return QGeoSatelliteInfo::GLONASS;
310 if (satId >= 193 && satId <= 200)
311 return QGeoSatelliteInfo::QZSS;
313 if ((satId >= 201 && satId <= 235) || (satId >= 401 && satId <= 437))
314 return QGeoSatelliteInfo::BEIDOU;
316 if (satId >= 301 && satId <= 336)
317 return QGeoSatelliteInfo::GALILEO;
319 return QGeoSatelliteInfo::Undefined;
322bool QLocationUtils::getPosInfoFromNmea(QByteArrayView bv, QGeoPositionInfo *info,
323 double uere,
bool *hasFix)
331 NmeaSentence nmeaType = getNmeaSentenceType(bv);
332 if (nmeaType == NmeaSentenceInvalid)
336 qsizetype idx = bv.indexOf(
'*');
337 QByteArrayView key = idx < 0 ? bv : bv.first(idx);
340 case NmeaSentenceGGA:
341 qlocationutils_readGga(key, info, uere, hasFix);
343 case NmeaSentenceGSA:
344 qlocationutils_readGsa(key, info, uere, hasFix);
346 case NmeaSentenceGLL:
347 qlocationutils_readGll(key, info, hasFix);
349 case NmeaSentenceRMC:
350 qlocationutils_readRmc(key, info, hasFix);
352 case NmeaSentenceVTG:
353 qlocationutils_readVtg(key, info, hasFix);
355 case NmeaSentenceZDA:
356 qlocationutils_readZda(key, info, hasFix);
364QLocationUtils::getSatInfoFromNmea(QByteArrayView bv, QList<QGeoSatelliteInfo> &infos, QGeoSatelliteInfo::SatelliteSystem &system)
367 return QNmeaSatelliteInfoSource::NotParsed;
369 NmeaSentence nmeaType = getNmeaSentenceType(bv);
370 if (nmeaType != NmeaSentenceGSV)
371 return QNmeaSatelliteInfoSource::NotParsed;
375 system = getSatelliteSystem(bv);
379 qsizetype idx = bv.indexOf(
'*');
381 const QList<QByteArray> parts = QByteArray::fromRawData(bv.data(),
382 idx < 0 ? bv.size() : idx).split(
',');
384 if (parts.size() <= 3) {
386 return QNmeaSatelliteInfoSource::FullyParsed;
389 const int totalSentences = parts.at(1).toInt(&ok);
392 return QNmeaSatelliteInfoSource::FullyParsed;
395 const int sentence = parts.at(2).toInt(&ok);
398 return QNmeaSatelliteInfoSource::FullyParsed;
401 const int totalSats = parts.at(3).toInt(&ok);
404 return QNmeaSatelliteInfoSource::FullyParsed;
410 const int numSatInSentence = qMin(sentence * 4, totalSats) - (sentence - 1) * 4;
411 if (parts.size() < (4 + numSatInSentence * 4)) {
413 return QNmeaSatelliteInfoSource::FullyParsed;
417 for (
int i = 0; i < numSatInSentence; ++i) {
418 QGeoSatelliteInfo info;
419 info.setSatelliteSystem(system);
420 int prn = parts.at(field++).toInt(&ok);
430 if (ok && (system == QGeoSatelliteInfo::GLONASS)) {
434 info.setSatelliteIdentifier((ok) ? prn : 0);
435 const int elevation = parts.at(field++).toInt(&ok);
436 info.setAttribute(QGeoSatelliteInfo::Elevation, (ok) ? elevation : 0);
437 const int azimuth = parts.at(field++).toInt(&ok);
438 info.setAttribute(QGeoSatelliteInfo::Azimuth, (ok) ? azimuth : 0);
439 const int snr = parts.at(field++).toInt(&ok);
440 info.setSignalStrength((ok) ? snr : -1);
444 if (sentence == totalSentences)
445 return QNmeaSatelliteInfoSource::FullyParsed;
447 return QNmeaSatelliteInfoSource::PartiallyParsed;
450QGeoSatelliteInfo::SatelliteSystem QLocationUtils::getSatInUseFromNmea(QByteArrayView bv,
451 QList<
int> &pnrsInUse)
454 return QGeoSatelliteInfo::Undefined;
456 NmeaSentence nmeaType = getNmeaSentenceType(bv);
457 if (nmeaType != NmeaSentenceGSA)
458 return QGeoSatelliteInfo::Undefined;
460 auto systemType = getSatelliteSystem(bv);
461 if (systemType == QGeoSatelliteInfo::Undefined)
469 qsizetype idx = bv.indexOf(
'*');
470 QByteArrayView key = idx < 0 ? bv : bv.first(idx);
472 qlocationutils_readGsa(key, pnrsInUse);
483 if (systemType == QGeoSatelliteInfo::GLONASS) {
484 std::for_each(pnrsInUse.begin(), pnrsInUse.end(), [](
int &id) {
490 if ((systemType == QGeoSatelliteInfo::Multiple) && !pnrsInUse.isEmpty()) {
494 auto tempSystemType = getSatelliteSystemBySatelliteId(pnrsInUse.front());
495 if (tempSystemType != QGeoSatelliteInfo::Undefined)
496 systemType = tempSystemType;
502bool QLocationUtils::hasValidNmeaChecksum(QByteArrayView bv)
504 qsizetype asteriskIndex = bv.indexOf(
'*');
506 constexpr qsizetype CSUM_LEN = 2;
507 if (asteriskIndex < 0 || asteriskIndex >= bv.size() - CSUM_LEN)
512 for (qsizetype i = 1; i < asteriskIndex; ++i)
515
516
517
518
520 QByteArrayView checkSumBytes = bv.sliced(asteriskIndex + 1, 2);
522 int checksum = checkSumBytes.toInt(&ok,16);
523 return ok && checksum == result;
526bool QLocationUtils::getNmeaTime(
const QByteArray &bytes, QTime *time)
528 QTime tempTime = QTime::fromString(QString::fromLatin1(bytes),
529 QStringView(bytes.size() > 6 && bytes[6] ==
'.'
533 if (tempTime.isValid()) {
540bool QLocationUtils::getNmeaLatLong(
const QByteArray &latString,
char latDirection,
const QByteArray &lngString,
char lngDirection,
double *lat,
double *lng)
542 if ((latDirection !=
'N' && latDirection !=
'S')
543 || (lngDirection !=
'E' && lngDirection !=
'W')) {
548 bool hasLong =
false;
549 double tempLat = latString.toDouble(&hasLat);
550 double tempLng = lngString.toDouble(&hasLong);
551 if (hasLat && hasLong) {
552 tempLat = qlocationutils_nmeaDegreesToDecimal(tempLat);
553 if (latDirection ==
'S')
555 tempLng = qlocationutils_nmeaDegreesToDecimal(tempLng);
556 if (lngDirection ==
'W')
559 if (isValidLat(tempLat) && isValidLong(tempLng)) {