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"
30 //#include "mapscene.h"
35 MapEngine::MapEngine(MapView *mapView, QWidget *parent)
39 m_mapScene = new MapScene(this);
40 mapView->setScene(m_mapScene);
43 m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
44 connect(m_mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)), this,
45 SLOT(mapImageReceived(QUrl, QPixmap)));
49 * @todo Create URL builder method
50 * QUrl buildURL(zoomLevel, QPoint tileNumbers)
52 void MapEngine::setViewLocation(QPointF latLonCoordinate)
54 m_mapView->setZoomLevel(m_zoomLevel);
56 /// Fetch some map tiles for demo purposes
57 for (int x=9351; x<=9354; x++) {
58 for (int y=4261; y<=4264; y++) {
59 QString url = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png")
60 .arg(m_zoomLevel).arg(x).arg(y);
61 m_mapFetcher->fetchMapImage(QUrl(url));
67 * @todo Create URL parser methor and refactor code (use pointers for returning)
69 void MapEngine::mapImageReceived(const QUrl &url, const QPixmap &pixmap)
72 QString path = url.path();
73 //qDebug() << __PRETTY_FUNCTION__ << "path:" << path;
75 QStringList pathParts = path.split("/", QString::SkipEmptyParts);
77 int zoom = (pathParts.at(1)).toInt();
78 int x = (pathParts.at(2)).toInt();
79 QString yString = pathParts.at(3);
81 int y = yString.toInt();
83 //qDebug() << __PRETTY_FUNCTION__ << zoom << x << y;
85 MapTile *mapTile = new MapTile();
86 mapTile->setZoomLevel(zoom);
87 mapTile->setTileNumber(QPoint(x, y));
88 mapTile->setPixmap(pixmap);
89 m_mapScene->addMapTile(mapTile);
93 * @todo QPoint convertLatLonToTile(int zoomLevel, QPointF latLonCoordinate), convert to static
95 QPoint MapEngine::latLonToTile(qreal latitude, qreal longitude, int zoom)
97 qDebug() << __PRETTY_FUNCTION__;
98 qreal z = static_cast<qreal>(1 << zoom);
100 qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
101 qreal y = static_cast<qreal>((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0
102 / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0);
104 return QPoint(qFloor(x*z), qFloor(y*z));
110 qreal MapEngine::tileXToLongitude(int x, int zoom)
112 qDebug() << __PRETTY_FUNCTION__;
113 qreal z = static_cast<qreal>(1 << zoom);
114 qreal lon = x / z * 360.0 - 180.0;
119 qreal MapEngine::tileYToLatitude(int y, int zoom)
121 qDebug() << __PRETTY_FUNCTION__;
122 qreal z = static_cast<qreal>(1 << zoom);
123 qreal n = M_PI - 2 * M_PI * y / zoom;
124 return 180.0 / (M_PI * atan(0.5 * exp(n) - exp(-n)));