Fixed defects found in the review
[situare] / src / coordinates / geocoordinate.cpp
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 #include <cmath>
24
25 #include <QDebug>
26
27 #include "map/osm.h"
28 #include "scenecoordinate.h"
29
30 #include "geocoordinate.h"
31
32 bool GeoCoordinate::m_metaTypeIsRegistered = false;
33
34 GeoCoordinate::GeoCoordinate() :
35         m_latitude(0),
36         m_longitude(0),
37         m_validityFlags(NoCoordinatesSet)
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     registerMetaType();
42 }
43
44 GeoCoordinate::GeoCoordinate(double latitude, double longitude)
45 {
46     qDebug() << __PRETTY_FUNCTION__;
47
48     registerMetaType();
49
50     setLatitude(latitude);
51     setLongitude(longitude);
52 }
53
54 GeoCoordinate::GeoCoordinate(const SceneCoordinate &coordinate)
55 {
56     qDebug() << __PRETTY_FUNCTION__;
57
58     registerMetaType();
59
60     convertFrom(coordinate);
61 }
62
63 void GeoCoordinate::convertFrom(const SceneCoordinate &coordinate)
64 {
65     qDebug() << __PRETTY_FUNCTION__;
66
67     const int MAP_PIXELS_Y = OSM_MAP_MAX_PIXEL_Y + 1;
68
69     double longitude = coordinate.x() / OMS_MAP_PIXELS_X * 360.0 - 180;
70
71     double n = M_PI - 2.0 * M_PI * coordinate.y() / MAP_PIXELS_Y;
72     double latitude = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
73
74     setLatitude(latitude);
75     setLongitude(longitude);
76 }
77
78 bool GeoCoordinate::isNull() const
79 {
80     qDebug() << __PRETTY_FUNCTION__;
81
82     if ((m_latitude == 0) && (m_longitude == 0))
83         return true;
84
85     return false;
86 }
87
88 bool GeoCoordinate::isValid()
89 {
90     qDebug() << __PRETTY_FUNCTION__;
91
92     const double MAX_LATITUDE = 90;
93     const double MIN_LATITUDE = -90;
94     const double MIN_LONGITUDE = -180.0;
95     const double MAX_LONGITUDE = 180.0;
96
97     if ((m_validityFlags & LatitudeSet) && (m_validityFlags & LongitudeSet) &&
98         (m_latitude >= MIN_LATITUDE) && (m_latitude <= MAX_LATITUDE) &&
99         (m_longitude >= MIN_LONGITUDE) && (m_longitude <= MAX_LONGITUDE))
100
101         return true;
102     else
103         return false;
104 }
105
106 double GeoCoordinate::latitude() const
107 {
108     qDebug() << __PRETTY_FUNCTION__;
109
110     return m_latitude;
111 }
112
113 double GeoCoordinate::longitude() const
114 {
115     qDebug() << __PRETTY_FUNCTION__;
116
117     return m_longitude;
118 }
119
120 void GeoCoordinate::registerMetaType()
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     if (!m_metaTypeIsRegistered) {
125         qRegisterMetaType<GeoCoordinate>("GeoCoordinate");
126         qRegisterMetaTypeStreamOperators<GeoCoordinate>("GeoCoordinate");
127         m_metaTypeIsRegistered = true;
128     }
129 }
130
131 void GeoCoordinate::setLatitude(double latitude)
132 {
133     qDebug() << __PRETTY_FUNCTION__;
134
135     m_latitude = latitude;
136     m_validityFlags |= LatitudeSet;
137 }
138
139 void GeoCoordinate::setLongitude(double longitude)
140 {
141     qDebug() << __PRETTY_FUNCTION__;
142
143     m_longitude = longitude;
144     m_validityFlags |= LongitudeSet;
145 }
146
147 QDataStream &operator<<(QDataStream &out, const GeoCoordinate &coordinate)
148 {
149     out << coordinate.m_latitude << coordinate.m_longitude;
150     return out;
151 }
152
153 QDataStream &operator>>(QDataStream &in, GeoCoordinate &coordinate)
154 {
155     in >> coordinate.m_latitude >> coordinate.m_longitude;
156     return in;
157 }
158
159 QDebug operator<<(QDebug dbg, const GeoCoordinate &coordinate)
160 {
161     dbg.nospace() << "(" << coordinate.latitude() << ", " << coordinate.longitude() << ")";
162
163     return dbg.space();
164 }