1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2023-05-15
* Description : geolocation engine based on Marble.
* (c) 2007-2022 Marble Team
* https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
*
* SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* ============================================================ */
#include "AzimuthalEquidistantProjection.h"
#include "AbstractProjection_p.h"
// Qt includes
#include <QIcon>
#include <qmath.h>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "ViewportParams.h"
#include "GeoDataPoint.h"
#include "GeoDataLineString.h"
#include "GeoDataCoordinates.h"
#include "MarbleGlobal.h"
#include "AzimuthalProjection_p.h"
#include "digikam_debug.h"
#define SAFE_DISTANCE
namespace Marble
{
class Q_DECL_HIDDEN AzimuthalEquidistantProjectionPrivate : public AzimuthalProjectionPrivate
{
public:
explicit AzimuthalEquidistantProjectionPrivate(AzimuthalEquidistantProjection* parent);
Q_DECLARE_PUBLIC(AzimuthalEquidistantProjection)<--- Derived function 'AzimuthalEquidistantProjectionPrivate::d_func'<--- Derived function 'AzimuthalEquidistantProjectionPrivate::d_func'
};
AzimuthalEquidistantProjection::AzimuthalEquidistantProjection()
: AzimuthalProjection(new AzimuthalEquidistantProjectionPrivate(this))
{
setMinLat(minValidLat());
setMaxLat(maxValidLat());
}
AzimuthalEquidistantProjection::AzimuthalEquidistantProjection(AzimuthalEquidistantProjectionPrivate* dd)
: AzimuthalProjection(dd)
{
setMinLat(minValidLat());
setMaxLat(maxValidLat());
}
AzimuthalEquidistantProjection::~AzimuthalEquidistantProjection()
{
}
QString AzimuthalEquidistantProjection::name() const
{
return i18n("Azimuthal Equidistant");
}
QString AzimuthalEquidistantProjection::description() const
{
return i18n("<p><b>Azimuthal Equidistant Projection</b> (\"fish eye\")</p><p>Applications: Display of seismic and radio data and for use in digital planetariums.</p>");
}
QIcon AzimuthalEquidistantProjection::icon() const
{
return QIcon::fromTheme(QStringLiteral("map-globe"));
}
AzimuthalEquidistantProjectionPrivate::AzimuthalEquidistantProjectionPrivate(AzimuthalEquidistantProjection* parent)
: AzimuthalProjectionPrivate(parent)
{
}
qreal AzimuthalEquidistantProjection::clippingRadius() const
{
return 1;
}
bool AzimuthalEquidistantProjection::screenCoordinates(const GeoDataCoordinates& coordinates,
const ViewportParams* viewport,
qreal& x, qreal& y, bool& globeHidesPoint) const
{
const qreal lambda = coordinates.longitude();
const qreal phi = coordinates.latitude();
const qreal lambdaPrime = viewport->centerLongitude();
const qreal phi1 = viewport->centerLatitude();
qreal cosC = qSin(phi1) * qSin(phi) + qCos(phi1) * qCos(phi) * qCos(lambda - lambdaPrime);
// Prevent division by zero
if (cosC <= 0)
{
globeHidesPoint = true;
return false;
}
qreal c = qAcos(cosC);
qreal k = cosC == 1 ? 1 : c / qSin(c);
// Let (x, y) be the position on the screen of the placemark..
x = (qCos(phi) * qSin(lambda - lambdaPrime)) * k;
y = (qCos(phi1) * qSin(phi) - qSin(phi1) * qCos(phi) * qCos(lambda - lambdaPrime)) * k;
x *= 2 * viewport->radius() / M_PI;
y *= 2 * viewport->radius() / M_PI;
const qint64 radius = clippingRadius() * viewport->radius();
if (x * x + y * y > radius * radius)
{
globeHidesPoint = true;
return false;
}
globeHidesPoint = false;
x += viewport->width() / 2;
y = viewport->height() / 2 - y;
// Skip placemarks that are outside the screen area
return !(x < 0 || x >= viewport->width() || y < 0 || y >= viewport->height());
}
bool AzimuthalEquidistantProjection::screenCoordinates(const GeoDataCoordinates& coordinates,
const ViewportParams* viewport,
qreal* x, qreal& y,
int& pointRepeatNum,
const QSizeF& size,
bool& globeHidesPoint) const
{
pointRepeatNum = 0;
globeHidesPoint = false;
bool visible = screenCoordinates(coordinates, viewport, *x, y, globeHidesPoint);
// Skip placemarks that are outside the screen area
if (*x + size.width() / 2.0 < 0.0 || *x >= viewport->width() + size.width() / 2.0
|| y + size.height() / 2.0 < 0.0 || y >= viewport->height() + size.height() / 2.0)
{
return false;
}
// This projection doesn't have any repetitions,
// so the number of screen points referring to the geopoint is one.
pointRepeatNum = 1;
return visible;
}
bool AzimuthalEquidistantProjection::geoCoordinates(const int x, const int y,
const ViewportParams* viewport,
qreal& lon, qreal& lat,
GeoDataCoordinates::Unit unit) const
{
const qint64 radius = viewport->radius();
// Calculate how many degrees are being represented per pixel.
const qreal rad2Pixel = (2 * radius) / M_PI;
const qreal centerLon = viewport->centerLongitude();
const qreal centerLat = viewport->centerLatitude();
const qreal rx = (- viewport->width() / 2 + x) / rad2Pixel;
const qreal ry = (viewport->height() / 2 - y) / rad2Pixel;
const qreal c = qMax(qSqrt(rx * rx + ry * ry), qreal(0.0001)); // ensure we don't divide by zero
const qreal sinc = qSin(c);
lon = centerLon + qAtan2(rx * sinc, (c * qCos(centerLat) * qCos(c) - ry * qSin(centerLat) * sinc));
while (lon < -M_PI)
{
lon += 2 * M_PI;
}
while (lon > M_PI)
{
lon -= 2 * M_PI;
}
lat = qAsin(qCos(c) * qSin(centerLat) + (ry * sinc * qCos(centerLat)) / c);
if (unit == GeoDataCoordinates::Degree)
{
lon *= RAD2DEG;
lat *= RAD2DEG;
}
return true;
}
} // namespace Marble
|