5c57fdd214f7d7dc28977cdbb091db4f4b766a82
[situare] / src / map / mapengine.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        Jussi Laitinen - jussi.laitinen@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 <QDebug>
24 #include <QString>
25 #include <QStringList>
26 #include <QUrl>
27
28 #include "mapengine.h"
29 #include "maptile.h"
30 //#include "mapscene.h"
31
32 #include <QtCore>
33 #include <QtGlobal>
34
35 MapEngine::MapEngine(MapView *mapView, QWidget *parent)
36     : QObject(parent)
37 {
38     m_mapView = mapView;
39     m_mapScene = new MapScene(this);
40     mapView->setScene(m_mapScene);
41     m_zoomLevel = 14;
42
43     m_mapFetcher = new MapFetcher(this);
44 }
45
46 void MapEngine::setViewLocation(QPointF latLonCoordinate)
47 {
48     m_mapView->setZoomLevel(m_zoomLevel);
49
50     /// Show some dummy map tiles for demo purposes
51     for (int x=9351; x<=9354; x++) {
52         for (int y=4261; y<=4264; y++) {
53
54         }
55     }
56 }
57
58 void MapEngine::mapImageReceived(const QUrl *url, const QPixmap *pixmap)
59 {
60     //QString path = "http://tile.openstreetmap.org/mapnik/14/9353/4261.png";
61     QString path = url.path();
62     qDebug() << __PRETTY_FUNCTION__ << "path:" << path;
63
64     QStringList pathParts = path.split("/", QString::SkipEmptyParts);
65     qDebug() << __PRETTY_FUNCTION__ << "pathParts:" << pathParts;
66
67 //    int zoom = (pathParts.at(1)).toInt();
68 //    int x = (pathParts.at(2)).toInt();
69 //    int y = (pathParts.at(3)).toInt();
70
71 //    MapTile *mapTile = new MapTile();
72 //    mapTile->setZoomLevel(zoom);
73 //    mapTile->setTileNumber(QPoint(x, y));
74 //    mapTile->setPixmap();
75 //    m_mapScene->addMapTile(mapTile);
76 }
77
78 QPoint MapEngine::latLonToTile(qreal latitude, qreal longitude, int zoom)
79 {
80     qDebug() << __PRETTY_FUNCTION__;
81     qreal z = static_cast<qreal>(1 << zoom);
82
83     qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
84     qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
85                                 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
86
87     return QPoint(qFloor(x*z), qFloor(y*z));
88 }
89
90 qreal MapEngine::tileXToLongitude(int x, int zoom)
91 {
92     qDebug() << __PRETTY_FUNCTION__;
93     qreal z = static_cast<qreal>(1 << zoom);
94     qreal lon = x / z * 360.0 - 180.0;
95     qDebug() << lon;
96     return lon;
97 }
98
99 qreal MapEngine::tileYToLatitude(int y, int zoom)
100 {
101     qDebug() << __PRETTY_FUNCTION__;
102     qreal z = static_cast<qreal>(1 << zoom);
103     qreal n = M_PI - 2 * M_PI * y / zoom;
104     return 180.0 / (M_PI * atan(0.5 * exp(n) - exp(-n)));
105 }