2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Sami Rämö - sami.ramo@ixonos.com
6 Jussi Laitinen - jussi.laitinen@ixonos.com
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.
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.
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,
25 #include <QStringList>
28 #include "mapengine.h"
34 MapEngine::MapEngine(QObject *parent)
38 m_mapScene = new MapScene(this);
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)));
46 void MapEngine::setViewLocation(QPointF latLonCoordinate)
48 emit zoomLevelChanged(m_zoomLevel);
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);
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();
63 setLocation(sceneCoord);
64 qDebug() << "centerToScene: " << sceneCoord.x() << "," << sceneCoord.y();
65 emit centerToSceneCoordinates(sceneCoord);
68 QUrl MapEngine::buildURL(int zoomLevel, QPoint tileNumbers)
70 QString url = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png")
71 .arg(zoomLevel).arg(tileNumbers.x()).arg(tileNumbers.y());
76 void MapEngine::parseURL(const QUrl &url, int &zoom, int &x, int &y)
78 QString path = url.path();
79 QStringList pathParts = path.split("/", QString::SkipEmptyParts);
81 int size = pathParts.size();
86 zoom = (pathParts.at(size-3)).toInt();
87 x = (pathParts.at(size-2)).toInt();
88 QString yString = pathParts.at(size-1);
93 void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
99 parseURL(url, zoom, x, y);
101 MapTile *mapTile = new MapTile();
102 mapTile->setZoomLevel(zoom);
103 mapTile->setTileNumber(QPoint(x, y));
104 mapTile->setPixmap(pixmap);
106 mapTilesInScene.insert(url.toString(), mapTile);
107 m_mapScene->addMapTile(mapTile);
109 qDebug() << "Tile count: " << mapTilesInScene.size();
112 QGraphicsScene* MapEngine::scene()
114 return dynamic_cast<QGraphicsScene *>(m_mapScene);
117 int MapEngine::tileMaxValue(int zoomLevel)
119 return (1 << zoomLevel) - 1;
122 QRect MapEngine::calculateGrid(QPointF sceneCoordinate)
124 QPoint tileCenterCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCoordinate);
126 int topLeftX = tileCenterCoordinate.x() - (GRID_WIDTH/2);
127 int topLeftY = tileCenterCoordinate.y() - (GRID_HEIGHT/2);
129 return QRect(topLeftX, topLeftY, GRID_WIDTH, GRID_HEIGHT);
132 void MapEngine::setLocation(QPointF sceneCoordinate)
134 emit locationChanged(sceneCoordinate);
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();
142 qDebug() << topLeftX << "," << topLeftY;
143 qDebug() << bottomRightY << "," << bottomRightX;
145 int tileMaxVal = tileMaxValue(m_zoomLevel);
147 for (int x = topLeftX; x <= bottomRightX; ++x) {
148 for (int y = topLeftY; y <= bottomRightY; ++y) {
155 else if (tileX > tileMaxVal)
160 else if (tileY > tileMaxVal)
163 QUrl url = buildURL(m_zoomLevel, QPoint(tileX, tileY));
165 if (!mapTilesInScene.contains(url.toString())) {
166 emit fetchImage(buildURL(m_zoomLevel, QPoint(tileX, tileY)));