1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-31 09:05:14 +01:00

Refactoring

This commit is contained in:
Martin Tůma 2018-09-25 21:07:44 +02:00
parent bc6d48d1fe
commit bb7787b001
6 changed files with 62 additions and 62 deletions

View File

@ -9,10 +9,10 @@
static int limitZoom(int zoom) static int limitZoom(int zoom)
{ {
if (zoom < osm::zooms.min()) if (zoom < OSM::ZOOMS.min())
return osm::zooms.min(); return OSM::ZOOMS.min();
if (zoom > osm::zooms.max()) if (zoom > OSM::ZOOMS.max())
return osm::zooms.max(); return OSM::ZOOMS.max();
return zoom; return zoom;
} }
@ -20,23 +20,23 @@ static int limitZoom(int zoom)
EmptyMap::EmptyMap(QObject *parent) : Map(parent) EmptyMap::EmptyMap(QObject *parent) : Map(parent)
{ {
_zoom = osm::zooms.max(); _zoom = OSM::ZOOMS.max();
} }
QRectF EmptyMap::bounds() QRectF EmptyMap::bounds()
{ {
return QRectF(ll2xy(osm::bounds.topLeft()), ll2xy(osm::bounds.bottomRight())); return QRectF(ll2xy(OSM::BOUNDS.topLeft()), ll2xy(OSM::BOUNDS.bottomRight()));
} }
int EmptyMap::zoomFit(const QSize &size, const RectC &rect) int EmptyMap::zoomFit(const QSize &size, const RectC &rect)
{ {
if (!rect.isValid()) if (!rect.isValid())
_zoom = osm::zooms.max(); _zoom = OSM::ZOOMS.max();
else { else {
QRectF tbr(osm::ll2m(rect.topLeft()), osm::ll2m(rect.bottomRight())); QRectF tbr(OSM::ll2m(rect.topLeft()), OSM::ll2m(rect.bottomRight()));
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height()); QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
_zoom = limitZoom(osm::scale2zoom(qMax(sc.x(), -sc.y()), TILE_SIZE)); _zoom = limitZoom(OSM::scale2zoom(qMax(sc.x(), -sc.y()), TILE_SIZE));
} }
return _zoom; return _zoom;
@ -44,18 +44,18 @@ int EmptyMap::zoomFit(const QSize &size, const RectC &rect)
qreal EmptyMap::resolution(const QRectF &rect) qreal EmptyMap::resolution(const QRectF &rect)
{ {
return osm::resolution(rect.center(), _zoom, TILE_SIZE); return OSM::resolution(rect.center(), _zoom, TILE_SIZE);
} }
int EmptyMap::zoomIn() int EmptyMap::zoomIn()
{ {
_zoom = qMin(_zoom + 1, osm::zooms.max()); _zoom = qMin(_zoom + 1, OSM::ZOOMS.max());
return _zoom; return _zoom;
} }
int EmptyMap::zoomOut() int EmptyMap::zoomOut()
{ {
_zoom = qMax(_zoom - 1, osm::zooms.min()); _zoom = qMax(_zoom - 1, OSM::ZOOMS.min());
return _zoom; return _zoom;
} }
@ -68,13 +68,13 @@ void EmptyMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
QPointF EmptyMap::ll2xy(const Coordinates &c) QPointF EmptyMap::ll2xy(const Coordinates &c)
{ {
qreal scale = osm::zoom2scale(_zoom, TILE_SIZE); qreal scale = OSM::zoom2scale(_zoom, TILE_SIZE);
QPointF m = osm::ll2m(c); QPointF m = OSM::ll2m(c);
return QPointF(m.x() / scale, m.y() / -scale); return QPointF(m.x() / scale, m.y() / -scale);
} }
Coordinates EmptyMap::xy2ll(const QPointF &p) Coordinates EmptyMap::xy2ll(const QPointF &p)
{ {
qreal scale = osm::zoom2scale(_zoom, TILE_SIZE); qreal scale = OSM::zoom2scale(_zoom, TILE_SIZE);
return osm::m2ll(QPointF(p.x() * scale, -p.y() * scale)); return OSM::m2ll(QPointF(p.x() * scale, -p.y() * scale));
} }

View File

@ -8,7 +8,7 @@
#include "mapsource.h" #include "mapsource.h"
MapSource::Config::Config() : type(OSM), zooms(osm::zooms), bounds(osm::bounds), MapSource::Config::Config() : type(OSM), zooms(OSM::ZOOMS), bounds(OSM::BOUNDS),
format("image/png"), rest(false), tileRatio(1.0) {} format("image/png"), rest(false), tileRatio(1.0) {}
@ -31,21 +31,21 @@ Range MapSource::zooms(QXmlStreamReader &reader)
if (attr.hasAttribute("min")) { if (attr.hasAttribute("min")) {
min = attr.value("min").toString().toInt(&res); min = attr.value("min").toString().toInt(&res);
if (!res || !osm::zooms.contains(min)) { if (!res || !OSM::ZOOMS.contains(min)) {
reader.raiseError("Invalid minimal zoom level"); reader.raiseError("Invalid minimal zoom level");
return Range(); return Range();
} }
} else } else
min = osm::zooms.min(); min = OSM::ZOOMS.min();
if (attr.hasAttribute("max")) { if (attr.hasAttribute("max")) {
max = attr.value("max").toString().toInt(&res); max = attr.value("max").toString().toInt(&res);
if (!res || !osm::zooms.contains(max)) { if (!res || !OSM::ZOOMS.contains(max)) {
reader.raiseError("Invalid maximal zoom level"); reader.raiseError("Invalid maximal zoom level");
return Range(); return Range();
} }
} else } else
max = osm::zooms.max(); max = OSM::ZOOMS.max();
if (min > max) { if (min > max) {
reader.raiseError("Invalid maximal/minimal zoom level combination"); reader.raiseError("Invalid maximal/minimal zoom level combination");
@ -63,41 +63,41 @@ RectC MapSource::bounds(QXmlStreamReader &reader)
if (attr.hasAttribute("top")) { if (attr.hasAttribute("top")) {
top = attr.value("top").toString().toDouble(&res); top = attr.value("top").toString().toDouble(&res);
if (!res || (top < osm::bounds.bottom() || top > osm::bounds.top())) { if (!res || (top < OSM::BOUNDS.bottom() || top > OSM::BOUNDS.top())) {
reader.raiseError("Invalid bounds top value"); reader.raiseError("Invalid bounds top value");
return RectC(); return RectC();
} }
} else } else
top = osm::bounds.top(); top = OSM::BOUNDS.top();
if (attr.hasAttribute("bottom")) { if (attr.hasAttribute("bottom")) {
bottom = attr.value("bottom").toString().toDouble(&res); bottom = attr.value("bottom").toString().toDouble(&res);
if (!res || (bottom < osm::bounds.bottom() if (!res || (bottom < OSM::BOUNDS.bottom()
|| bottom > osm::bounds.top())) { || bottom > OSM::BOUNDS.top())) {
reader.raiseError("Invalid bounds bottom value"); reader.raiseError("Invalid bounds bottom value");
return RectC(); return RectC();
} }
} else } else
bottom = osm::bounds.bottom(); bottom = OSM::BOUNDS.bottom();
if (attr.hasAttribute("left")) { if (attr.hasAttribute("left")) {
left = attr.value("left").toString().toDouble(&res); left = attr.value("left").toString().toDouble(&res);
if (!res || (left < osm::bounds.left() || left > osm::bounds.right())) { if (!res || (left < OSM::BOUNDS.left() || left > OSM::BOUNDS.right())) {
reader.raiseError("Invalid bounds left value"); reader.raiseError("Invalid bounds left value");
return RectC(); return RectC();
} }
} else } else
left = osm::bounds.left(); left = OSM::BOUNDS.left();
if (attr.hasAttribute("right")) { if (attr.hasAttribute("right")) {
right = attr.value("right").toString().toDouble(&res); right = attr.value("right").toString().toDouble(&res);
if (!res || (right < osm::bounds.left() if (!res || (right < OSM::BOUNDS.left()
|| right > osm::bounds.right())) { || right > OSM::BOUNDS.right())) {
reader.raiseError("Invalid bounds right value"); reader.raiseError("Invalid bounds right value");
return RectC(); return RectC();
} }
} else } else
right = osm::bounds.right(); right = OSM::BOUNDS.right();
if (bottom >= top) { if (bottom >= top) {
reader.raiseError("Invalid bottom/top bounds combination"); reader.raiseError("Invalid bottom/top bounds combination");

View File

@ -73,11 +73,11 @@ MBTilesMap::MBTilesMap(const QString &fileName, QObject *parent)
qMax(0, query.value(2).toInt())) + 1, _zooms.min()); qMax(0, query.value(2).toInt())) + 1, _zooms.min());
double maxY = index2mercator(qMin((1<<_zooms.min()) - 1, double maxY = index2mercator(qMin((1<<_zooms.min()) - 1,
qMax(0, query.value(3).toInt())) + 1, _zooms.min()); qMax(0, query.value(3).toInt())) + 1, _zooms.min());
Coordinates tl(osm::m2ll(QPointF(minX, maxY))); Coordinates tl(OSM::m2ll(QPointF(minX, maxY)));
Coordinates br(osm::m2ll(QPointF(maxX, minY))); Coordinates br(OSM::m2ll(QPointF(maxX, minY)));
// Workaround of broken zoom levels 0 and 1 due to numerical instability // Workaround of broken zoom levels 0 and 1 due to numerical instability
tl.rlat() = qMin(tl.lat(), osm::bounds.top()); tl.rlat() = qMin(tl.lat(), OSM::BOUNDS.top());
br.rlat() = qMax(br.lat(), osm::bounds.bottom()); br.rlat() = qMax(br.lat(), OSM::BOUNDS.bottom());
_bounds = RectC(tl, br); _bounds = RectC(tl, br);
} }
@ -151,9 +151,9 @@ int MBTilesMap::zoomFit(const QSize &size, const RectC &rect)
if (!rect.isValid()) if (!rect.isValid())
_zoom = _zooms.max(); _zoom = _zooms.max();
else { else {
QRectF tbr(osm::ll2m(rect.topLeft()), osm::ll2m(rect.bottomRight())); QRectF tbr(OSM::ll2m(rect.topLeft()), OSM::ll2m(rect.bottomRight()));
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height()); QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
_zoom = limitZoom(osm::scale2zoom(qMax(sc.x(), -sc.y()) _zoom = limitZoom(OSM::scale2zoom(qMax(sc.x(), -sc.y())
/ coordinatesRatio(), _tileSize)); / coordinatesRatio(), _tileSize));
} }
@ -162,7 +162,7 @@ int MBTilesMap::zoomFit(const QSize &size, const RectC &rect)
qreal MBTilesMap::resolution(const QRectF &rect) qreal MBTilesMap::resolution(const QRectF &rect)
{ {
return osm::resolution(rect.center(), _zoom, _tileSize); return OSM::resolution(rect.center(), _zoom, _tileSize);
} }
int MBTilesMap::zoomIn() int MBTilesMap::zoomIn()
@ -211,11 +211,11 @@ QByteArray MBTilesMap::tileData(int zoom, const QPoint &tile) const
void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags) void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{ {
Q_UNUSED(flags); Q_UNUSED(flags);
qreal scale = osm::zoom2scale(_zoom, _tileSize); qreal scale = OSM::zoom2scale(_zoom, _tileSize);
QRectF b(bounds()); QRectF b(bounds());
QPoint tile = osm::mercator2tile(QPointF(rect.topLeft().x() * scale, QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
-rect.topLeft().y() * scale) * coordinatesRatio(), _zoom); -rect.topLeft().y() * scale) * coordinatesRatio(), _zoom);
QPointF tl(floor(rect.left() / tileSize()) QPointF tl(floor(rect.left() / tileSize())
* tileSize(), floor(rect.top() / tileSize()) * tileSize()); * tileSize(), floor(rect.top() / tileSize()) * tileSize());
@ -247,14 +247,14 @@ void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
QPointF MBTilesMap::ll2xy(const Coordinates &c) QPointF MBTilesMap::ll2xy(const Coordinates &c)
{ {
qreal scale = osm::zoom2scale(_zoom, _tileSize); qreal scale = OSM::zoom2scale(_zoom, _tileSize);
QPointF m = osm::ll2m(c); QPointF m = OSM::ll2m(c);
return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio(); return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio();
} }
Coordinates MBTilesMap::xy2ll(const QPointF &p) Coordinates MBTilesMap::xy2ll(const QPointF &p)
{ {
qreal scale = osm::zoom2scale(_zoom, _tileSize); qreal scale = OSM::zoom2scale(_zoom, _tileSize);
return osm::m2ll(QPointF(p.x() * scale, -p.y() * scale) return OSM::m2ll(QPointF(p.x() * scale, -p.y() * scale)
* coordinatesRatio()); * coordinatesRatio());
} }

