Removed debug messages.
[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
31 #include <QtCore>
32 #include <QtGlobal>
33
34 MapEngine::MapEngine(QObject *parent)
35     : QObject(parent)
36     , m_zoomLevel(14)
37 {
38     m_mapScene = new MapScene(this);
39
40     m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
41     connect(this, SIGNAL(fetchImage(QUrl)), m_mapFetcher, SLOT(fetchMapImage(QUrl)));
42     connect(m_mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)), this,
43             SLOT(mapImageReceived(QUrl, QPixmap)));
44 }
45
46 void MapEngine::setViewLocation(QPointF latLonCoordinate)
47 {
48     emit zoomLevelChanged(m_zoomLevel);
49
50     /*/// Fetch some map tiles for demo purposes
51     for (int x=9348; x<=9357; x++) {
52         for (int y=4259; y<=4266; y++) {
53             QUrl url = buildURL(m_zoomLevel, QPoint(x, y));
54             m_mapFetcher->fetchMapImage(url);
55         }
56     }*/
57
58     QPoint tileCoord = convertLatLonToTile(m_zoomLevel, latLonCoordinate);
59     qDebug() << "Tile coord: " << tileCoord.x() << "," << tileCoord.y();
60     QPoint sceneCoord = convertTileNumberToSceneCoordinate(m_zoomLevel, tileCoord);
61     qDebug() << "Scene coord: " << sceneCoord.x() << "," << sceneCoord.y();
62
63     setLocation(sceneCoord);
64     qDebug() << "centerToScene: " << sceneCoord.x() << "," << sceneCoord.y();
65     emit centerToSceneCoordinates(sceneCoord);
66 }
67
68 QUrl MapEngine::buildURL(int zoomLevel, QPoint tileNumbers)
69 {
70     QString url = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png")
71                   .arg(zoomLevel).arg(tileNumbers.x()).arg(tileNumbers.y());
72
73     return QUrl(url);
74 }
75
76 void MapEngine::parseURL(const QUrl &url, int &zoom, int &x, int &y)
77 {
78     QString path = url.path();
79     QStringList pathParts = path.split("/", QString::SkipEmptyParts);
80
81     int size = pathParts.size();
82
83     if (size < 3)
84         return;
85
86     zoom = (pathParts.at(size-3)).toInt();
87     x = (pathParts.at(size-2)).toInt();
88     QString yString = pathParts.at(size-1);
89     yString.chop(4);
90     y = yString.toInt();
91 }
92
93 void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
94 {
95     int zoom = -1;
96     int x = -1;
97     int y = -1;
98
99     parseURL(url, zoom, x, y);
100
101     MapTile *mapTile = new MapTile();
102     mapTile->setZoomLevel(zoom);
103     mapTile->setTileNumber(QPoint(x, y));
104     mapTile->setPixmap(pixmap); 
105
106     mapTilesInScene.insert(url.toString(), mapTile);
107     m_mapScene->addMapTile(mapTile);
108
109     qDebug() << "Tile count: " << mapTilesInScene.size();
110 }
111
112 QGraphicsScene* MapEngine::scene()
113 {
114     return dynamic_cast<QGraphicsScene *>(m_mapScene);
115 }
116
117 int MapEngine::tileMaxValue(int zoomLevel)
118 {
119     return (1 << zoomLevel) - 1;
120 }
121
122 QRect MapEngine::calculateGrid(QPointF sceneCoordinate)
123 {
124     QPoint tileCenterCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
125
126     int topLeftX = tileCenterCoordinate.x() - (GRID_WIDTH/2);
127     int topLeftY = tileCenterCoordinate.y() - (GRID_HEIGHT/2);
128
129    return QRect(topLeftX, topLeftY, GRID_WIDTH, GRID_HEIGHT);
130 }
131
132 void MapEngine::setLocation(QPointF sceneCoordinate)
133 {
134     emit locationChanged(sceneCoordinate);
135
136     QRect grid = calculateGrid(sceneCoordinate);
137     int topLeftX = grid.topLeft().x();
138     int topLeftY = grid.topLeft().y();
139     int bottomRightX = grid.bottomRight().x();
140     int bottomRightY = grid.bottomRight().y();
141
142     qDebug() << topLeftX << "," << topLeftY;
143     qDebug() << bottomRightY << "," << bottomRightX;
144
145     int tileMaxVal = tileMaxValue(m_zoomLevel);
146
147     for (int x = topLeftX; x <= bottomRightX; ++x) {
148         for (int y = topLeftY; y <= bottomRightY; ++y) {
149
150             int tileX = x;
151             int tileY = y;
152
153             if (tileX < 0)
154                 tileX += tileMaxVal;
155             else if (tileX > tileMaxVal)
156                 tileX -= tileMaxVal;
157
158             if (tileY < 0)
159                 tileY += tileMaxVal;
160             else if (tileY > tileMaxVal)
161                 tileY -= tileMaxVal;
162
163             QUrl url = buildURL(m_zoomLevel, QPoint(tileX, tileY));
164
165             if (!mapTilesInScene.contains(url.toString())) {
166                 emit fetchImage(buildURL(m_zoomLevel, QPoint(tileX, tileY)));   
167             }
168         }
169     }
170 }
171