static const qreal MAX_LATITUDE = 85.0511; ///< Maximum latitude value
static const qreal MIN_LONGITUDE = -180.0; ///< Minimum longitude value
static const qreal MAX_LONGITUDE = 180.0; ///< Maximum longitude value
+static const int GRID_WIDTH = 4;
+static const int GRID_HEIGHT = 4;
/**
* \var UNDEFINED
* \brief Value to be used when zoom level, tile numbers or position are not defined
MapEngine::MapEngine(QObject *parent)
: QObject(parent)
+ , m_zoomLevel(14)
{
m_mapScene = new MapScene(this);
- m_zoomLevel = 14;
m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
+ connect(this, SIGNAL(fetchImage(QUrl)), m_mapFetcher, SLOT(fetchMapImage(QUrl)));
connect(m_mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap)), this,
SLOT(mapImageReceived(QUrl, QPixmap)));
}
{
emit zoomLevelChanged(m_zoomLevel);
- /// Fetch some map tiles for demo purposes
- for (int x=9351; x<=9354; x++) {
- for (int y=4261; y<=4264; y++) {
+ /*/// Fetch some map tiles for demo purposes
+ for (int x=9348; x<=9357; x++) {
+ for (int y=4259; y<=4266; y++) {
QUrl url = buildURL(m_zoomLevel, QPoint(x, y));
m_mapFetcher->fetchMapImage(url);
}
- }
+ }*/
+
+ QPoint tileCoord = convertLatLonToTile(m_zoomLevel, latLonCoordinate);
+ qDebug() << "Tile coord: " << tileCoord.x() << "," << tileCoord.y();
+ QPoint sceneCoord = convertTileNumberToSceneCoordinate(m_zoomLevel, tileCoord);
+ qDebug() << "Scene coord: " << sceneCoord.x() << "," << sceneCoord.y();
+
+ setLocation(sceneCoord);
}
QUrl MapEngine::buildURL(int zoomLevel, QPoint tileNumbers)
{
return dynamic_cast<QGraphicsScene *>(m_mapScene);
}
+
+int MapEngine::tileMaxValue(int zoomLevel)
+{
+ return (1 << zoomLevel) - 1;
+}
+
+QRect MapEngine::calculateGrid(QPointF sceneCenterCoordinate)
+{
+
+ QPoint tileCenterCoordinate = convertSceneCoordinateToTileNumber(m_zoomLevel, sceneCenterCoordinate);
+
+ qDebug() << tileCenterCoordinate.x() - (GRID_WIDTH/2);
+ int topLeftX = tileCenterCoordinate.x() - (GRID_WIDTH/2);
+ int topLeftY = tileCenterCoordinate.y() - (GRID_HEIGHT/2);
+
+ return QRect(topLeftX, topLeftY, GRID_WIDTH, GRID_HEIGHT);
+}
+
+void MapEngine::setLocation(QPointF sceneCenterCoordinate)
+{
+ QRect grid = calculateGrid(sceneCenterCoordinate);
+ int topLeftX = grid.topLeft().x();
+ int topLeftY = grid.topLeft().y();
+ int bottomRightX = grid.bottomRight().x();
+ int bottomRightY = grid.bottomRight().y();
+
+ qDebug() << topLeftX << "," << topLeftY;
+ qDebug() << bottomRightY << "," << bottomRightX;
+
+ int tileMaxVal = tileMaxValue(m_zoomLevel);
+
+ for (int x = topLeftX; x <= bottomRightX; ++x) {
+ for (int y = topLeftY; y <= bottomRightY; ++y) {
+
+ int tileX = x;
+ int tileY = y;
+
+ if (tileX < 0)
+ tileX += tileMaxVal;
+ else if (tileX > tileMaxVal)
+ tileX -= tileMaxVal;
+
+ if (tileY < 0)
+ tileY += tileMaxVal;
+ else if (tileY > tileMaxVal)
+ tileY -= tileMaxVal;
+
+ qDebug() << QUrl(buildURL(m_zoomLevel, QPoint(tileX, tileY))).toString();
+ emit fetchImage(buildURL(m_zoomLevel, QPoint(tileX, tileY)));
+ }
+ }
+}
return QPoint(x, y);
}
+ static QPoint convertSceneCoordinateToTileNumber(int zoomLevel, QPointF sceneCoordinate)
+ {
+ int pow = 1 << (MAX_ZOOM_LEVEL - zoomLevel);
+ int x = static_cast<int>(sceneCoordinate.x()) / (TILE_SIZE_X*pow);
+ int y = static_cast<int>(sceneCoordinate.y()) / (TILE_SIZE_Y*pow);
+
+ return QPoint(x, y);
+ }
+
/**
* @brief Getter for scene
*
return QPoint(qFloor(x*z), qFloor(y*z));
}
+ QRect calculateGrid(QPointF sceneCenterCoordinate);
+
+public slots:
+ void setLocation(QPointF sceneCenterCoordinate);
+
private:
/**
* @brief Build URL for donwloading single map tile from OpenStreetMap tile server
*/
void parseURL(const QUrl &url, int &zoom, int &x, int &y);
+ int tileMaxValue(int zoomLevel);
+
+
private slots:
/**
* @brief Slot for received map tile images
*/
void zoomLevelChanged(int newZoomLevel);
+ void fetchImage(const QUrl &url);
+
private:
MapScene *m_mapScene; ///< Scene for map tiles
MapFetcher *m_mapFetcher; ///< Fetcher for map tiles
int m_zoomLevel; ///< Current zoom level
+ QStringList mapTilesInScene; ///< List of map tiles in map scene
};
#endif // MAPENGINE_H
*/
MapFetcher(QNetworkAccessManager *manager, QObject *parent = 0);
- ~MapFetcher();
-
/**
- * @brief Fetch image from given URL.
+ * @brief Destructor for MapFetcher.
*
- * @param url URL to image
+ * @fn ~MapFetcher
*/
- void fetchMapImage(const QUrl &url);
+ ~MapFetcher();
signals:
/**
*/
void error(const QString &message);
+public slots:
+ /**
+ * @brief Fetch image from given URL.
+ *
+ * @param url URL to image
+ */
+ void fetchMapImage(const QUrl &url);
+
private slots:
/**
{
QHBoxLayout *mapViewLayout = new QHBoxLayout;
MapView *mapView = new MapView(this);
+
+ //DEBUG
+ QVBoxLayout *topAreaLayout = new QVBoxLayout;
+ search = new QPushButton("Show", this);
+ topAreaLayout->addWidget(&lonLine);
+ topAreaLayout->addWidget(&latLine);
+ topAreaLayout->addWidget(search);
+ QWidget *topArea = new QWidget(this);
+ topArea->setLayout(topAreaLayout);
+ mapViewLayout->addWidget(topArea);
+ connect(search, SIGNAL(clicked()), this, SLOT(show()));
+ //DEBUG
+
mapViewLayout->addWidget(mapView);
setLayout(mapViewLayout);
- MapEngine *mapEngine = new MapEngine(this);
+ mapEngine = new MapEngine(this);
mapView->setScene(mapEngine->scene());
connect(mapEngine, SIGNAL(zoomLevelChanged(int)), mapView, SLOT(setZoomLevel(int)));
mapEngine->setViewLocation(QPointF(25.5000, 65.0000));
}
+
+void MapViewScreen::show()
+{
+ qreal lon = lonLine.text().toFloat();
+ qreal lat = latLine.text().toFloat();
+
+ mapEngine->setViewLocation(QPointF(lon, lat));
+}
#include <QtGui>
+#include "map/mapengine.h"
+
/**
* @brief Map View class. Used to display Map
*
*/
class MapViewScreen : public QWidget
{
+ Q_OBJECT
+
public:
MapViewScreen(QWidget *parent = 0);
+
+public slots:
+ void show();
+
+private:
+ //DEBUG
+ QLineEdit latLine;
+ QLineEdit lonLine;
+ QPushButton *search;
+ MapEngine *mapEngine;
+
};
#endif // MAPVIEWTAB_H
{
Q_OBJECT
private slots:
- void convertTileNumberToSceneCoordinate();
+ //void convertTileNumberToSceneCoordinate();
// void setViewLocation();
- void convertLatLonToTile();
+ //void convertLatLonToTile();
+ void calculateRect();
+ void setLocation();
};
/**
*
* Different zoom levels are also tested
*/
-void TestMapEngine::convertTileNumberToSceneCoordinate()
-{
- QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(18, QPoint(0,0)), QPoint(0,0));
- QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(18, QPoint(1,2)), QPoint(256,512));
- QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(16, QPoint(3,4)), QPoint(3072,4096));
-}
+//void TestMapEngine::convertTileNumberToSceneCoordinate()
+//{
+// QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(18, QPoint(0,0)), QPoint(0,0));
+// QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(18, QPoint(1,2)), QPoint(256,512));
+// QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(16, QPoint(3,4)), QPoint(3072,4096));
+//}
/**
* @brief DUMMY TESTCASE!
* @brief Test converting real world cordinates to tile numbers
* @todo Implement
*/
-void TestMapEngine::convertLatLonToTile()
+//void TestMapEngine::convertLatLonToTile()
+//{
+// QCOMPARE(MapEngine::convertLatLonToTile(7, QPointF(25.5, 65.0)), QPoint(73, 33));
+// QCOMPARE(MapEngine::convertLatLonToTile(1, QPointF(25.5, 65.0)), QPoint(1, 0));
+// QCOMPARE(MapEngine::convertLatLonToTile(1, QPointF(-190.0, 65.0)), QPoint(UNDEFINED, UNDEFINED));
+// QCOMPARE(MapEngine::convertLatLonToTile(100, QPointF(20.0, 65.0)), QPoint(UNDEFINED, UNDEFINED));
+//}
+
+void TestMapEngine::calculateRect()
+{
+// MapEngine engine;
+//
+// QRect grid1 = QRect(0, 0, GRID_WIDTH, GRID_HEIGHT);
+// QCOMPARE(engine.calculateGrid(QPointF(550.23, 550.23)), grid1);
+//
+// QRect grid2 = QRect(-2, -2, GRID_WIDTH, GRID_HEIGHT);
+// QCOMPARE(engine.calculateGrid(QPointF(0.23, 0.23)), grid2);
+//
+// QRect grid3 = QRect(1018, 498, GRID_WIDTH, GRID_HEIGHT);
+// QCOMPARE(engine.calculateGrid(QPointF(1020*TILE_SIZE_X, 500*TILE_SIZE_Y)), grid3);
+}
+
+void TestMapEngine::setLocation()
{
- QCOMPARE(MapEngine::convertLatLonToTile(7, QPointF(25.5, 65.0)), QPoint(73, 33));
- QCOMPARE(MapEngine::convertLatLonToTile(1, QPointF(25.5, 65.0)), QPoint(1, 0));
- QCOMPARE(MapEngine::convertLatLonToTile(1, QPointF(-190.0, 65.0)), QPoint(UNDEFINED, UNDEFINED));
- QCOMPARE(MapEngine::convertLatLonToTile(100, QPointF(20.0, 65.0)), QPoint(UNDEFINED, UNDEFINED));
+ MapEngine engine;
+
+ QSignalSpy fetchImageSpy(&engine, SIGNAL(fetchImage(QUrl)));
+
+ engine.setLocation(QPointF(550.23, 550.23));
+ QTest::qWait(1000);
+ QCOMPARE(fetchImageSpy.count(), GRID_WIDTH*GRID_HEIGHT);
}
QTEST_MAIN(TestMapEngine)
DEPENDPATH += .
INCLUDEPATH += . \
../../../src/
-DEFINES += QT_NO_DEBUG_OUTPUT
+#DEFINES += QT_NO_DEBUG_OUTPUT
# Input
SOURCES += testmapengine.cpp \
../../../src/map/mapscene.h \
../../../src/map/maptile.h \
../../../src/map/mapview.h \
- ../../../src/map/mapfetcher.h
+ ../../../src/map/mapfetcher.h \
+ ../../../src/common.h
RESOURCES +=