12#include <QtCore/qmetaobject.h>
19QT_IMPL_METATYPE_EXTERN(QGeoCoordinate)
21static const double qgeocoordinate_EARTH_MEAN_RADIUS = 6371.0072;
24QGeoCoordinatePrivate::QGeoCoordinatePrivate():
30QGeoCoordinatePrivate::QGeoCoordinatePrivate(
const QGeoCoordinatePrivate &other)
35 altType(other.altType)
38QGeoCoordinatePrivate::~QGeoCoordinatePrivate()
42QGeoMercatorCoordinatePrivate::QGeoMercatorCoordinatePrivate():
43 QGeoCoordinatePrivate(),
48QGeoMercatorCoordinatePrivate::QGeoMercatorCoordinatePrivate(
const QGeoMercatorCoordinatePrivate &other)
49 : QGeoCoordinatePrivate(other),
50 m_mercatorX(other.m_mercatorX),
51 m_mercatorY(other.m_mercatorY)
54QGeoMercatorCoordinatePrivate::~QGeoMercatorCoordinatePrivate()
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
88
89
90
91
92
93
94
97
98
99
100
101
102
103
104
105
106
107
108
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
175
176
177
178
179
180
181
182
183
184
185
186
187
190
191
192
193
194
195
196
197
198
199
200
201
204
205
206
207
208
209
210
211
212
213
214
217
218
219
220QGeoCoordinate::QGeoCoordinate()
221 : d(
new QGeoCoordinatePrivate)
226
227
228
229
230
231
232
233
234QGeoCoordinate::QGeoCoordinate(
double latitude,
double longitude)
235 : d(
new QGeoCoordinatePrivate)
237 if (QLocationUtils::isValidLat(latitude) && QLocationUtils::isValidLong(longitude)) {
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258QGeoCoordinate::QGeoCoordinate(
double latitude,
double longitude,
double altitude)
259 : d(
new QGeoCoordinatePrivate)
261 if (QLocationUtils::isValidLat(latitude) && QLocationUtils::isValidLong(longitude)) {
269
270
271QGeoCoordinate::QGeoCoordinate(
const QGeoCoordinate &other)
276
277
278
279
280
281
282
283
284
287
288
289QGeoCoordinate &QGeoCoordinate::operator=(
const QGeoCoordinate &other)
299
300
301
302
303
304
305
306
307
308
311
312
313QGeoCoordinate::~QGeoCoordinate()
317QT_DEFINE_QSDP_SPECIALIZATION_DTOR(QGeoCoordinatePrivate)
320
321
322
323
324
325
326
327
328
329
330
333
334
335
336
337
338
339
340
341
344
345
346
347
350
351
352bool QGeoCoordinate::isValid()
const
354 CoordinateType t = type();
355 return t == Coordinate2D || t == Coordinate3D;
359
360
361QGeoCoordinate::CoordinateType QGeoCoordinate::type()
const
363 if (QLocationUtils::isValidLat(d->lat)
364 && QLocationUtils::isValidLong(d->lng)) {
369 return InvalidCoordinate;
374
375
376
377
378
379
380
381
382double QGeoCoordinate::latitude()
const
388
389
390
391
392
393
394
395void QGeoCoordinate::setLatitude(
double latitude)
401
402
403
404
405
406
407
408
409double QGeoCoordinate::longitude()
const
415
416
417
418
419
420
421
422void QGeoCoordinate::setLongitude(
double longitude)
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496double QGeoCoordinate::altitude()
const
502
503
504
505
506
507
508void QGeoCoordinate::setAltitude(
double altitude)
514
515
516
517
518
519
520
521
522
523
524QGeoCoordinate::AltitudeType QGeoCoordinate::altitudeType()
const
530
531
532
533
534
535
536
537
538
539
540
541
542
543void QGeoCoordinate::setAltitudeType(AltitudeType type)
549
550
551
552
553
554
555
556
557
558
559qreal QGeoCoordinate::distanceTo(
const QGeoCoordinate &other)
const
561 if (type() == QGeoCoordinate::InvalidCoordinate
562 || other.type() == QGeoCoordinate::InvalidCoordinate) {
567 double dlat = qDegreesToRadians(other.d->lat - d->lat);
568 double dlon = qDegreesToRadians(other.d->lng - d->lng);
569 double haversine_dlat = sin(dlat / 2.0);
570 haversine_dlat *= haversine_dlat;
571 double haversine_dlon = sin(dlon / 2.0);
572 haversine_dlon *= haversine_dlon;
573 double y = haversine_dlat
574 + cos(qDegreesToRadians(d->lat))
575 * cos(qDegreesToRadians(other.d->lat))
577 double x = 2 * asin(sqrt(y));
578 return qreal(x * qgeocoordinate_EARTH_MEAN_RADIUS * 1000);
582
583
584
585
586
587
588
589
590
591
592qreal QGeoCoordinate::azimuthTo(
const QGeoCoordinate &other)
const
594 if (type() == QGeoCoordinate::InvalidCoordinate
595 || other.type() == QGeoCoordinate::InvalidCoordinate) {
599 double dlon = qDegreesToRadians(other.d->lng - d->lng);
600 double lat1Rad = qDegreesToRadians(d->lat);
601 double lat2Rad = qDegreesToRadians(other.d->lat);
603 double y = sin(dlon) * cos(lat2Rad);
604 double x = cos(lat1Rad) * sin(lat2Rad) - sin(lat1Rad) * cos(lat2Rad) * cos(dlon);
606 double azimuth = qRadiansToDegrees(atan2(y, x)) + 360.0;
608 double fraction = modf(azimuth, &whole);
609 return qreal((
int(whole + 360) % 360) + fraction);
612void QGeoCoordinatePrivate::atDistanceAndAzimuth(
const QGeoCoordinate &coord,
613 qreal distance, qreal azimuth,
614 double *lon,
double *lat)
616 double latRad = qDegreesToRadians(coord.d->lat);
617 double lonRad = qDegreesToRadians(coord.d->lng);
618 double cosLatRad = cos(latRad);
619 double sinLatRad = sin(latRad);
621 double azimuthRad = qDegreesToRadians(azimuth);
623 double ratio = (distance / (qgeocoordinate_EARTH_MEAN_RADIUS * 1000.0));
624 double cosRatio = cos(ratio);
625 double sinRatio = sin(ratio);
627 double resultLatRad = asin(sinLatRad * cosRatio
628 + cosLatRad * sinRatio * cos(azimuthRad));
629 double resultLonRad = lonRad + atan2(sin(azimuthRad) * sinRatio * cosLatRad,
630 cosRatio - sinLatRad * sin(resultLatRad));
632 *lat = qRadiansToDegrees(resultLatRad);
633 *lon = qRadiansToDegrees(resultLonRad);
637
638
639
640
641
642
643
644
645
646QGeoCoordinate QGeoCoordinate::atDistanceAndAzimuth(qreal distance, qreal azimuth, qreal distanceUp)
const
649 return QGeoCoordinate();
651 double resultLon, resultLat;
652 QGeoCoordinatePrivate::atDistanceAndAzimuth(*
this, distance, azimuth,
653 &resultLon, &resultLat);
654 double resultAlt = d->alt + distanceUp;
655 QGeoCoordinate result(resultLat, QLocationUtils::wrapLong(resultLon), resultAlt);
656 result.d->altType = d->altType;
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695QString QGeoCoordinate::toString(CoordinateFormat format)
const
697 if (type() == QGeoCoordinate::InvalidCoordinate)
703 double absLat = qAbs(d->lat);
704 double absLng = qAbs(d->lng);
708 case DegreesWithHemisphere: {
709 latStr = QString::asprintf(
"%.5f°", absLat);
710 longStr = QString::asprintf(
"%.5f°", absLng);
714 case DegreesMinutesWithHemisphere: {
715 double latMin = (absLat -
int(absLat)) * 60;
716 double lngMin = (absLng -
int(absLng)) * 60;
723 if (latMin > 59.9995) {
727 if (lngMin > 59.9995) {
732 latStr = QString::asprintf(
"%d° %.3f'",
int(absLat), latMin);
733 longStr = QString::asprintf(
"%d° %.3f'",
int(absLng), lngMin);
736 case DegreesMinutesSeconds:
737 case DegreesMinutesSecondsWithHemisphere: {
738 double latMin = (absLat -
int(absLat)) * 60;
739 double lngMin = (absLng -
int(absLng)) * 60;
740 double latSec = (latMin -
int(latMin)) * 60;
741 double lngSec = (lngMin -
int(lngMin)) * 60;
748 if (latSec >= 59.95) {
754 if (qRound(latMin) >= 60) {
759 if (lngSec >= 59.95) {
762 if (qRound(lngMin) >= 60) {
768 latStr = QString::asprintf(
"%d° %d' %.1f\"",
int(absLat),
int(latMin), latSec);
769 longStr = QString::asprintf(
"%d° %d' %.1f\"",
int(absLng),
int(lngMin), lngSec);
778 case DegreesMinutesSeconds: {
780 latStr.insert(0, QStringLiteral(
"-"));
782 longStr.insert(0, QStringLiteral(
"-"));
785 case DegreesWithHemisphere:
786 case DegreesMinutesWithHemisphere:
787 case DegreesMinutesSecondsWithHemisphere: {
789 latStr.append(QString::fromLatin1(
" S"));
791 latStr.append(QString::fromLatin1(
" N"));
793 longStr.append(QString::fromLatin1(
" W"));
795 longStr.append(QString::fromLatin1(
" E"));
801 return QString::fromLatin1(
"%1, %2").arg(latStr, longStr);
802 return QString::fromLatin1(
"%1, %2, %3m").arg(latStr, longStr, QString::number(d->alt));
805bool QGeoCoordinate::equals(
const QGeoCoordinate &lhs,
const QGeoCoordinate &rhs)
807 bool latEqual = (qIsNaN(lhs.d->lat) && qIsNaN(rhs.d->lat))
808 || QtPrivate::fuzzyCompare(lhs.d->lat, rhs.d->lat);
809 bool lngEqual = (qIsNaN(lhs.d->lng) && qIsNaN(rhs.d->lng))
810 || QtPrivate::fuzzyCompare(lhs.d->lng, rhs.d->lng);
812 const bool altitudesUndefined = qIsNaN(lhs.d->alt) && qIsNaN(rhs.d->alt);
813 bool altEqual = altitudesUndefined || QtPrivate::fuzzyCompare(lhs.d->alt, rhs.d->alt);
815 if (!qIsNaN(lhs.d->lat) && ((lhs.d->lat == 90.0) || (lhs.d->lat == -90.0)))
819 bool altTypeEqual = altitudesUndefined || (lhs.d->altType == rhs.d->altType);
821 return (latEqual && lngEqual && altEqual && altTypeEqual);
824QGeoCoordinate::QGeoCoordinate(QGeoCoordinatePrivate &dd):
829#ifndef QT_NO_DEBUG_STREAM
830QDebug QGeoCoordinate::debugStreaming(QDebug dbg,
const QGeoCoordinate &coord)
832 QDebugStateSaver saver(dbg);
833 double lat = coord.latitude();
834 double lng = coord.longitude();
836 auto altTypeToStr = [](
const QGeoCoordinate &c) {
837 quint64 type =
static_cast<quint64>(c.altitudeType());
838 return QMetaEnum::fromType<QGeoCoordinate::AltitudeType>().valueToKey(type);
841 QTextStreamManipulator tsm = qSetRealNumberPrecision(11);
843 dbg.nospace() <<
"QGeoCoordinate(";
853 if (coord.type() == QGeoCoordinate::Coordinate3D) {
855 dbg << coord.altitude();
858 if (coord.altitudeType() != QGeoCoordinate::AltitudeType::Unknown)
859 dbg <<
" " << altTypeToStr(coord);
866#ifndef QT_NO_DATASTREAM
868
869
870
871
872
873
874
875
876
877
879QDataStream &QGeoCoordinate::dataStreamOut(QDataStream &stream,
const QGeoCoordinate &coordinate)
881 stream << coordinate.latitude();
882 stream << coordinate.longitude();
883 stream << coordinate.altitude();
884 if (stream.version() >= QDataStream::Qt_6_13)
885 stream <<
int(coordinate.altitudeType());
890#ifndef QT_NO_DATASTREAM
892
893
894
895
896
897
898
899
900
901
902
903
904
906QDataStream &QGeoCoordinate::dataStreamIn(QDataStream &stream, QGeoCoordinate &coordinate)
910 coordinate.setLatitude(value);
912 coordinate.setLongitude(value);
914 coordinate.setAltitude(value);
916 AltitudeType altType = AltitudeType::Unknown;
917 if (stream.version() >= QDataStream::Qt_6_13) {
920 if (rawType < 0 || rawType >
int(AltitudeType::WGS84)) {
921 stream.setStatus(QDataStream::ReadCorruptData);
922 rawType =
int(AltitudeType::Unknown);
924 altType = AltitudeType(rawType);
926 coordinate.setAltitudeType(altType);
932
933
934
935
938 QtPrivate::QHashCombine hash(seed);
940 if (coordinate.latitude() != 90.0 && coordinate.latitude() != -90.0)
941 seed = hash(seed, coordinate.longitude());
942 seed = hash(seed, coordinate.latitude());
943 seed = hash(seed, coordinate.altitude());
945 if (!qIsNaN(coordinate.altitude()))
946 seed = hash(seed, coordinate.altitudeType());
952#include "moc_qgeocoordinate.cpp"
Combined button and popup list for selecting options.
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept