Re-factored the source to use the new coordinate classes
[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 GeoCoordinate::GeoCoordinate() :
33         m_latitude(0),
34         m_longitude(0)
35 {
36     qDebug() << __PRETTY_FUNCTION__;
37 }
38
39 GeoCoordinate::GeoCoordinate(double latitude, double longitude) :
40         m_latitude(latitude),
41         m_longitude(longitude)
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44 }
45
46 GeoCoordinate::GeoCoordinate(const SceneCoordinate &coordinate)
47 {
48     qDebug() << __PRETTY_FUNCTION__;
49
50     convertFrom(coordinate);
51 }
52
53 void GeoCoordinate::convertFrom(const SceneCoordinate &coordinate)
54 {
55     qDebug() << __PRETTY_FUNCTION__;
56
57     const int MAP_PIXELS_Y = OSM_MAP_MAX_PIXEL_Y + 1;
58
59     double longitude = coordinate.x() / OMS_MAP_PIXELS_X * 360.0 - 180;
60
61     double n = M_PI - 2.0 * M_PI * coordinate.y() / MAP_PIXELS_Y;
62     double latitude = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
63
64     setLatitude(latitude);
65     setLongitude(longitude);
66 }
67
68 bool GeoCoordinate::isNull() const
69 {
70     qDebug() << __PRETTY_FUNCTION__;
71
72     if (m_latitude == 0 && m_longitude == 0)
73         return true;
74
75     return false;
76 }
77
78 bool GeoCoordinate::isValid()
79 {
80     qDebug() << __PRETTY_FUNCTION__;
81
82     const double MAX_LATITUDE = 90;
83     const double MIN_LATITUDE = -90;
84     const double MIN_LONGITUDE = -180.0;
85     const double MAX_LONGITUDE = 180.0;
86
87     if ((m_latitude >= MIN_LATITUDE) && (m_latitude < MAX_LATITUDE) &&
88         (m_longitude >= MIN_LONGITUDE) && (m_longitude < MAX_LONGITUDE))
89
90         return true;
91     else
92         return false;
93 }
94
95 double GeoCoordinate::latitude() const
96 {
97     qDebug() << __PRETTY_FUNCTION__;
98
99     return m_latitude;
100 }
101
102 double GeoCoordinate::longitude() const
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     return m_longitude;
107 }
108
109 void GeoCoordinate::setLatitude(double latitude)
110 {
111     qDebug() << __PRETTY_FUNCTION__;
112
113     m_latitude = latitude;
114 }
115
116 void GeoCoordinate::setLongitude(double longitude)
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     m_longitude = longitude;
121 }
122
123 QDataStream &operator<<(QDataStream &out, const GeoCoordinate &coordinate)
124 {
125     out << coordinate.m_latitude << coordinate.m_longitude;
126     return out;
127 }
128
129 QDataStream &operator>>(QDataStream &in, GeoCoordinate &coordinate)
130 {
131     in >> coordinate.m_latitude >> coordinate.m_longitude;
132     return in;
133 }
134
135 QDebug operator<<(QDebug dbg, const GeoCoordinate &coordinate)
136 {
137     dbg.nospace() << "(" << coordinate.latitude() << ", " << coordinate.longitude() << ")";
138
139     return dbg.space();
140 }