From 0afdd75bf968ab65e864136e315c274b44a0bd47 Mon Sep 17 00:00:00 2001 From: Jussi Laitinen Date: Fri, 26 Mar 2010 15:14:16 +0200 Subject: [PATCH] Added MapEngine and MapFetcher files. --- src/map/mapengine.cpp | 38 +++++++++++++++++++++++++++++++ src/map/mapengine.h | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ src/map/mapfetcher.cpp | 49 ++++++++++++++++++++++++++++++++++++++++ src/map/mapfetcher.h | 32 ++++++++++++++++++++++++++ 4 files changed, 178 insertions(+) create mode 100644 src/map/mapengine.cpp create mode 100644 src/map/mapengine.h create mode 100644 src/map/mapfetcher.cpp create mode 100644 src/map/mapfetcher.h diff --git a/src/map/mapengine.cpp b/src/map/mapengine.cpp new file mode 100644 index 0000000..575f8c9 --- /dev/null +++ b/src/map/mapengine.cpp @@ -0,0 +1,38 @@ +#include +#include + +#include "map/mapengine.h" + +MapEngine::MapEngine(QObject *parent) + : QObject(parent) +{ + +} + +QPoint MapEngine::tileFromCoordinate(qreal latitude, qreal longitude, int zoom) +{ + //Power of two + qreal z = static_cast(1 << zoom); + + qreal x = static_cast((longitude + 180.0) / 360.0); + qreal y = static_cast((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0 + / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0); + + qDebug() << x*z << "," << y*z; + return QPoint(qFloor(x*z), qFloor(y*z)); +} + +qreal MapEngine::longitudeFromTileX(int x, int zoom) +{ + qreal z = static_cast(1 << zoom); + qreal lon = x / z * 360.0 - 180.0; + qDebug() << lon; + return lon; +} + +qreal MapEngine::latitudeFromTileY(int y, int zoom) +{ + qreal z = static_cast(1 << zoom); + qreal n = M_PI - 2 * M_PI * y / zoom; + return 180.0 / (M_PI * atan(0.5 * exp(n) - exp(-n))); +} diff --git a/src/map/mapengine.h b/src/map/mapengine.h new file mode 100644 index 0000000..c92ef88 --- /dev/null +++ b/src/map/mapengine.h @@ -0,0 +1,59 @@ +#ifndef MAPENGINE_H +#define MAPENGINE_H + +#include + + +/** +* @brief +* +* @class MapEngine mapengine.h "map/mapengine.h" +*/ +class MapEngine : public QObject +{ + Q_OBJECT + +public: + /** + * @brief Constructor for the MapEngine. + * + * @fn MapEngine + * @param parent QObject + */ + MapEngine(QObject *parent = 0); + + + /** + * @brief Transforms coordinates to tile x,y values. + * + * @fn tileFromCoordinate + * @param latitude latitude value + * @param longitude longitude value + * @param zoom zoom level + * @return QPoint tile x,y + */ + QPoint tileFromCoordinate(qreal latitude, qreal longitude, int zoom); + + /** + * @brief Transforms tile x value to longitude. + * + * @fn longitudeFromTile + * @param x tile x value + * @param zoom zoom value + * @return qreal longitude + */ + qreal longitudeFromTileX(int x, int zoom); + + /** + * @brief Transforms tile y value to latitude. + * + * @fn latitudeFromTile + * @param y tile y value + * @param zoom zoom value + * @return qreal latitude + */ + qreal latitudeFromTileY(int y, int zoom); + +}; + +#endif // MAPENGINE_H diff --git a/src/map/mapfetcher.cpp b/src/map/mapfetcher.cpp new file mode 100644 index 0000000..cdc7923 --- /dev/null +++ b/src/map/mapfetcher.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include + +#include "mapfetcher.h" + +MapFetcher::MapFetcher(QObject *parent) + : QObject(parent) +{ + connect(&m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*))); +} + +void MapFetcher::fetchMapImage(const QUrl &url) +{ + if (url.isEmpty()) + return; + + QNetworkRequest request(url); + request.setRawHeader("User-Agent", "Map Test"); + qDebug() << request.url(); + m_manager.get(request); +} + +void MapFetcher::downloadFinished(QNetworkReply *reply) +{ + qDebug() << "downloadFinished()"; + if (reply->error()) { + qDebug() << reply->errorString(); + emit error(reply->errorString()); + } + else { + QImage image; + QUrl url = reply->url(); + if (!image.load(reply, 0)) + image = QImage(); + + emit mapImageReceived(image); + } + + reply->deleteLater(); +} + +MapFetcher::~MapFetcher() +{ + +} diff --git a/src/map/mapfetcher.h b/src/map/mapfetcher.h new file mode 100644 index 0000000..12ba61f --- /dev/null +++ b/src/map/mapfetcher.h @@ -0,0 +1,32 @@ +#ifndef MAPFETCHER_H +#define MAPFETCHER_H + +#include +#include + +class QNetworkReply; +class QUrl; + +#include "mapfetcher.h" + +class MapFetcher : public QObject +{ + Q_OBJECT + +public: + MapFetcher(QObject *parent = 0); + ~MapFetcher(); + void fetchMapImage(const QUrl &url); + +signals: + void mapImageReceived(const QImage &image); + void error(const QString &message); + +public slots: + void downloadFinished(QNetworkReply *reply); + +private: + QNetworkAccessManager m_manager; +}; + +#endif -- 1.7.9.5