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

Fixed broken GeoTIFF Mercator coordinates transformation

This commit is contained in:
Martin Tůma 2018-02-16 19:37:04 +01:00
parent dc25290637
commit 3def9f95b0
3 changed files with 33 additions and 13 deletions

View File

@ -3,13 +3,23 @@
#include "common/coordinates.h"
#include "common/rectc.h"
#include "common/wgs84.h"
#include "mercator.h"
#include "emptymap.h"
#define SCALE_MIN 0.5
#define SCALE_MAX 1.0E-6
static QPointF ll2m(const Coordinates &c)
{
return QPointF(c.lon(), rad2deg(log(tan(M_PI/4.0 + deg2rad(c.lat())/2.0))));
}
static Coordinates m2ll(const QPointF &p)
{
return Coordinates(p.x(), rad2deg(2 * atan(exp(deg2rad(p.y()))) - M_PI/2));
}
EmptyMap::EmptyMap(QObject *parent) : Map(parent)
{
_scale = SCALE_MAX;
@ -25,8 +35,7 @@ qreal EmptyMap::zoomFit(const QSize &size, const RectC &br)
if (!br.isValid())
_scale = SCALE_MAX;
else {
QRectF tbr(Mercator().ll2xy(br.topLeft()),
Mercator().ll2xy(br.bottomRight()));
QRectF tbr(ll2m(br.topLeft()), ll2m(br.bottomRight()));
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
_scale = qMax(sc.x(), sc.y());
}
@ -73,12 +82,12 @@ void EmptyMap::draw(QPainter *painter, const QRectF &rect)
QPointF EmptyMap::ll2xy(const Coordinates &c) const
{
QPointF m = Mercator().ll2xy(c);
QPointF m = ll2m(c);
return QPointF(m.x() / _scale, m.y() / -_scale);
}
Coordinates EmptyMap::xy2ll(const QPointF &p) const
{
QPointF m(p.x() * _scale, -p.y() * _scale);
return Mercator().xy2ll(m);
return m2ll(QPointF(p.x() * _scale, -p.y() * _scale));
}

View File

@ -1,12 +1,16 @@
#include <cmath>
#include "common/coordinates.h"
#include "common/wgs84.h"
#include "mercator.h"
QPointF Mercator::ll2xy(const Coordinates &c) const
{
return QPointF(c.lon(), rad2deg(log(tan(M_PI/4.0 + deg2rad(c.lat())/2.0))));
return QPointF(deg2rad(c.lon()) * WGS84_RADIUS,
log(tan(M_PI/4.0 + deg2rad(c.lat())/2.0)) * WGS84_RADIUS);
}
Coordinates Mercator::xy2ll(const QPointF &p) const
{
return Coordinates(p.x(), rad2deg(2 * atan(exp(deg2rad(p.y()))) - M_PI/2));
return Coordinates(rad2deg(p.x() / WGS84_RADIUS),
rad2deg(2 * atan(exp(p.y() / WGS84_RADIUS)) - M_PI/2));
}

View File

@ -4,7 +4,6 @@
#include "common/coordinates.h"
#include "common/rectc.h"
#include "common/wgs84.h"
#include "mercator.h"
#include "downloader.h"
#include "config.h"
#include "onlinemap.h"
@ -12,6 +11,16 @@
#define TILE_SIZE 256
static QPointF ll2m(const Coordinates &c)
{
return QPointF(c.lon(), rad2deg(log(tan(M_PI/4.0 + deg2rad(c.lat())/2.0))));
}
static Coordinates m2ll(const QPointF &p)
{
return Coordinates(p.x(), rad2deg(2 * atan(exp(deg2rad(p.y()))) - M_PI/2));
}
static QPoint mercator2tile(const QPointF &m, int z)
{
QPoint tile;
@ -183,8 +192,7 @@ qreal OnlineMap::zoomFit(const QSize &size, const RectC &br)
if (!br.isValid())
_zoom = _zooms.max();
else {
QRectF tbr(Mercator().ll2xy(br.topLeft()),
Mercator().ll2xy(br.bottomRight()));
QRectF tbr(ll2m(br.topLeft()), ll2m(br.bottomRight()));
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
_zoom = limitZoom(scale2zoom(qMax(sc.x(), sc.y())));
@ -252,13 +260,12 @@ void OnlineMap::draw(QPainter *painter, const QRectF &rect)
QPointF OnlineMap::ll2xy(const Coordinates &c) const
{
qreal scale = zoom2scale(_zoom);
QPointF m = Mercator().ll2xy(c);
QPointF m = ll2m(c);
return QPointF(m.x() / scale, m.y() / -scale);
}
Coordinates OnlineMap::xy2ll(const QPointF &p) const
{
qreal scale = zoom2scale(_zoom);
QPointF m(p.x() * scale, -p.y() * scale);
return Mercator().xy2ll(m);
return m2ll(QPointF(p.x() * scale, -p.y() * scale));
}