1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 05:34:47 +01:00

Code cleanup

This commit is contained in:
Martin Tůma 2018-09-24 22:49:10 +02:00
parent d8116c7f5d
commit 351fc0309a
10 changed files with 44 additions and 93 deletions

View File

@ -1,41 +1,18 @@
#include <QtGlobal>
#include <QPainter>
#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) * TILE_SIZE));
}
static int scale2zoom(qreal scale)
{
return (int)log2(360.0/(scale * (qreal)TILE_SIZE));
}
static int limitZoom(int zoom)
{
if (zoom < ZOOM_MIN)
return ZOOM_MIN;
if (zoom > 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));
}

View File

@ -5,7 +5,6 @@
#include <QPainter>
#include <QPixmapCache>
#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()

View File

@ -1,7 +1,5 @@
#include <QPainter>
#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()

View File

@ -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

View File

@ -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<<z))),
(int)(floor((1.0 - (m.y() / 180.0)) / 2.0 * (1<<z))));
return QPoint((int)(floor((m.x() + 180.0) / 360.0 * (1<<zoom))),
(int)(floor((1.0 - (m.y() / 180.0)) / 2.0 * (1<<zoom))));
}
qreal osm::zoom2scale(int zoom, int tileSize)
@ -27,3 +29,11 @@ int osm::scale2zoom(qreal scale, int tileSize)
{
return (int)(log2(360.0/(scale * (qreal)tileSize)) + EPSILON);
}
qreal osm::resolution(const QPointF &p, int zoom, int tileSize)
{
qreal scale = zoom2scale(zoom, tileSize);
return (WGS84_RADIUS * 2.0 * M_PI * scale / 360.0
* cos(2.0 * atan(exp(deg2rad(-p.y() * scale))) - M_PI/2));
}

View File

@ -14,9 +14,10 @@ namespace osm
QPointF ll2m(const Coordinates &c);
Coordinates m2ll(const QPointF &p);
QPoint mercator2tile(const QPointF &m, int z);
QPoint mercator2tile(const QPointF &m, int zoom);
qreal zoom2scale(int zoom, int tileSize);
int scale2zoom(qreal scale, int tileSize);
qreal resolution(const QPointF &p, int zoom, int tileSize);
}
#endif // OSM_H

View File

@ -14,8 +14,12 @@ static bool loadTileFile(Tile &tile, const QString &file)
return true;
}
TileLoader::TileLoader(QObject *parent) : QObject(parent)
TileLoader::TileLoader(const QString &dir, QObject *parent)
: QObject(parent), _dir(dir)
{
if (!QDir().mkpath(_dir))
qWarning("%s: %s\n", qPrintable(_dir), "Error creating tiles directory");
_downloader = new Downloader(this);
connect(_downloader, SIGNAL(finished()), this, SIGNAL(finished()));
}

View File

@ -11,10 +11,9 @@ class TileLoader : public QObject
Q_OBJECT
public:
TileLoader(QObject *parent = 0);
TileLoader(const QString &dir, QObject *parent = 0);
void setUrl(const QString &url) {_url = url;}
void setDir(const QString &dir) {_dir = dir;}
void setAuthorization(const Authorization &authorization)
{_authorization = authorization;}

View File

@ -105,13 +105,7 @@ WMSMap::WMSMap(const QString &name, const WMS::Setup &setup, QObject *parent)
: Map(parent), _name(name), _setup(setup), _tileLoader(0), _zoom(0),
_ratio(1.0), _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()));

View File

@ -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()));