Created osm.h, re-factoring, unit test script bug fix
[situare] / src / coordinates / geocoordinate.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Sami Rämö - sami.ramo@ixonos.com
6
7     Situare is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     version 2 as published by the Free Software Foundation.
10
11     Situare is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Situare; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19     USA.
20 */
21
22 #include <cmath>
23
24 #include <QDebug>
25
26 #include "map/osm.h"
27 #include "scenecoordinate.h"
28
29 #include "geocoordinate.h"
30
31 GeoCoordinate::GeoCoordinate() :
32         m_latitude(0),
33         m_longitude(0)
34 {
35     qDebug() << __PRETTY_FUNCTION__;
36 }
37
38 GeoCoordinate::GeoCoordinate(double latitude, double longitude) :
39         m_latitude(latitude),
40         m_longitude(longitude)
41 {
42     qDebug() << __PRETTY_FUNCTION__;
43 }
44
45 GeoCoordinate::GeoCoordinate(SceneCoordinate &coordinate)
46 {
47     qDebug() << __PRETTY_FUNCTION__;
48
49     convertFrom(coordinate);
50 }
51
52 void GeoCoordinate::convertFrom(const SceneCoordinate &coordinate)
53 {
54     qDebug() << __PRETTY_FUNCTION__;
55
56     const int MAP_PIXELS_Y = OSM_MAP_MAX_PIXEL_Y + 1;
57
58     double longitude = coordinate.x() / OMS_MAP_PIXELS_X * 360.0 - 180;
59
60     double n = M_PI - 2.0 * M_PI * coordinate.y() / MAP_PIXELS_Y;
61     double latitude = 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
62
63     setLatitude(latitude);
64     setLongitude(longitude);
65 }
66
67 bool GeoCoordinate::isNull() const
68 {
69     qDebug() << __PRETTY_FUNCTION__;
70
71     if (m_latitude == 0 && m_longitude == 0)
72         return true;
73
74     return false;
75 }
76
77 double GeoCoordinate::latitude() const
78 {
79     qDebug() << __PRETTY_FUNCTION__;
80
81     return m_latitude;
82 }
83
84 double GeoCoordinate::longitude() const
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87
88     return m_longitude;
89 }
90
91 void GeoCoordinate::setLatitude(double latitude)
92 {
93     qDebug() << __PRETTY_FUNCTION__;
94
95     m_latitude = latitude;
96 }
97
98 void GeoCoordinate::setLongitude(double longitude)
99 {
100     qDebug() << __PRETTY_FUNCTION__;
101
102     m_longitude = longitude;
103 }
104
105 QDebug operator<<(QDebug dbg, const GeoCoordinate &coordinate)
106 {
107     dbg.nospace() << "(" << coordinate.latitude() << ", " << coordinate.longitude() << ")";
108
109     return dbg.space();
110 }