From 351fc0309ab6dddb63daedcfc513c11c944849f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Mon, 24 Sep 2018 22:49:10 +0200 Subject: [PATCH] Code cleanup --- src/map/emptymap.cpp | 60 ++++++++++++------------------------------ src/map/mbtilesmap.cpp | 6 +---- src/map/onlinemap.cpp | 21 +++------------ src/map/onlinemap.h | 6 ----- src/map/osm.cpp | 16 ++++++++--- src/map/osm.h | 3 ++- src/map/tileloader.cpp | 6 ++++- src/map/tileloader.h | 3 +-- src/map/wmsmap.cpp | 8 +----- src/map/wmtsmap.cpp | 8 +----- 10 files changed, 44 insertions(+), 93 deletions(-) diff --git a/src/map/emptymap.cpp b/src/map/emptymap.cpp index 65e430a0..430e68dc 100644 --- a/src/map/emptymap.cpp +++ b/src/map/emptymap.cpp @@ -1,41 +1,18 @@ #include #include -#include "common/coordinates.h" #include "common/rectc.h" -#include "common/wgs84.h" +#include "osm.h" #include "emptymap.h" -#define ZOOM_MIN 0 -#define ZOOM_MAX 19 #define TILE_SIZE 256 -static QPointF ll2m(const Coordinates &c) -{ - return QPointF(c.lon(), rad2deg(log(tan(M_PI_4 + deg2rad(c.lat())/2.0)))); -} - -static Coordinates m2ll(const QPointF &p) -{ - return Coordinates(p.x(), rad2deg(2.0 * atan(exp(deg2rad(p.y()))) - M_PI_2)); -} - -static qreal zoom2scale(int zoom) -{ - return (360.0/(qreal)((1< ZOOM_MAX) - return ZOOM_MAX; + if (zoom < osm::zooms.min()) + return osm::zooms.min(); + if (zoom > osm::zooms.max()) + return osm::zooms.max(); return zoom; } @@ -43,23 +20,23 @@ static int limitZoom(int zoom) EmptyMap::EmptyMap(QObject *parent) : Map(parent) { - _zoom = ZOOM_MAX; + _zoom = osm::zooms.max(); } QRectF EmptyMap::bounds() { - return QRectF(ll2xy(Coordinates(-180, 85)), ll2xy(Coordinates(180, -85))); + return QRectF(ll2xy(osm::bounds.topLeft()), ll2xy(osm::bounds.bottomRight())); } int EmptyMap::zoomFit(const QSize &size, const RectC &rect) { if (!rect.isValid()) - _zoom = ZOOM_MAX; + _zoom = osm::zooms.max(); else { - QRectF tbr(ll2m(rect.topLeft()), ll2m(rect.bottomRight())); + QRectF tbr(osm::ll2m(rect.topLeft()), osm::ll2m(rect.bottomRight())); QPointF sc(tbr.width() / size.width(), tbr.height() / size.height()); - _zoom = limitZoom(scale2zoom(qMax(sc.x(), -sc.y()))); + _zoom = limitZoom(osm::scale2zoom(qMax(sc.x(), -sc.y()), TILE_SIZE)); } return _zoom; @@ -67,21 +44,18 @@ int EmptyMap::zoomFit(const QSize &size, const RectC &rect) qreal EmptyMap::resolution(const QRectF &rect) { - qreal scale = zoom2scale(_zoom); - - return (WGS84_RADIUS * 2.0 * M_PI * scale / 360.0 - * cos(2.0 * atan(exp(deg2rad(-rect.center().y() * scale))) - M_PI/2)); + return osm::resolution(rect.center(), _zoom, TILE_SIZE); } int EmptyMap::zoomIn() { - _zoom = qMin(_zoom + 1, ZOOM_MAX); + _zoom = qMin(_zoom + 1, osm::zooms.max()); return _zoom; } int EmptyMap::zoomOut() { - _zoom = qMax(_zoom - 1, ZOOM_MIN); + _zoom = qMax(_zoom - 1, osm::zooms.min()); return _zoom; } @@ -94,13 +68,13 @@ void EmptyMap::draw(QPainter *painter, const QRectF &rect, Flags flags) QPointF EmptyMap::ll2xy(const Coordinates &c) { - qreal scale = zoom2scale(_zoom); - QPointF m = ll2m(c); + qreal scale = osm::zoom2scale(_zoom, TILE_SIZE); + QPointF m = osm::ll2m(c); return QPointF(m.x() / scale, m.y() / -scale); } Coordinates EmptyMap::xy2ll(const QPointF &p) { - qreal scale = zoom2scale(_zoom); - return m2ll(QPointF(p.x() * scale, -p.y() * scale)); + qreal scale = osm::zoom2scale(_zoom, TILE_SIZE); + return osm::m2ll(QPointF(p.x() * scale, -p.y() * scale)); } diff --git a/src/map/mbtilesmap.cpp b/src/map/mbtilesmap.cpp index 01b439c1..c7380f9d 100644 --- a/src/map/mbtilesmap.cpp +++ b/src/map/mbtilesmap.cpp @@ -5,7 +5,6 @@ #include #include #include "common/rectc.h" -#include "common/wgs84.h" #include "osm.h" #include "config.h" #include "mbtilesmap.h" @@ -163,10 +162,7 @@ int MBTilesMap::zoomFit(const QSize &size, const RectC &rect) qreal MBTilesMap::resolution(const QRectF &rect) { - qreal scale = osm::zoom2scale(_zoom, _tileSize); - - return (WGS84_RADIUS * 2.0 * M_PI * scale / 360.0 - * cos(2.0 * atan(exp(deg2rad(-rect.center().y() * scale))) - M_PI/2)); + return osm::resolution(rect.center(), _zoom, _tileSize); } int MBTilesMap::zoomIn() diff --git a/src/map/onlinemap.cpp b/src/map/onlinemap.cpp index f0e0f20a..b9df7f31 100644 --- a/src/map/onlinemap.cpp +++ b/src/map/onlinemap.cpp @@ -1,7 +1,5 @@ #include -#include "common/coordinates.h" #include "common/rectc.h" -#include "common/wgs84.h" #include "downloader.h" #include "osm.h" #include "config.h" @@ -15,22 +13,12 @@ OnlineMap::OnlineMap(const QString &name, const QString &url, const Authorization &authorization, bool invertY, QObject *parent) : Map(parent), _name(name), _zooms(zooms), _bounds(bounds), _zoom(_zooms.max()), _deviceRatio(1.0), _tileRatio(tileRatio), - _invertY(invertY), _valid(false) + _invertY(invertY) { - QString dir(TILES_DIR + "/" + _name); - - _tileLoader = new TileLoader(this); + _tileLoader = new TileLoader(TILES_DIR + "/" + _name, this); _tileLoader->setUrl(url); - _tileLoader->setDir(dir); _tileLoader->setAuthorization(authorization); connect(_tileLoader, SIGNAL(finished()), this, SIGNAL(loaded())); - - if (!QDir().mkpath(dir)) { - _errorString = "Error creating tiles dir"; - return; - } - - _valid = true; } QRectF OnlineMap::bounds() @@ -64,10 +52,7 @@ int OnlineMap::zoomFit(const QSize &size, const RectC &rect) qreal OnlineMap::resolution(const QRectF &rect) { - qreal scale = osm::zoom2scale(_zoom, TILE_SIZE); - - return (WGS84_RADIUS * 2.0 * M_PI * scale / 360.0 - * cos(2.0 * atan(exp(deg2rad(-rect.center().y() * scale))) - M_PI/2)); + return osm::resolution(rect.center(), _zoom, TILE_SIZE); } int OnlineMap::zoomIn() diff --git a/src/map/onlinemap.h b/src/map/onlinemap.h index a6a3b67a..146e3638 100644 --- a/src/map/onlinemap.h +++ b/src/map/onlinemap.h @@ -34,9 +34,6 @@ public: void setDevicePixelRatio(qreal ratio) {_deviceRatio = ratio;} void clearCache() {_tileLoader->clearCache();} - bool isValid() const {return _valid;} - QString errorString() const {return _errorString;} - private: int limitZoom(int zoom) const; qreal tileSize() const; @@ -50,9 +47,6 @@ private: int _zoom; qreal _deviceRatio, _tileRatio; bool _invertY; - - bool _valid; - QString _errorString; }; #endif // ONLINEMAP_H diff --git a/src/map/osm.cpp b/src/map/osm.cpp index 4fee38e6..9a2aa50b 100644 --- a/src/map/osm.cpp +++ b/src/map/osm.cpp @@ -1,5 +1,7 @@ +#include "common/wgs84.h" #include "osm.h" + #define EPSILON 1e-6 QPointF osm::ll2m(const Coordinates &c) @@ -12,10 +14,10 @@ Coordinates osm::m2ll(const QPointF &p) return Coordinates(p.x(), rad2deg(2.0 * atan(exp(deg2rad(p.y()))) - M_PI_2)); } -QPoint osm::mercator2tile(const QPointF &m, int z) +QPoint osm::mercator2tile(const QPointF &m, int zoom) { - return QPoint((int)(floor((m.x() + 180.0) / 360.0 * (1<setDir(tilesDir()); + _tileLoader = new TileLoader(tilesDir(), this); _tileLoader->setAuthorization(_setup.authorization()); connect(_tileLoader, SIGNAL(finished()), this, SIGNAL(loaded())); diff --git a/src/map/wmtsmap.cpp b/src/map/wmtsmap.cpp index 7a5856a2..9ec21a79 100644 --- a/src/map/wmtsmap.cpp +++ b/src/map/wmtsmap.cpp @@ -39,13 +39,7 @@ WMTSMap::WMTSMap(const QString &name, const WMTS::Setup &setup, qreal tileRatio, QObject *parent) : Map(parent), _name(name), _setup(setup), _tileLoader(0), _zoom(0), _deviceRatio(1.0), _tileRatio(tileRatio), _valid(false) { - if (!QDir().mkpath(tilesDir())) { - _errorString = "Error creating tiles dir"; - return; - } - - _tileLoader = new TileLoader(this); - _tileLoader->setDir(tilesDir()); + _tileLoader = new TileLoader(tilesDir(), this); _tileLoader->setAuthorization(_setup.authorization()); connect(_tileLoader, SIGNAL(finished()), this, SIGNAL(loaded()));