2018-10-07 13:07:04 +02:00
|
|
|
#include <QtCore>
|
2018-09-24 22:49:10 +02:00
|
|
|
#include "common/wgs84.h"
|
2018-09-20 07:59:47 +02:00
|
|
|
#include "osm.h"
|
|
|
|
|
2018-09-24 22:49:10 +02:00
|
|
|
|
2018-09-20 07:59:47 +02:00
|
|
|
#define EPSILON 1e-6
|
|
|
|
|
2021-04-10 15:27:40 +02:00
|
|
|
static double index2mercator(int index, int zoom)
|
|
|
|
{
|
|
|
|
return rad2deg(-M_PI + 2 * M_PI * ((double)index / (1<<zoom)));
|
|
|
|
}
|
|
|
|
|
2018-09-25 21:07:44 +02:00
|
|
|
QPointF OSM::ll2m(const Coordinates &c)
|
2018-09-20 07:59:47 +02:00
|
|
|
{
|
|
|
|
return QPointF(c.lon(), rad2deg(log(tan(M_PI_4 + deg2rad(c.lat())/2.0))));
|
|
|
|
}
|
|
|
|
|
2018-09-25 21:07:44 +02:00
|
|
|
Coordinates OSM::m2ll(const QPointF &p)
|
2018-09-20 07:59:47 +02:00
|
|
|
{
|
|
|
|
return Coordinates(p.x(), rad2deg(2.0 * atan(exp(deg2rad(p.y()))) - M_PI_2));
|
|
|
|
}
|
|
|
|
|
2018-09-25 21:07:44 +02:00
|
|
|
QPoint OSM::mercator2tile(const QPointF &m, int zoom)
|
2018-09-20 07:59:47 +02:00
|
|
|
{
|
2021-05-27 22:07:09 +02:00
|
|
|
return QPoint(qMin(qFloor((m.x() + 180.0) / 360.0 * (1<<zoom)), (1<<zoom) - 1),
|
|
|
|
qMin(qFloor((1.0 - (m.y() / 180.0)) / 2.0 * (1<<zoom)), (1<<zoom) - 1));
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 15:27:40 +02:00
|
|
|
QPointF OSM::tile2mercator(const QPoint &p, int zoom)
|
2021-02-05 21:58:34 +01:00
|
|
|
{
|
2021-04-10 15:27:40 +02:00
|
|
|
return QPointF(index2mercator(p.x(), zoom), index2mercator(p.y(), zoom));
|
2021-02-05 21:58:34 +01:00
|
|
|
}
|
|
|
|
|
2018-09-25 21:07:44 +02:00
|
|
|
qreal OSM::zoom2scale(int zoom, int tileSize)
|
2018-09-20 07:59:47 +02:00
|
|
|
{
|
2018-09-21 23:18:05 +02:00
|
|
|
return (360.0/(qreal)((1<<zoom) * tileSize));
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 21:07:44 +02:00
|
|
|
int OSM::scale2zoom(qreal scale, int tileSize)
|
2018-09-20 07:59:47 +02:00
|
|
|
{
|
2018-09-21 23:18:05 +02:00
|
|
|
return (int)(log2(360.0/(scale * (qreal)tileSize)) + EPSILON);
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
2018-09-24 22:49:10 +02:00
|
|
|
|
2018-09-25 21:07:44 +02:00
|
|
|
qreal OSM::resolution(const QPointF &p, int zoom, int tileSize)
|
2018-09-24 22:49:10 +02:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|