1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtPositioning module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 #include "qwebmercator_p.h"
40 
41 #include "qgeocoordinate.h"
42 
43 #include <qnumeric.h>
44 #include <qmath.h>
45 
46 #include "qdoublevector2d_p.h"
47 #include "qdoublevector3d_p.h"
48 
49 QT_BEGIN_NAMESPACE
50 
coordToMercator(const QGeoCoordinate & coord)51 QDoubleVector2D QWebMercator::coordToMercator(const QGeoCoordinate &coord)
52 {
53     const double pi = M_PI;
54 
55     double lon = coord.longitude() / 360.0 + 0.5;
56 
57     double lat = coord.latitude();
58     lat = 0.5 - (std::log(std::tan((pi / 4.0) + (pi / 2.0) * lat / 180.0)) / pi) / 2.0;
59     lat = qBound(0.0, lat, 1.0);
60 
61     return QDoubleVector2D(lon, lat);
62 }
63 
realmod(const double a,const double b)64 double QWebMercator::realmod(const double a, const double b)
65 {
66     quint64 div = static_cast<quint64>(a / b);
67     return a - static_cast<double>(div) * b;
68 }
69 
mercatorToCoord(const QDoubleVector2D & mercator)70 QGeoCoordinate QWebMercator::mercatorToCoord(const QDoubleVector2D &mercator)
71 {
72     const double pi = M_PI;
73 
74     double fx = mercator.x();
75     double fy = mercator.y();
76 
77     if (fy < 0.0)
78         fy = 0.0;
79     else if (fy > 1.0)
80         fy = 1.0;
81 
82     double lat;
83 
84     if (fy == 0.0)
85         lat = 90.0;
86     else if (fy == 1.0)
87         lat = -90.0;
88     else
89         lat = (180.0 / pi) * (2.0 * std::atan(std::exp(pi * (1.0 - 2.0 * fy))) - (pi / 2.0));
90 
91     double lng;
92     if (fx >= 0) {
93         lng = realmod(fx, 1.0);
94     } else {
95         lng = realmod(1.0 - realmod(-1.0 * fx, 1.0), 1.0);
96     }
97 
98     lng = lng * 360.0 - 180.0;
99 
100     return QGeoCoordinate(lat, lng, 0.0);
101 }
102 
coordinateInterpolation(const QGeoCoordinate & from,const QGeoCoordinate & to,qreal progress)103 QGeoCoordinate QWebMercator::coordinateInterpolation(const QGeoCoordinate &from, const QGeoCoordinate &to, qreal progress)
104 {
105     QDoubleVector2D s = QWebMercator::coordToMercator(from);
106     QDoubleVector2D e = QWebMercator::coordToMercator(to);
107 
108     double x = s.x();
109 
110     if (0.5 < qAbs(e.x() - s.x())) {
111         // handle dateline crossing
112         double ex = e.x();
113         double sx = s.x();
114         if (ex < sx)
115             sx -= 1.0;
116         else if (sx < ex)
117             ex -= 1.0;
118 
119         x = (1.0 - progress) * sx + progress * ex;
120 
121         if (!qFuzzyIsNull(x) && (x < 0.0))
122             x += 1.0;
123 
124     } else {
125         x = (1.0 - progress) * s.x() + progress * e.x();
126     }
127 
128     double y = (1.0 - progress) * s.y() + progress * e.y();
129 
130     QGeoCoordinate result = QWebMercator::mercatorToCoord(QDoubleVector2D(x, y));
131     result.setAltitude((1.0 - progress) * from.altitude() + progress * to.altitude());
132 
133     return result;
134 }
135 
136 QT_END_NAMESPACE
137