Merge branch 'master' of https://vcs.maemo.org/git/situare
[situare] / src / coordinates / geocoordinate.h
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Jussi Laitinen - jussi.laitinen@ixonos.com
6         Sami Rämö - sami.ramo@ixonos.com
7
8     Situare is free software; you can redistribute it and/or
9     modify it under the terms of the GNU General Public License
10     version 2 as published by the Free Software Foundation.
11
12     Situare is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with Situare; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20     USA.
21 */
22
23
24 #ifndef GEOCOORDINATE_H
25 #define GEOCOORDINATE_H
26
27 #include <QDebug>
28 #include <QMetaType>
29
30 class SceneCoordinate;
31
32 /**
33 * @brief Geographic coordinate
34 *
35 * @author Jussi Laitinen - jussi.laitinen@ixonos.com
36 * @author Sami Rämö - sami.ramo@ixonos.com
37 */
38 class GeoCoordinate
39 {
40 public:
41     /**
42     * @brief Constructs a null coordinate
43     */
44     GeoCoordinate();
45
46     /**
47     * @brief Constructs a coordinate with given latitude and longitude values
48     *
49     * @param latitude Latitude value
50     * @param longitude Longitude value
51     */
52     GeoCoordinate(double latitude, double longitude);
53
54     /**
55     * @brief Constructs a coordinate with values converted from the given SceneCoordinate
56     *
57     * @param coordinate Scene coordinate
58     */
59     GeoCoordinate(const SceneCoordinate &coordinate);
60
61 /*******************************************************************************
62  * MEMBER FUNCTIONS AND SLOTS
63  ******************************************************************************/
64 public:
65     /**
66     * @brief Distance to other coordinate
67     *
68     * This calculation returns the great-circle distance between the two coordinates, with an
69     * assumption that the Earth is spherical. Calculation is done using haversine formula. Altitude
70     * is not used in the calculation.
71     *
72     * @param other Coordinate where the distance is calculated from this coordinate
73     * @returns Distance to other coordinate (in meters)
74     */
75     qreal distanceTo(const GeoCoordinate &other) const;
76
77     /**
78     * @brief Check if coordinate is (0.0, 0.0)
79     *
80     * @returns True if both latitude and longitude are 0.0, otherwise false
81     */
82     bool isNull() const;
83
84     /**
85     * @brief Check if coordinate is valid.
86     *
87     * Latitude and longitude values must be set, latitude must be -90..90 and longitude must
88     * be -180..180 for valid coordinate.
89     *
90     * @return true if coordinate is valid, false otherwise
91     */
92     bool isValid();
93
94     /**
95     * @brief Returns the latitude value
96     *
97     * @returns latitude
98     */
99     double latitude() const;
100
101     /**
102     * @brief Returns the longitude value
103     *
104     * @returns longitude
105     */
106     double longitude() const;
107
108     /**
109     * @brief Sets the latitude
110     *
111     * @param latitude Latitude value
112     */
113     void setLatitude(double latitude);
114
115     /**
116     * @brief Sets the longitude
117     *
118     * @param longitude Longitude value
119     */
120     void setLongitude(double longitude);
121
122 private:
123     /**
124      * @brief Convert values from SceneCoordinate
125      *
126      * @param coordinate Scene coordinate
127      */
128     void convertFrom(const SceneCoordinate &coordinate);
129
130     /**
131     * @brief Register meta type and stream operators for using the class with QVariant and QSetting
132     *
133     * Registering is done only once at the first time the GeoCoordinate object is constructed.
134     */
135     void registerMetaType();
136
137 /*******************************************************************************
138  * DATA MEMBERS
139  ******************************************************************************/
140 private:
141     /**
142     * @enum ValidityFlag
143     *
144     * @brief Flag values for coordinate validity
145     */
146     enum ValidityFlag {
147         NoCoordinatesSet = 0x0,
148         LatitudeSet = 0x1,
149         LongitudeSet = 0x2
150     };
151
152     static bool m_metaTypeIsRegistered;     ///< Is the meta type already registered?
153     double m_latitude;                      ///< Latitude value
154     double m_longitude;                     ///< Longitude value
155     int m_validityFlags;                    ///< Coordinate validity flags
156
157 /*******************************************************************************
158  * OPERATORS
159  ******************************************************************************/
160 public:
161     /**
162     * @brief Subtract operator
163     *
164     * @returns Returns a GeoCoordinate object that is formed by subtracting coordinate2 from
165     *          coordinate1. Each component is subtracted separately.
166     */
167     friend const GeoCoordinate operator-(const GeoCoordinate &coordinate1,
168                                          const GeoCoordinate &coordinate2);
169
170     /**
171     * @brief Output operator
172     *
173     * @param out Output stream
174     * @param coordinate Coordinate object which is written to the stream
175     */
176     friend QDataStream &operator<<(QDataStream &out, const GeoCoordinate &coordinate);
177
178     /**
179     * @brief Input operator
180     *
181     * @param in Input stream
182     * @param coordinate Object where the values from the stream are saved
183     */
184     friend QDataStream &operator>>(QDataStream &in, GeoCoordinate &coordinate);
185 };
186
187 QDebug operator<<(QDebug dbg, const GeoCoordinate &coordinate);
188
189 Q_DECLARE_METATYPE(GeoCoordinate)
190
191 #endif // GEOCOORDINATE_H