mirror of
https://github.com/tumic0/GPXSee.git
synced 2025-01-31 09:05:14 +01:00
Refactoring
This commit is contained in:
parent
bc6d48d1fe
commit
bb7787b001
@ -9,10 +9,10 @@
|
||||
|
||||
static int limitZoom(int zoom)
|
||||
{
|
||||
if (zoom < osm::zooms.min())
|
||||
return osm::zooms.min();
|
||||
if (zoom > osm::zooms.max())
|
||||
return osm::zooms.max();
|
||||
if (zoom < OSM::ZOOMS.min())
|
||||
return OSM::ZOOMS.min();
|
||||
if (zoom > OSM::ZOOMS.max())
|
||||
return OSM::ZOOMS.max();
|
||||
|
||||
return zoom;
|
||||
}
|
||||
@ -20,23 +20,23 @@ static int limitZoom(int zoom)
|
||||
|
||||
EmptyMap::EmptyMap(QObject *parent) : Map(parent)
|
||||
{
|
||||
_zoom = osm::zooms.max();
|
||||
_zoom = OSM::ZOOMS.max();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (!rect.isValid())
|
||||
_zoom = osm::zooms.max();
|
||||
_zoom = OSM::ZOOMS.max();
|
||||
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());
|
||||
|
||||
_zoom = limitZoom(osm::scale2zoom(qMax(sc.x(), -sc.y()), TILE_SIZE));
|
||||
_zoom = limitZoom(OSM::scale2zoom(qMax(sc.x(), -sc.y()), TILE_SIZE));
|
||||
}
|
||||
|
||||
return _zoom;
|
||||
@ -44,18 +44,18 @@ int EmptyMap::zoomFit(const QSize &size, const RectC &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()
|
||||
{
|
||||
_zoom = qMin(_zoom + 1, osm::zooms.max());
|
||||
_zoom = qMin(_zoom + 1, OSM::ZOOMS.max());
|
||||
return _zoom;
|
||||
}
|
||||
|
||||
int EmptyMap::zoomOut()
|
||||
{
|
||||
_zoom = qMax(_zoom - 1, osm::zooms.min());
|
||||
_zoom = qMax(_zoom - 1, OSM::ZOOMS.min());
|
||||
return _zoom;
|
||||
}
|
||||
|
||||
@ -68,13 +68,13 @@ void EmptyMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
|
||||
|
||||
QPointF EmptyMap::ll2xy(const Coordinates &c)
|
||||
{
|
||||
qreal scale = osm::zoom2scale(_zoom, TILE_SIZE);
|
||||
QPointF m = osm::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 = osm::zoom2scale(_zoom, TILE_SIZE);
|
||||
return osm::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));
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
#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) {}
|
||||
|
||||
|
||||
@ -31,21 +31,21 @@ Range MapSource::zooms(QXmlStreamReader &reader)
|
||||
|
||||
if (attr.hasAttribute("min")) {
|
||||
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");
|
||||
return Range();
|
||||
}
|
||||
} else
|
||||
min = osm::zooms.min();
|
||||
min = OSM::ZOOMS.min();
|
||||
|
||||
if (attr.hasAttribute("max")) {
|
||||
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");
|
||||
return Range();
|
||||
}
|
||||
} else
|
||||
max = osm::zooms.max();
|
||||
max = OSM::ZOOMS.max();
|
||||
|
||||
if (min > max) {
|
||||
reader.raiseError("Invalid maximal/minimal zoom level combination");
|
||||
@ -63,41 +63,41 @@ RectC MapSource::bounds(QXmlStreamReader &reader)
|
||||
|
||||
if (attr.hasAttribute("top")) {
|
||||
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");
|
||||
return RectC();
|
||||
}
|
||||
} else
|
||||
top = osm::bounds.top();
|
||||
top = OSM::BOUNDS.top();
|
||||
|
||||
if (attr.hasAttribute("bottom")) {
|
||||
bottom = attr.value("bottom").toString().toDouble(&res);
|
||||
if (!res || (bottom < osm::bounds.bottom()
|
||||
|| bottom > osm::bounds.top())) {
|
||||
if (!res || (bottom < OSM::BOUNDS.bottom()
|
||||
|| bottom > OSM::BOUNDS.top())) {
|
||||
reader.raiseError("Invalid bounds bottom value");
|
||||
return RectC();
|
||||
}
|
||||
} else
|
||||
bottom = osm::bounds.bottom();
|
||||
bottom = OSM::BOUNDS.bottom();
|
||||
|
||||
if (attr.hasAttribute("left")) {
|
||||
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");
|
||||
return RectC();
|
||||
}
|
||||
} else
|
||||
left = osm::bounds.left();
|
||||
left = OSM::BOUNDS.left();
|
||||
|
||||
if (attr.hasAttribute("right")) {
|
||||
right = attr.value("right").toString().toDouble(&res);
|
||||
if (!res || (right < osm::bounds.left()
|
||||
|| right > osm::bounds.right())) {
|
||||
if (!res || (right < OSM::BOUNDS.left()
|
||||
|| right > OSM::BOUNDS.right())) {
|
||||
reader.raiseError("Invalid bounds right value");
|
||||
return RectC();
|
||||
}
|
||||
} else
|
||||
right = osm::bounds.right();
|
||||
right = OSM::BOUNDS.right();
|
||||
|
||||
if (bottom >= top) {
|
||||
reader.raiseError("Invalid bottom/top bounds combination");
|
||||
|
@ -73,11 +73,11 @@ MBTilesMap::MBTilesMap(const QString &fileName, QObject *parent)
|
||||
qMax(0, query.value(2).toInt())) + 1, _zooms.min());
|
||||
double maxY = index2mercator(qMin((1<<_zooms.min()) - 1,
|
||||
qMax(0, query.value(3).toInt())) + 1, _zooms.min());
|
||||
Coordinates tl(osm::m2ll(QPointF(minX, maxY)));
|
||||
Coordinates br(osm::m2ll(QPointF(maxX, minY)));
|
||||
Coordinates tl(OSM::m2ll(QPointF(minX, maxY)));
|
||||
Coordinates br(OSM::m2ll(QPointF(maxX, minY)));
|
||||
// Workaround of broken zoom levels 0 and 1 due to numerical instability
|
||||
tl.rlat() = qMin(tl.lat(), osm::bounds.top());
|
||||
br.rlat() = qMax(br.lat(), osm::bounds.bottom());
|
||||
tl.rlat() = qMin(tl.lat(), OSM::BOUNDS.top());
|
||||
br.rlat() = qMax(br.lat(), OSM::BOUNDS.bottom());
|
||||
_bounds = RectC(tl, br);
|
||||
}
|
||||
|
||||
@ -151,9 +151,9 @@ int MBTilesMap::zoomFit(const QSize &size, const RectC &rect)
|
||||
if (!rect.isValid())
|
||||
_zoom = _zooms.max();
|
||||
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());
|
||||
_zoom = limitZoom(osm::scale2zoom(qMax(sc.x(), -sc.y())
|
||||
_zoom = limitZoom(OSM::scale2zoom(qMax(sc.x(), -sc.y())
|
||||
/ coordinatesRatio(), _tileSize));
|
||||
}
|
||||
|
||||
@ -162,7 +162,7 @@ int MBTilesMap::zoomFit(const QSize &size, const RectC &rect)
|
||||
|
||||
qreal MBTilesMap::resolution(const QRectF &rect)
|
||||
{
|
||||
return osm::resolution(rect.center(), _zoom, _tileSize);
|
||||
return OSM::resolution(rect.center(), _zoom, _tileSize);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
Q_UNUSED(flags);
|
||||
qreal scale = osm::zoom2scale(_zoom, _tileSize);
|
||||
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
|
||||
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);
|
||||
QPointF tl(floor(rect.left() / 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)
|
||||
{
|
||||
qreal scale = osm::zoom2scale(_zoom, _tileSize);
|
||||
QPointF m = osm::ll2m(c);
|
||||
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
|
||||
QPointF m = OSM::ll2m(c);
|
||||
return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio();
|
||||
}
|
||||
|
||||
Coordinates MBTilesMap::xy2ll(const QPointF &p)
|
||||
{
|
||||
qreal scale = osm::zoom2scale(_zoom, _tileSize);
|
||||
return osm::m2ll(QPointF(p.x() * scale, -p.y() * scale)
|
||||
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
|
||||
return OSM::m2ll(QPointF(p.x() * scale, -p.y() * scale)
|
||||
* coordinatesRatio());
|
||||
}
|
||||
|
@ -41,9 +41,9 @@ int OnlineMap::zoomFit(const QSize &size, const RectC &rect)
|
||||
if (!rect.isValid())
|
||||
_zoom = _zooms.max();
|
||||
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());
|
||||
_zoom = limitZoom(osm::scale2zoom(qMax(sc.x(), -sc.y())
|
||||
_zoom = limitZoom(OSM::scale2zoom(qMax(sc.x(), -sc.y())
|
||||
/ coordinatesRatio(), TILE_SIZE));
|
||||
}
|
||||
|
||||
@ -52,7 +52,7 @@ int OnlineMap::zoomFit(const QSize &size, const RectC &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()
|
||||
@ -84,10 +84,10 @@ qreal OnlineMap::tileSize() const
|
||||
|
||||
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());
|
||||
|
||||
QPoint tile = osm::mercator2tile(QPointF(rect.topLeft().x() * scale,
|
||||
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
|
||||
-rect.topLeft().y() * scale) * coordinatesRatio(), _zoom);
|
||||
QPointF tl(floor(rect.left() / 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)
|
||||
{
|
||||
qreal scale = osm::zoom2scale(_zoom, TILE_SIZE);
|
||||
QPointF m = osm::ll2m(c);
|
||||
qreal scale = OSM::zoom2scale(_zoom, TILE_SIZE);
|
||||
QPointF m = OSM::ll2m(c);
|
||||
return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio();
|
||||
}
|
||||
|
||||
Coordinates OnlineMap::xy2ll(const QPointF &p)
|
||||
{
|
||||
qreal scale = osm::zoom2scale(_zoom, TILE_SIZE);
|
||||
return osm::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)
|
||||
* coordinatesRatio());
|
||||
}
|
||||
|
@ -4,33 +4,33 @@
|
||||
|
||||
#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))));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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))),
|
||||
(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));
|
||||
}
|
||||
|
||||
int osm::scale2zoom(qreal scale, int tileSize)
|
||||
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 OSM::resolution(const QPointF &p, int zoom, int tileSize)
|
||||
{
|
||||
qreal scale = zoom2scale(zoom, tileSize);
|
||||
|
||||
|
@ -6,11 +6,11 @@
|
||||
#include <common/rectc.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));
|
||||
static const Range zooms(0, 19);
|
||||
static const Range ZOOMS(0, 19);
|
||||
|
||||
QPointF ll2m(const Coordinates &c);
|
||||
Coordinates m2ll(const QPointF &p);
|
||||
|
Loading…
x
Reference in New Issue
Block a user