View File

@ -41,9 +41,9 @@ int OnlineMap::zoomFit(const QSize &size, const RectC &rect)
if (!rect.isValid()) if (!rect.isValid())
_zoom = _zooms.max(); _zoom = _zooms.max();
else { else {
QRectF tbr(osm::ll2m(rect.topLeft()), osm::ll2m(rect.bottomRight())); QRectF tbr(OSM::ll2m(rect.topLeft()), OSM::ll2m(rect.bottomRight()));
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height()); QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
_zoom = limitZoom(osm::scale2zoom(qMax(sc.x(), -sc.y()) _zoom = limitZoom(OSM::scale2zoom(qMax(sc.x(), -sc.y())
/ coordinatesRatio(), TILE_SIZE)); / coordinatesRatio(), TILE_SIZE));
} }
@ -52,7 +52,7 @@ int OnlineMap::zoomFit(const QSize &size, const RectC &rect)
qreal OnlineMap::resolution(const QRectF &rect) qreal OnlineMap::resolution(const QRectF &rect)
{ {
return osm::resolution(rect.center(), _zoom, TILE_SIZE); return OSM::resolution(rect.center(), _zoom, TILE_SIZE);
} }
int OnlineMap::zoomIn() int OnlineMap::zoomIn()
@ -84,10 +84,10 @@ qreal OnlineMap::tileSize() const
void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags) void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{ {
qreal scale = osm::zoom2scale(_zoom, TILE_SIZE); qreal scale = OSM::zoom2scale(_zoom, TILE_SIZE);
QRectF b(bounds()); QRectF b(bounds());
QPoint tile = osm::mercator2tile(QPointF(rect.topLeft().x() * scale, QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
-rect.topLeft().y() * scale) * coordinatesRatio(), _zoom); -rect.topLeft().y() * scale) * coordinatesRatio(), _zoom);
QPointF tl(floor(rect.left() / tileSize()) QPointF tl(floor(rect.left() / tileSize())
* tileSize(), floor(rect.top() / tileSize()) * tileSize()); * tileSize(), floor(rect.top() / tileSize()) * tileSize());
@ -122,14 +122,14 @@ void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
QPointF OnlineMap::ll2xy(const Coordinates &c) QPointF OnlineMap::ll2xy(const Coordinates &c)
{ {
qreal scale = osm::zoom2scale(_zoom, TILE_SIZE); qreal scale = OSM::zoom2scale(_zoom, TILE_SIZE);
QPointF m = osm::ll2m(c); QPointF m = OSM::ll2m(c);
return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio(); return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio();
} }
Coordinates OnlineMap::xy2ll(const QPointF &p) Coordinates OnlineMap::xy2ll(const QPointF &p)
{ {
qreal scale = osm::zoom2scale(_zoom, TILE_SIZE); qreal scale = OSM::zoom2scale(_zoom, TILE_SIZE);
return osm::m2ll(QPointF(p.x() * scale, -p.y() * scale) return OSM::m2ll(QPointF(p.x() * scale, -p.y() * scale)
* coordinatesRatio()); * coordinatesRatio());
} }

