Added MapEngine and MapFetcher files.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Fri, 26 Mar 2010 13:14:16 +0000 (15:14 +0200)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Fri, 26 Mar 2010 13:14:16 +0000 (15:14 +0200)
src/map/mapengine.cpp [new file with mode: 0644]
src/map/mapengine.h [new file with mode: 0644]
src/map/mapfetcher.cpp [new file with mode: 0644]
src/map/mapfetcher.h [new file with mode: 0644]

diff --git a/src/map/mapengine.cpp b/src/map/mapengine.cpp
new file mode 100644 (file)
index 0000000..575f8c9
--- /dev/null
@@ -0,0 +1,38 @@
+#include <QtCore>
+#include <QtGlobal>
+
+#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<qreal>(1 << zoom);
+
+    qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
+    qreal y = static_cast<qreal>((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<qreal>(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<qreal>(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 (file)
index 0000000..c92ef88
--- /dev/null
@@ -0,0 +1,59 @@
+#ifndef MAPENGINE_H
+#define MAPENGINE_H
+
+#include <QtCore>
+
+
+/**
+* @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 (file)
index 0000000..cdc7923
--- /dev/null
@@ -0,0 +1,49 @@
+#include <QNetworkAccessManager>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include <QUrl>
+#include <QDebug>
+#include <QPixmap>
+
+#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 (file)
index 0000000..12ba61f
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef MAPFETCHER_H
+#define MAPFETCHER_H
+
+#include <QtCore>
+#include <QNetworkAccessManager>
+
+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