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