Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qgeocoordinate.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
6
7#include <QDateTime>
8#include <QHash>
9#include <QDataStream>
10#include <QDebug>
11#include <QMetaType>
12#include <qnumeric.h>
13#include <qmath.h>
14
16
17QT_IMPL_METATYPE_EXTERN(QGeoCoordinate)
18
19static const double qgeocoordinate_EARTH_MEAN_RADIUS = 6371.0072;
20
21
22QGeoCoordinatePrivate::QGeoCoordinatePrivate():
23 lat(qQNaN()),
24 lng(qQNaN()),
25 alt(qQNaN())
26{}
27
28QGeoCoordinatePrivate::QGeoCoordinatePrivate(const QGeoCoordinatePrivate &other)
29 : QSharedData(other),
30 lat(other.lat),
31 lng(other.lng),
32 alt(other.alt)
33{}
34
35QGeoCoordinatePrivate::~QGeoCoordinatePrivate()
36{}
37
38
39QGeoMercatorCoordinatePrivate::QGeoMercatorCoordinatePrivate():
40 QGeoCoordinatePrivate(),
41 m_mercatorX(qQNaN()),
42 m_mercatorY(qQNaN())
43{}
44
45QGeoMercatorCoordinatePrivate::QGeoMercatorCoordinatePrivate(const QGeoMercatorCoordinatePrivate &other)
46 : QGeoCoordinatePrivate(other),
47 m_mercatorX(other.m_mercatorX),
48 m_mercatorY(other.m_mercatorY)
49{}
50
51QGeoMercatorCoordinatePrivate::~QGeoMercatorCoordinatePrivate()
52{}
53
54/*!
55 \class QGeoCoordinate
56 \inmodule QtPositioning
57 \ingroup QtPositioning-positioning
58 \since 5.2
59
60 \brief The QGeoCoordinate class defines a geographical position on the surface of the Earth.
61
62 A QGeoCoordinate is defined by latitude, longitude, and optionally, altitude.
63
64 Use type() to determine whether a coordinate is a 2D coordinate (has
65 latitude and longitude only) or 3D coordinate (has latitude, longitude
66 and altitude). Use distanceTo() and azimuthTo() to calculate the distance
67 and bearing between coordinates.
68
69 The coordinate values should be specified using the WGS84 datum. For more information
70 on geographical terms see this article on \l {http://en.wikipedia.org/wiki/Geographic_coordinate_system}{coordinates} and
71 another on \l {http://en.wikipedia.org/wiki/Geodetic_system}{geodetic systems}
72 including WGS84.
73
74 Azimuth in this context is equivalent to a compass bearing based on true north.
75
76 This class is a \l Q_GADGET since Qt 5.5. It can be
77 \l{Cpp_value_integration_positioning}{directly used from C++ and QML}.
78*/
79
80/*!
81 \enum QGeoCoordinate::CoordinateType
82 Defines the types of a coordinate.
83
84 \value InvalidCoordinate An invalid coordinate. A coordinate is invalid if its latitude or longitude values are invalid.
85 \value Coordinate2D A coordinate with valid latitude and longitude values.
86 \value Coordinate3D A coordinate with valid latitude and longitude values, and also an altitude value.
87*/
88
89/*!
90 \enum QGeoCoordinate::CoordinateFormat
91 Defines the possible formatting options for toString().
92
93 \value Degrees Returns a string representation of the coordinates in decimal degrees format.
94 \value DegreesWithHemisphere Returns a string representation of the coordinates in decimal degrees format, using 'N', 'S', 'E' or 'W' to indicate the hemispheres of the coordinates.
95 \value DegreesMinutes Returns a string representation of the coordinates in degrees-minutes format.
96 \value DegreesMinutesWithHemisphere Returns a string representation of the coordinates in degrees-minutes format, using 'N', 'S', 'E' or 'W' to indicate the hemispheres of the coordinates.
97 \value DegreesMinutesSeconds Returns a string representation of the coordinates in degrees-minutes-seconds format.
98 \value DegreesMinutesSecondsWithHemisphere Returns a string representation of the coordinates in degrees-minutes-seconds format, using 'N', 'S', 'E' or 'W' to indicate the hemispheres of the coordinates.
99
100 \sa toString()
101*/
102
103/*!
104 \property QGeoCoordinate::latitude
105 \brief This property holds the latitude in decimal degrees.
106
107 The property is undefined (\l {qQNaN()}) if the latitude has not been set.
108 A positive latitude indicates the Northern Hemisphere, and a negative
109 latitude indicates the Southern Hemisphere. When setting the latitude the
110 new value should be in the
111 \l {http://en.wikipedia.org/wiki/World_Geodetic_System}{WGS84} datum format.
112
113 To be valid, the latitude must be between -90 to 90 inclusive.
114
115 While this property is introduced in Qt 5.5, the related accessor functions
116 exist since the first version of this class.
117
118 \since 5.5
119*/
120
121/*!
122 \property QGeoCoordinate::longitude
123 \brief This property holds the longitude in decimal degrees.
124
125 The property is undefined (\l {qQNaN()}) if the longitude has not been set.
126 A positive longitude indicates the Eastern Hemisphere, and a negative
127 longitude indicates the Western Hemisphere. When setting the longitude the
128 new value should be in the
129 \l {http://en.wikipedia.org/wiki/World_Geodetic_System}{WGS84} datum format.
130
131 To be valid, the longitude must be between -180 to 180 inclusive.
132
133 While this property is introduced in Qt 5.5, the related accessor functions
134 exist since the first version of this class.
135
136 \since 5.5
137*/
138
139/*!
140 \property QGeoCoordinate::altitude
141 \brief This property holds the altitude in meters above sea level.
142
143 The property is undefined (\l {qQNaN()}) if the altitude has not been set.
144
145 While this property is introduced in Qt 5.5, the related accessor functions
146 exist since the first version of this class.
147
148 \since 5.5
149*/
150
151/*!
152 \property QGeoCoordinate::isValid
153 \brief This property holds the validity of this geo coordinate.
154
155 The geo coordinate is valid if the \l [CPP]{longitude} and \l [CPP]{latitude}
156 properties have been set to valid values.
157
158 While this property is introduced in Qt 5.5, the related accessor functions
159 exist since the first version of this class.
160
161 \since 5.5
162*/
163
164/*!
165 Constructs a coordinate. The coordinate will be invalid until
166 setLatitude() and setLongitude() have been called.
167*/
168QGeoCoordinate::QGeoCoordinate()
169 : d(new QGeoCoordinatePrivate)
170{
171}
172
173/*!
174 Constructs a coordinate with the given \a latitude and \a longitude.
175
176 If the latitude is not between -90 to 90 inclusive, or the longitude
177 is not between -180 to 180 inclusive, none of the values are set and
178 the type() will be QGeoCoordinate::InvalidCoordinate.
179
180 \sa isValid()
181*/
182QGeoCoordinate::QGeoCoordinate(double latitude, double longitude)
183 : d(new QGeoCoordinatePrivate)
184{
185 if (QLocationUtils::isValidLat(latitude) && QLocationUtils::isValidLong(longitude)) {
186 d->lat = latitude;
187 d->lng = longitude;
188 }
189}
190
191/*!
192 Constructs a coordinate with the given \a latitude, \a longitude
193 and \a altitude.
194
195 If the latitude is not between -90 to 90 inclusive, or the longitude
196 is not between -180 to 180 inclusive, none of the values are set and
197 the type() will be QGeoCoordinate::InvalidCoordinate.
198
199 Note that \a altitude specifies the meters above sea level.
200
201 \sa isValid()
202*/
203QGeoCoordinate::QGeoCoordinate(double latitude, double longitude, double altitude)
204 : d(new QGeoCoordinatePrivate)
205{
206 if (QLocationUtils::isValidLat(latitude) && QLocationUtils::isValidLong(longitude)) {
207 d->lat = latitude;
208 d->lng = longitude;
209 d->alt = altitude;
210 }
211}
212
213/*!
214 Constructs a coordinate from the contents of \a other.
215*/
216QGeoCoordinate::QGeoCoordinate(const QGeoCoordinate &other)
217 : d(other.d)
218{}
219
220/*!
221 \fn QGeoCoordinate::QGeoCoordinate(QGeoCoordinate &&other)
222 \since 6.2
223
224 Constructs a coordinate by moving from \a other.
225
226 \note The moved-from QGeoCoordinate object can only be destroyed or
227 assigned to. The effect of calling other functions than the destructor
228 or one of the assignment operators is undefined.
229*/
230
231/*!
232 Assigns \a other to this coordinate and returns a reference to this coordinate.
233*/
234QGeoCoordinate &QGeoCoordinate::operator=(const QGeoCoordinate &other)
235{
236 if (this == &other)
237 return *this;
238
239 d = other.d;
240 return (*this);
241}
242
243/*!
244 \fn QGeoCoordinate &QGeoCoordinate::operator=(QGeoCoordinate &&other)
245 \since 6.2
246
247 Move-assigns \a other to this coordinate and returns a reference to this
248 coordinate.
249
250 \note The moved-from QGeoCoordinate object can only be destroyed or
251 assigned to. The effect of calling other functions than the destructor
252 or one of the assignment operators is undefined.
253*/
254
255/*!
256 Destroys the coordinate object.
257*/
258QGeoCoordinate::~QGeoCoordinate()
259{
260}
261
262QT_DEFINE_QSDP_SPECIALIZATION_DTOR(QGeoCoordinatePrivate)
263
264/*!
265 \fn bool QGeoCoordinate::operator==(const QGeoCoordinate &lhs, const QGeoCoordinate &rhs)
266
267 Returns \c true if the latitude, longitude and altitude of the \a lhs
268 coordinate are the same as those of the \a rhs coordinate. Otherwise
269 returns \c false.
270
271 The longitude will be ignored if the latitude is +/- 90 degrees.
272*/
273
274/*!
275 \fn bool QGeoCoordinate::operator!=(const QGeoCoordinate &lhs, const QGeoCoordinate &rhs)
276
277 Returns \c true if latitude, longitude, or altitude of the \a lhs
278 coordinate are not identical to those of the \a rhs coordinate. Otherwise
279 returns \c false.
280*/
281
282/*!
283 Returns \c true if the \l longitude and \l latitude are valid.
284*/
285bool QGeoCoordinate::isValid() const
286{
287 CoordinateType t = type();
288 return t == Coordinate2D || t == Coordinate3D;
289}
290
291/*!
292 Returns the type of this coordinate.
293*/
294QGeoCoordinate::CoordinateType QGeoCoordinate::type() const
295{
296 if (QLocationUtils::isValidLat(d->lat)
297 && QLocationUtils::isValidLong(d->lng)) {
298 if (qIsNaN(d->alt))
299 return Coordinate2D;
300 return Coordinate3D;
301 }
302 return InvalidCoordinate;
303}
304
305
306/*!
307 Returns the latitude, in decimal degrees. The return value is undefined
308 if the latitude has not been set.
309
310 A positive latitude indicates the Northern Hemisphere, and a negative
311 latitude indicates the Southern Hemisphere.
312
313 \sa setLatitude(), type()
314*/
315double QGeoCoordinate::latitude() const
316{
317 return d->lat;
318}
319
320/*!
321 Sets the latitude (in decimal degrees) to \a latitude. The value should
322 be in the WGS84 datum.
323
324 To be valid, the latitude must be between -90 to 90 inclusive.
325
326 \sa latitude()
327*/
328void QGeoCoordinate::setLatitude(double latitude)
329{
330 d->lat = latitude;
331}
332
333/*!
334 Returns the longitude, in decimal degrees. The return value is undefined
335 if the longitude has not been set.
336
337 A positive longitude indicates the Eastern Hemisphere, and a negative
338 longitude indicates the Western Hemisphere.
339
340 \sa setLongitude(), type()
341*/
342double QGeoCoordinate::longitude() const
343{
344 return d->lng;
345}
346
347/*!
348 Sets the longitude (in decimal degrees) to \a longitude. The value should
349 be in the WGS84 datum.
350
351 To be valid, the longitude must be between -180 to 180 inclusive.
352
353 \sa longitude()
354*/
355void QGeoCoordinate::setLongitude(double longitude)
356{
357 d->lng = longitude;
358}
359
360/*!
361 Returns the altitude (meters above sea level).
362
363 The return value is undefined if the altitude has not been set.
364
365 \sa setAltitude(), type()
366*/
367double QGeoCoordinate::altitude() const
368{
369 return d->alt;
370}
371
372/*!
373 Sets the altitude (meters above sea level) to \a altitude.
374
375 \sa altitude()
376*/
377void QGeoCoordinate::setAltitude(double altitude)
378{
379 d->alt = altitude;
380}
381
382/*!
383 Returns the distance (in meters) from this coordinate to the coordinate
384 specified by \a other. Altitude is not used in the calculation.
385
386 This calculation returns the great-circle distance between the two
387 coordinates, with an assumption that the Earth is spherical for the
388 purpose of this calculation.
389
390 Returns 0 if the type of this coordinate or the type of \a other is
391 QGeoCoordinate::InvalidCoordinate.
392*/
393qreal QGeoCoordinate::distanceTo(const QGeoCoordinate &other) const
394{
395 if (type() == QGeoCoordinate::InvalidCoordinate
396 || other.type() == QGeoCoordinate::InvalidCoordinate) {
397 return 0;
398 }
399
400 // Haversine formula
401 double dlat = qDegreesToRadians(other.d->lat - d->lat);
402 double dlon = qDegreesToRadians(other.d->lng - d->lng);
403 double haversine_dlat = sin(dlat / 2.0);
404 haversine_dlat *= haversine_dlat;
405 double haversine_dlon = sin(dlon / 2.0);
406 haversine_dlon *= haversine_dlon;
407 double y = haversine_dlat
408 + cos(qDegreesToRadians(d->lat))
409 * cos(qDegreesToRadians(other.d->lat))
410 * haversine_dlon;
411 double x = 2 * asin(sqrt(y));
412 return qreal(x * qgeocoordinate_EARTH_MEAN_RADIUS * 1000);
413}
414
415/*!
416 Returns the azimuth (or bearing) in degrees from this coordinate to the
417 coordinate specified by \a other. Altitude is not used in the calculation.
418
419 The bearing returned is the bearing from the origin to \a other along the
420 great-circle between the two coordinates. There is an assumption that the
421 Earth is spherical for the purpose of this calculation.
422
423 Returns 0 if the type of this coordinate or the type of \a other is
424 QGeoCoordinate::InvalidCoordinate.
425*/
426qreal QGeoCoordinate::azimuthTo(const QGeoCoordinate &other) const
427{
428 if (type() == QGeoCoordinate::InvalidCoordinate
429 || other.type() == QGeoCoordinate::InvalidCoordinate) {
430 return 0;
431 }
432
433 double dlon = qDegreesToRadians(other.d->lng - d->lng);
434 double lat1Rad = qDegreesToRadians(d->lat);
435 double lat2Rad = qDegreesToRadians(other.d->lat);
436
437 double y = sin(dlon) * cos(lat2Rad);
438 double x = cos(lat1Rad) * sin(lat2Rad) - sin(lat1Rad) * cos(lat2Rad) * cos(dlon);
439
440 double azimuth = qRadiansToDegrees(atan2(y, x)) + 360.0;
441 double whole;
442 double fraction = modf(azimuth, &whole);
443 return qreal((int(whole + 360) % 360) + fraction);
444}
445
446void QGeoCoordinatePrivate::atDistanceAndAzimuth(const QGeoCoordinate &coord,
447 qreal distance, qreal azimuth,
448 double *lon, double *lat)
449{
450 double latRad = qDegreesToRadians(coord.d->lat);
451 double lonRad = qDegreesToRadians(coord.d->lng);
452 double cosLatRad = cos(latRad);
453 double sinLatRad = sin(latRad);
454
455 double azimuthRad = qDegreesToRadians(azimuth);
456
457 double ratio = (distance / (qgeocoordinate_EARTH_MEAN_RADIUS * 1000.0));
458 double cosRatio = cos(ratio);
459 double sinRatio = sin(ratio);
460
461 double resultLatRad = asin(sinLatRad * cosRatio
462 + cosLatRad * sinRatio * cos(azimuthRad));
463 double resultLonRad = lonRad + atan2(sin(azimuthRad) * sinRatio * cosLatRad,
464 cosRatio - sinLatRad * sin(resultLatRad));
465
466 *lat = qRadiansToDegrees(resultLatRad);
467 *lon = qRadiansToDegrees(resultLonRad);
468}
469
470/*!
471 Returns the coordinate that is reached by traveling \a distance meters
472 from the current coordinate at \a azimuth (or bearing) along a great-circle.
473 There is an assumption that the Earth is spherical for the purpose of this
474 calculation.
475
476 The altitude will have \a distanceUp added to it.
477
478 Returns an invalid coordinate if this coordinate is invalid.
479*/
480QGeoCoordinate QGeoCoordinate::atDistanceAndAzimuth(qreal distance, qreal azimuth, qreal distanceUp) const
481{
482 if (!isValid())
483 return QGeoCoordinate();
484
485 double resultLon, resultLat;
486 QGeoCoordinatePrivate::atDistanceAndAzimuth(*this, distance, azimuth,
487 &resultLon, &resultLat);
488 double resultAlt = d->alt + distanceUp;
489 return QGeoCoordinate(resultLat, QLocationUtils::wrapLong(resultLon), resultAlt);
490}
491
492/*!
493 Returns this coordinate as a string in the specified \a format.
494
495 For example, if this coordinate has a latitude of -27.46758, a longitude
496 of 153.027892 and an altitude of 28.1, these are the strings
497 returned depending on \a format:
498
499 \table
500 \header
501 \li \a format value
502 \li Returned string
503 \row
504 \li \l Degrees
505 \li -27.46758\unicode{0xB0}, 153.02789\unicode{0xB0}, 28.1m
506 \row
507 \li \l DegreesWithHemisphere
508 \li 27.46758\unicode{0xB0} S, 153.02789\unicode{0xB0} E, 28.1m
509 \row
510 \li \l DegreesMinutes
511 \li -27\unicode{0xB0} 28.054', 153\unicode{0xB0} 1.673', 28.1m
512 \row
513 \li \l DegreesMinutesWithHemisphere
514 \li 27\unicode{0xB0} 28.054 S', 153\unicode{0xB0} 1.673' E, 28.1m
515 \row
516 \li \l DegreesMinutesSeconds
517 \li -27\unicode{0xB0} 28' 3.2", 153\unicode{0xB0} 1' 40.4", 28.1m
518 \row
519 \li \l DegreesMinutesSecondsWithHemisphere
520 \li 27\unicode{0xB0} 28' 3.2" S, 153\unicode{0xB0} 1' 40.4" E, 28.1m
521 \endtable
522
523 The altitude field is omitted if no altitude is set.
524
525 If the coordinate is invalid, an empty string is returned.
526*/
527QString QGeoCoordinate::toString(CoordinateFormat format) const
528{
529 if (type() == QGeoCoordinate::InvalidCoordinate)
530 return QString();
531
532 QString latStr;
533 QString longStr;
534
535 double absLat = qAbs(d->lat);
536 double absLng = qAbs(d->lng);
537 QChar symbol(0x00B0); // degrees symbol
538
539 switch (format) {
540 case Degrees:
541 case DegreesWithHemisphere: {
542 latStr = QString::number(absLat, 'f', 5) + symbol;
543 longStr = QString::number(absLng, 'f', 5) + symbol;
544 break;
545 }
546 case DegreesMinutes:
547 case DegreesMinutesWithHemisphere: {
548 double latMin = (absLat - int(absLat)) * 60;
549 double lngMin = (absLng - int(absLng)) * 60;
550
551 // We use QString::number(val, 'f', 3) to represent minutes.
552 // It rounds up to the next integer in case the fraction > 0.9995.
553 // Such behavior should be handled specifically when the rounded
554 // value is 60, so that we overflow to degrees correctly.
555 // If we overflow, the minutes should unconditionally be 0.0.
556 if (latMin > 59.9995) {
557 absLat++;
558 latMin = 0.0f;
559 }
560 if (lngMin > 59.9995) {
561 absLng++;
562 lngMin = 0.0f;
563 }
564
565 latStr = QString::fromLatin1("%1%2 %3'")
566 .arg(QString::number(int(absLat)))
567 .arg(symbol)
568 .arg(QString::number(latMin, 'f', 3));
569 longStr = QString::fromLatin1("%1%2 %3'")
570 .arg(QString::number(int(absLng)))
571 .arg(symbol)
572 .arg(QString::number(lngMin, 'f', 3));
573 break;
574 }
575 case DegreesMinutesSeconds:
576 case DegreesMinutesSecondsWithHemisphere: {
577 double latMin = (absLat - int(absLat)) * 60;
578 double lngMin = (absLng - int(absLng)) * 60;
579 double latSec = (latMin - int(latMin)) * 60;
580 double lngSec = (lngMin - int(lngMin)) * 60;
581
582 // We use QString::number(val, 'f', 1) to represent seconds.
583 // It rounds up to the next integer in case the fraction >= 0.95.
584 // Such behavior should be handled specifically when the rounded
585 // value is 60, so that we overflow to minutes correctly.
586 // If we overflow, the seconds should unconditionally be 0.0.
587 if (latSec >= 59.95) {
588 latMin++;
589 latSec = 0.0f;
590 // We cast to int to represent minutes, so we can use qRound()
591 // to determine if we need to overflow to full degrees.
592 // If we overflow, the minutes will unconditionally be 0.0.
593 if (qRound(latMin) >= 60) {
594 absLat++;
595 latMin = 0.0f;
596 }
597 }
598 if (lngSec >= 59.95) {
599 lngMin++;
600 lngSec = 0.0f;
601 if (qRound(lngMin) >= 60) {
602 absLng++;
603 lngMin = 0.0f;
604 }
605 }
606
607 latStr = QString::fromLatin1("%1%2 %3' %4\"")
608 .arg(QString::number(int(absLat)))
609 .arg(symbol)
610 .arg(QString::number(int(latMin)))
611 .arg(QString::number(latSec, 'f', 1));
612 longStr = QString::fromLatin1("%1%2 %3' %4\"")
613 .arg(QString::number(int(absLng)))
614 .arg(symbol)
615 .arg(QString::number(int(lngMin)))
616 .arg(QString::number(lngSec, 'f', 1));
617 break;
618 }
619 }
620
621 // now add the "-" to the start, or append the hemisphere char
622 switch (format) {
623 case Degrees:
624 case DegreesMinutes:
625 case DegreesMinutesSeconds: {
626 if (d->lat < 0)
627 latStr.insert(0, QStringLiteral("-"));
628 if (d->lng < 0)
629 longStr.insert(0, QStringLiteral("-"));
630 break;
631 }
632 case DegreesWithHemisphere:
633 case DegreesMinutesWithHemisphere:
634 case DegreesMinutesSecondsWithHemisphere: {
635 if (d->lat < 0)
636 latStr.append(QString::fromLatin1(" S"));
637 else if (d->lat > 0)
638 latStr.append(QString::fromLatin1(" N"));
639 if (d->lng < 0)
640 longStr.append(QString::fromLatin1(" W"));
641 else if (d->lng > 0)
642 longStr.append(QString::fromLatin1(" E"));
643 break;
644 }
645 }
646
647 if (qIsNaN(d->alt))
648 return QString::fromLatin1("%1, %2").arg(latStr, longStr);
649 return QString::fromLatin1("%1, %2, %3m").arg(latStr, longStr, QString::number(d->alt));
650}
651
652bool QGeoCoordinate::equals(const QGeoCoordinate &lhs, const QGeoCoordinate &rhs)
653{
654 bool latEqual = (qIsNaN(lhs.d->lat) && qIsNaN(rhs.d->lat))
655 || qFuzzyCompare(lhs.d->lat, rhs.d->lat);
656 bool lngEqual = (qIsNaN(lhs.d->lng) && qIsNaN(rhs.d->lng))
657 || qFuzzyCompare(lhs.d->lng, rhs.d->lng);
658 bool altEqual = (qIsNaN(lhs.d->alt) && qIsNaN(rhs.d->alt))
659 || qFuzzyCompare(lhs.d->alt, rhs.d->alt);
660
661 if (!qIsNaN(lhs.d->lat) && ((lhs.d->lat == 90.0) || (lhs.d->lat == -90.0)))
662 lngEqual = true;
663
664 return (latEqual && lngEqual && altEqual);
665}
666
667QGeoCoordinate::QGeoCoordinate(QGeoCoordinatePrivate &dd):
668 d(&dd)
669{
670}
671
672#ifndef QT_NO_DEBUG_STREAM
673QDebug QGeoCoordinate::debugStreaming(QDebug dbg, const QGeoCoordinate &coord)
674{
675 QDebugStateSaver saver(dbg);
676 double lat = coord.latitude();
677 double lng = coord.longitude();
678
679 QTextStreamManipulator tsm = qSetRealNumberPrecision(11);
680 dbg << tsm;
681 dbg.nospace() << "QGeoCoordinate(";
682 if (qIsNaN(lat))
683 dbg << '?';
684 else
685 dbg << lat;
686 dbg << ", ";
687 if (qIsNaN(lng))
688 dbg << '?';
689 else
690 dbg << lng;
691 if (coord.type() == QGeoCoordinate::Coordinate3D) {
692 dbg << ", ";
693 dbg << coord.altitude();
694 }
695 dbg << ')';
696 return dbg;
697}
698#endif
699
700#ifndef QT_NO_DATASTREAM
701/*!
702 \fn QDataStream &QGeoCoordinate::operator<<(QDataStream &stream, const QGeoCoordinate &coordinate)
703
704 Writes the given \a coordinate to the specified \a stream.
705
706 \sa {Serializing Qt Data Types}
707*/
708
709QDataStream &QGeoCoordinate::dataStreamOut(QDataStream &stream, const QGeoCoordinate &coordinate)
710{
711 stream << coordinate.latitude();
712 stream << coordinate.longitude();
713 stream << coordinate.altitude();
714 return stream;
715}
716#endif
717
718#ifndef QT_NO_DATASTREAM
719/*!
720 \fn QDataStream &QGeoCoordinate::operator>>(QDataStream &stream, QGeoCoordinate &coordinate)
721
722 Reads a coordinate from the specified \a stream into the given
723 \a coordinate.
724
725 \sa {Serializing Qt Data Types}
726*/
727
728QDataStream &QGeoCoordinate::dataStreamIn(QDataStream &stream, QGeoCoordinate &coordinate)
729{
730 double value;
731 stream >> value;
732 coordinate.setLatitude(value);
733 stream >> value;
734 coordinate.setLongitude(value);
735 stream >> value;
736 coordinate.setAltitude(value);
737 return stream;
738}
739#endif
740
741/*! \fn size_t qHash(const QGeoCoordinate &coordinate, size_t seed = 0)
742 \relates QHash
743
744 Returns a hash value for \a coordinate, using \a seed to seed the calculation.
745*/
746size_t qHash(const QGeoCoordinate &coordinate, size_t seed)
747{
748 QtPrivate::QHashCombine hash;
749 // north and south pole are geographically equivalent (no matter the longitude)
750 if (coordinate.latitude() != 90.0 && coordinate.latitude() != -90.0)
751 seed = hash(seed, coordinate.longitude());
752 seed = hash(seed, coordinate.latitude());
753 seed = hash(seed, coordinate.altitude());
754 return seed;
755}
756
757QT_END_NAMESPACE
758
759#include "moc_qgeocoordinate.cpp"
Combined button and popup list for selecting options.
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
Definition qsize.h:181