View File

@ -4,33 +4,33 @@
#define EPSILON 1e-6 #define EPSILON 1e-6
QPointF osm::ll2m(const Coordinates &c) QPointF OSM::ll2m(const Coordinates &c)
{ {
return QPointF(c.lon(), rad2deg(log(tan(M_PI_4 + deg2rad(c.lat())/2.0)))); return QPointF(c.lon(), rad2deg(log(tan(M_PI_4 + deg2rad(c.lat())/2.0))));
} }
Coordinates osm::m2ll(const QPointF &p) Coordinates OSM::m2ll(const QPointF &p)
{ {
return Coordinates(p.x(), rad2deg(2.0 * atan(exp(deg2rad(p.y()))) - M_PI_2)); return Coordinates(p.x(), rad2deg(2.0 * atan(exp(deg2rad(p.y()))) - M_PI_2));
} }
QPoint osm::mercator2tile(const QPointF &m, int zoom) QPoint OSM::mercator2tile(const QPointF &m, int zoom)
{ {
return QPoint((int)(floor((m.x() + 180.0) / 360.0 * (1<<zoom))), return QPoint((int)(floor((m.x() + 180.0) / 360.0 * (1<<zoom))),
(int)(floor((1.0 - (m.y() / 180.0)) / 2.0 * (1<<zoom)))); (int)(floor((1.0 - (m.y() / 180.0)) / 2.0 * (1<<zoom))));
} }
qreal osm::zoom2scale(int zoom, int tileSize) qreal OSM::zoom2scale(int zoom, int tileSize)
{ {
return (360.0/(qreal)((1<<zoom) * tileSize)); return (360.0/(qreal)((1<<zoom) * tileSize));
} }
int osm::scale2zoom(qreal scale, int tileSize) int OSM::scale2zoom(qreal scale, int tileSize)
{ {
return (int)(log2(360.0/(scale * (qreal)tileSize)) + EPSILON); return (int)(log2(360.0/(scale * (qreal)tileSize)) + EPSILON);
} }
qreal osm::resolution(const QPointF &p, int zoom, int tileSize) qreal OSM::resolution(const QPointF &p, int zoom, int tileSize)
{ {
qreal scale = zoom2scale(zoom, tileSize); qreal scale = zoom2scale(zoom, tileSize);

View File

@ -6,11 +6,11 @@
#include <common/rectc.h> #include <common/rectc.h>
#include <common/range.h> #include <common/range.h>
namespace osm namespace OSM
{ {
static const RectC bounds(Coordinates(-180, 85.0511), static const RectC BOUNDS(Coordinates(-180, 85.0511),
Coordinates(180, -85.0511)); Coordinates(180, -85.0511));
static const Range zooms(0, 19); static const Range ZOOMS(0, 19);
QPointF ll2m(const Coordinates &c); QPointF ll2m(const Coordinates &c);
Coordinates m2ll(const QPointF &p); Coordinates m2ll(const QPointF &p);