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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* ============================================================
 *
 * 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
 *
 * ============================================================ */

#pragma once

// Qt includes

#include <QAtomicInt>

// Local includes

#include "Quaternion.h"

namespace Marble
{

class Q_DECL_HIDDEN GeoDataCoordinatesPrivate
{
public:

    /**
     * if this ctor is called there exists exactly one GeoDataCoordinates object
     * with this data.
     * changes will be made in the GeoDataCoordinates class
     * ref must be called ref as qAtomicAssign used in GeoDataCoordinates::operator=
     * needs this name. Maybe we can rename it to our scheme later on.
     */
    GeoDataCoordinatesPrivate()
        : m_q(nullptr),
          m_lon(0),
          m_lat(0),
          m_altitude(0),
          m_detail(0),
          ref(0)
    {
    }

    /**
     * if this ctor is called there exists exactly one GeoDataCoordinates object
     * with this data.
     * changes will be made in the GeoDataCoordinates class
     * ref must be called ref as qAtomicAssign used in GeoDataCoordinates::operator=
     * needs this name. Maybe we can rename it to our scheme later on.
     */
    GeoDataCoordinatesPrivate(qreal _lon, qreal _lat, qreal _alt,
                              GeoDataCoordinates::Unit unit,
                              int _detail)
        : m_q(nullptr),
          m_altitude(_alt),
          m_detail(_detail),
          ref(0)
    {
        switch (unit)
        {
            default:
            case GeoDataCoordinates::Radian:
                m_lon = _lon;
                m_lat = _lat;
                break;

            case GeoDataCoordinates::Degree:
                m_lon = _lon * DEG2RAD;
                m_lat = _lat * DEG2RAD;
                break;
        }
    }

    /**
     * this constructor is needed as Quaternion doesn't define a copy ctor
     * initialize the reference with the value of the other
     */
    GeoDataCoordinatesPrivate(const GeoDataCoordinatesPrivate& other)
        : m_q(nullptr),
          m_lon(other.m_lon),
          m_lat(other.m_lat),
          m_altitude(other.m_altitude),
          m_detail(other.m_detail),
          ref(0)
    {
    }

    /**
     * return this instead of &other
     */
    GeoDataCoordinatesPrivate& operator=(const GeoDataCoordinatesPrivate& other)
    {
        m_lon = other.m_lon;
        m_lat = other.m_lat;
        m_altitude = other.m_altitude;
        m_detail = other.m_detail;
        ref = 0;
        delete m_q;
        m_q = nullptr;
        return *this;
    }

    bool operator==(const GeoDataCoordinatesPrivate& rhs) const;
    bool operator!=(const GeoDataCoordinatesPrivate& rhs) const;

    static Quaternion basePoint(const Quaternion& q1, const Quaternion& q2, const Quaternion& q3);

    // Helper functions for UTM-related development.
    // Based on Chuck Taylor work:
    // https://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html

    /**
     * Computes the ellipsoidal distance from the equator to a point at a
     * given latitude.
     *
     * Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
     * GPS: Theory and Practice, 3rd ed.  New York: Springer-Verlag Wien, 1994.
     *
     * @param phi Latitude of the point, in radians.
     * @return The ellipsoidal distance of the point from the equator, in meters.
     */
    static qreal arcLengthOfMeridian(qreal phi);

    /**
     * Determines the central meridian for the given UTM zone.
     *
     * @param zone An integer value designating the UTM zone, range [1,60].
     * @return The central meridian for the given UTM zone, in radians, or zero
     * if the UTM zone parameter is outside the range [1,60].
     * Range of the central meridian is the radian equivalent of [-177,+177].
     */
    static qreal centralMeridianUTM(qreal zone);

    /**
     * Computes the footpoint latitude for use in converting transverse
     * Mercator coordinates to ellipsoidal coordinates.
     *
     * Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
     *   GPS: Theory and Practice, 3rd ed.  New York: Springer-Verlag Wien, 1994.
     *
     * @param northing The UTM northing coordinate, in meters.
     * @return The footpoint latitude, in radians.
     */
    static qreal footpointLatitude(qreal northing);

    /**
     * Converts a latitude/longitude pair to x and y coordinates in the
     * Transverse Mercator projection.  Note that Transverse Mercator is not
     * the same as UTM; a scale factor is required to convert between them.
     *
     * Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
     * GPS: Theory and Practice, 3rd ed.  New York: Springer-Verlag Wien, 1994.
     *
     * @param lambda Longitude of the point, in radians.
     * @param phi Latitude of the point, in radians.
     * @param lambda0 Longitude of the central meridian to be used, in radians.
     * @return The computed point with its x and y coordinates
     */
    static QPointF mapLonLatToXY(qreal lambda, qreal phi, qreal lambda0);

    /**
     * Converts a latitude/longitude pair to x and y coordinates in the
     * Universal Transverse Mercator projection.
     *
     * @param lon Longitude of the point, in radians.
     * @param lat Latitude of the point, in radians.
     * @param zone UTM zone between 1 and 60 to be used for calculating
     * values for x and y.
     * @return A point with its x and y coordinates representing
     * easting and northing of the UTM coordinates computed.
     */
    static QPointF lonLatToUTMXY(qreal lon, qreal lat, qreal zone);

    /**
     * @brief retrieves the UTM latitude band of a longitude/latitude
     * pair
     * @param lon longitude, in radians
     * @param lat latitude, in radians
     * @return latitude band
     */
    static QString lonLatToLatitudeBand(qreal lon, qreal lat);

    /**
     * @brief retrieves the northing value of a longitude/latitude
     * pair
     * @param lon longitude, in radians
     * @param lat latitude, in radians
     * @return UTM northing value
     */
    static qreal lonLatToNorthing(qreal lon, qreal lat);

    /**
     * @brief retrieves the UTM zone number of a longitude/latitude
     * pair
     * @param lon longitude, in radians
     * @param lat latitude, in radians
     * @return UTM zone number
     */
    static int lonLatToZone(qreal lon, qreal lat);

    /**
     * @brief  retrieves the easting value of a longitude/latitude
     * pair
     * @param lon longitude, in radians
     * @param lat latitude, in radians
     * @return UTM easting value
     */
    static qreal lonLatToEasting(qreal lon, qreal lat);

    Quaternion* m_q = nullptr;
    qreal      m_lon;
    qreal      m_lat;
    qreal      m_altitude;     // in meters above sea level
    quint8     m_detail;
    QAtomicInt ref;

    /* UTM Ellipsoid model constants (actual values here are for WGS84) */
    static const qreal sm_semiMajorAxis;
    static const qreal sm_semiMinorAxis;
    static const qreal sm_eccentricitySquared;
    static const qreal sm_utmScaleFactor;
};

inline bool GeoDataCoordinatesPrivate::operator==(const GeoDataCoordinatesPrivate& rhs) const
{
    // do not compare the m_detail member as it does not really belong to
    // GeoDataCoordinates and should be removed
    return m_lon == rhs.m_lon && m_lat == rhs.m_lat && m_altitude == rhs.m_altitude;
}

inline bool GeoDataCoordinatesPrivate::operator!=(const GeoDataCoordinatesPrivate& rhs) const
{
    // do not compare the m_detail member as it does not really belong to
    // GeoDataCoordinates and should be removed
    return !(*this == rhs);
}

} // namespace Marble