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
qwebmercator.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
3// Qt-Security score:significant reason:default
5
7
8#include <qnumeric.h>
9#include <qmath.h>
10
12
14// We reverse the y-direction to make North be its decreasing direction, which
15// is "up" in computer graphics. We offset it by 0.5 to put the range -85 deg to
16// +85 deg latitude into the unit interval. The x-coordinate maps the range from
17// -180 deg to +180 deg longitude into its unit interval, with The Prime
18// Meridian mapped to x = 0.5. The x-coordinate is formally periodic, with
19// period 1, so adding or subtracting any whole number gets a representation of
20// the same longitude.
21
22// As latitudes approach ±90, the Mercator y-Coordinate aproaches ∓infinity. We
23// limit that to avoid numerical problems. Here the limit, -4 <= y <= 5, is
24// chosen such that only points beyond ±89.9999999999 deg latituide are cut off,
25// which matches the accuracy of qFuzzyCompare()
26const static double yCutOff = 4.0;
27
28QDoubleVector2D QWebMercator::coordToMercator(const QGeoCoordinate &coord)
29{
30 const double x = coord.longitude() / 360.0 + 0.5;
31 const double lat = coord.latitude();
32 const double y = (1.0 - std::log(std::tan(qDegreesToRadians((90.0 + lat) / 2.0))) / M_PI) / 2.0;
33 return QDoubleVector2D(x, qBound(-yCutOff, y, 1.0 + yCutOff));
34}
35
36double QWebMercator::realmod(const double a, const double b)
37{
38 quint64 div = static_cast<quint64>(a / b);
39 return a - static_cast<double>(div) * b;
40}
41
42QGeoCoordinate QWebMercator::mercatorToCoord(const QDoubleVector2D &mercator)
43{
44 double fx = mercator.x();
45 const double fy = mercator.y();
46
47 double lat;
48 if (fy <= -yCutOff)
49 lat = 90.0;
50 else if (fy >= 1.0 + yCutOff)
51 lat = -90.0;
52 else
53 lat = qRadiansToDegrees(2.0 * std::atan(std::exp(M_PI * (1.0 - 2.0 * fy)))) - 90.0;
54
55 if (fx < 0) // Map to the +ve unit interval:
56 fx = 1.0 - realmod(-fx, 1.0);
57 const double lng = realmod(fx, 1.0) * 360.0 - 180.0;
58
59 return QGeoCoordinate(lat, lng, 0.0);
60}
61
62QGeoCoordinate QWebMercator::coordinateInterpolation(const QGeoCoordinate &from,
63 const QGeoCoordinate &to, qreal progress)
64{
65 QDoubleVector2D s = QWebMercator::coordToMercator(from);
66 QDoubleVector2D e = QWebMercator::coordToMercator(to);
67
68 double x;
69
70 if (0.5 < qAbs(e.x() - s.x())) {
71 // handle dateline crossing
72 double ex = e.x();
73 double sx = s.x();
74 if (ex < sx)
75 sx -= 1.0;
76 else if (sx < ex)
77 ex -= 1.0;
78
79 x = (1.0 - progress) * sx + progress * ex;
80
81 if (!qFuzzyIsNull(x) && (x < 0.0))
82 x += 1.0;
83
84 } else {
85 x = (1.0 - progress) * s.x() + progress * e.x();
86 }
87
88 double y = (1.0 - progress) * s.y() + progress * e.y();
89
90 QGeoCoordinate result = QWebMercator::mercatorToCoord(QDoubleVector2D(x, y));
91 result.setAltitude((1.0 - progress) * from.altitude() + progress * to.altitude());
92
93 return result;
94}
95
96QT_END_NAMESPACE
Combined button and popup list for selecting options.
#define M_PI
Definition qmath.h:201
QT_BEGIN_NAMESPACE static const double yCutOff