2017-03-18 01:30:31 +01:00
|
|
|
#include <QPainter>
|
2017-11-26 18:54:03 +01:00
|
|
|
#include "common/coordinates.h"
|
|
|
|
#include "common/rectc.h"
|
|
|
|
#include "common/wgs84.h"
|
2017-03-18 01:30:31 +01:00
|
|
|
#include "downloader.h"
|
2018-09-20 07:59:47 +02:00
|
|
|
#include "osm.h"
|
2017-03-18 01:30:31 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "onlinemap.h"
|
|
|
|
|
|
|
|
|
2018-09-21 23:18:05 +02:00
|
|
|
#define TILE_SIZE 256
|
|
|
|
|
2017-09-09 12:33:43 +02:00
|
|
|
OnlineMap::OnlineMap(const QString &name, const QString &url,
|
2018-08-18 21:06:36 +02:00
|
|
|
const Range &zooms, const RectC &bounds, qreal tileRatio,
|
2018-09-22 13:32:54 +02:00
|
|
|
const Authorization &authorization, bool invertY, QObject *parent)
|
2018-08-18 21:06:36 +02:00
|
|
|
: Map(parent), _name(name), _zooms(zooms), _bounds(bounds),
|
2018-09-22 13:32:54 +02:00
|
|
|
_zoom(_zooms.max()), _deviceRatio(1.0), _tileRatio(tileRatio),
|
|
|
|
_invertY(invertY), _valid(false)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-02-25 02:31:01 +01:00
|
|
|
QString dir(TILES_DIR + "/" + _name);
|
|
|
|
|
2018-04-27 19:31:27 +02:00
|
|
|
_tileLoader = new TileLoader(this);
|
|
|
|
_tileLoader->setUrl(url);
|
|
|
|
_tileLoader->setDir(dir);
|
2018-04-27 21:13:10 +02:00
|
|
|
_tileLoader->setAuthorization(authorization);
|
2018-04-27 19:31:27 +02:00
|
|
|
connect(_tileLoader, SIGNAL(finished()), this, SIGNAL(loaded()));
|
2018-02-25 02:31:01 +01:00
|
|
|
|
|
|
|
if (!QDir().mkpath(dir)) {
|
|
|
|
_errorString = "Error creating tiles dir";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_valid = true;
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-07-13 09:51:41 +02:00
|
|
|
QRectF OnlineMap::bounds()
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-01-28 22:56:08 +01:00
|
|
|
return QRectF(ll2xy(_bounds.topLeft()), ll2xy(_bounds.bottomRight()));
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2017-09-09 12:33:43 +02:00
|
|
|
int OnlineMap::limitZoom(int zoom) const
|
|
|
|
{
|
|
|
|
if (zoom < _zooms.min())
|
|
|
|
return _zooms.min();
|
|
|
|
if (zoom > _zooms.max())
|
|
|
|
return _zooms.max();
|
|
|
|
|
|
|
|
return zoom;
|
|
|
|
}
|
|
|
|
|
2018-04-16 20:26:10 +02:00
|
|
|
int OnlineMap::zoomFit(const QSize &size, const RectC &rect)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-04-16 20:26:10 +02:00
|
|
|
if (!rect.isValid())
|
2017-09-09 12:33:43 +02:00
|
|
|
_zoom = _zooms.max();
|
2017-03-18 01:30:31 +01:00
|
|
|
else {
|
2018-09-20 07:59:47 +02:00
|
|
|
QRectF tbr(osm::ll2m(rect.topLeft()), osm::ll2m(rect.bottomRight()));
|
2017-03-18 01:30:31 +01:00
|
|
|
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
|
2018-09-20 07:59:47 +02:00
|
|
|
_zoom = limitZoom(osm::scale2zoom(qMax(sc.x(), -sc.y())
|
2018-09-21 23:18:05 +02:00
|
|
|
/ coordinatesRatio(), TILE_SIZE));
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2017-06-25 16:23:43 +02:00
|
|
|
return _zoom;
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-06-30 12:14:58 +02:00
|
|
|
qreal OnlineMap::resolution(const QRectF &rect)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-09-21 23:18:05 +02:00
|
|
|
qreal scale = osm::zoom2scale(_zoom, TILE_SIZE);
|
2017-06-25 16:23:43 +02:00
|
|
|
|
2017-11-27 23:50:55 +01:00
|
|
|
return (WGS84_RADIUS * 2.0 * M_PI * scale / 360.0
|
2018-02-28 22:19:46 +01:00
|
|
|
* cos(2.0 * atan(exp(deg2rad(-rect.center().y() * scale))) - M_PI/2));
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 22:19:46 +01:00
|
|
|
int OnlineMap::zoomIn()
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2017-09-09 12:33:43 +02:00
|
|
|
_zoom = qMin(_zoom + 1, _zooms.max());
|
2017-06-25 16:23:43 +02:00
|
|
|
return _zoom;
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 22:19:46 +01:00
|
|
|
int OnlineMap::zoomOut()
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2017-09-09 12:33:43 +02:00
|
|
|
_zoom = qMax(_zoom - 1, _zooms.min());
|
2017-06-25 16:23:43 +02:00
|
|
|
return _zoom;
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-08-18 21:06:36 +02:00
|
|
|
qreal OnlineMap::coordinatesRatio() const
|
|
|
|
{
|
|
|
|
return _deviceRatio > 1.0 ? _deviceRatio / _tileRatio : 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal OnlineMap::imageRatio() const
|
|
|
|
{
|
|
|
|
return _deviceRatio > 1.0 ? _deviceRatio : _tileRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal OnlineMap::tileSize() const
|
|
|
|
{
|
|
|
|
return (TILE_SIZE / coordinatesRatio());
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:26:10 +02:00
|
|
|
void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-09-21 23:18:05 +02:00
|
|
|
qreal scale = osm::zoom2scale(_zoom, TILE_SIZE);
|
2018-09-09 09:41:04 +02:00
|
|
|
QRectF b(bounds());
|
2017-03-18 01:30:31 +01:00
|
|
|
|
2018-09-20 07:59:47 +02:00
|
|
|
QPoint tile = osm::mercator2tile(QPointF(rect.topLeft().x() * scale,
|
2018-08-18 21:06:36 +02:00
|
|
|
-rect.topLeft().y() * scale) * coordinatesRatio(), _zoom);
|
|
|
|
QPointF tl(floor(rect.left() / tileSize())
|
|
|
|
* tileSize(), floor(rect.top() / tileSize()) * tileSize());
|
2017-03-18 01:30:31 +01:00
|
|
|
|
|
|
|
QList<Tile> tiles;
|
2018-09-09 09:41:04 +02:00
|
|
|
QSizeF s(qMin(rect.right() - tl.x(), b.width()),
|
|
|
|
qMin(rect.bottom() - tl.y(), b.height()));
|
2018-08-18 21:06:36 +02:00
|
|
|
for (int i = 0; i < ceil(s.width() / tileSize()); i++)
|
|
|
|
for (int j = 0; j < ceil(s.height() / tileSize()); j++)
|
2018-09-22 13:32:54 +02:00
|
|
|
tiles.append(Tile(QPoint(tile.x() + i,
|
|
|
|
_invertY ? (1<<_zoom) - (tile.y() + j) - 1 : tile.y() + j), _zoom));
|
2017-03-18 01:30:31 +01:00
|
|
|
|
2018-08-23 20:26:10 +02:00
|
|
|
if (flags & Map::Block)
|
2018-04-27 19:31:27 +02:00
|
|
|
_tileLoader->loadTilesSync(tiles);
|
2017-03-18 01:30:31 +01:00
|
|
|
else
|
2018-04-27 19:31:27 +02:00
|
|
|
_tileLoader->loadTilesAsync(tiles);
|
2017-03-18 01:30:31 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < tiles.count(); i++) {
|
|
|
|
Tile &t = tiles[i];
|
2018-09-09 09:41:04 +02:00
|
|
|
QPointF tp(qMax(tl.x(), b.left()) + (t.xy().x() - tile.x()) * tileSize(),
|
2018-09-22 13:32:54 +02:00
|
|
|
qMax(tl.y(), b.top()) + ((_invertY ? (1<<_zoom) - t.xy().y() - 1 :
|
|
|
|
t.xy().y()) - tile.y()) * tileSize());
|
2018-08-18 21:06:36 +02:00
|
|
|
if (!t.pixmap().isNull()) {
|
|
|
|
#ifdef ENABLE_HIDPI
|
|
|
|
t.pixmap().setDevicePixelRatio(imageRatio());
|
|
|
|
#endif // ENABLE_HIDPI
|
2018-02-20 23:37:19 +01:00
|
|
|
painter->drawPixmap(tp, t.pixmap());
|
2018-08-18 21:06:36 +02:00
|
|
|
}
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 09:51:41 +02:00
|
|
|
QPointF OnlineMap::ll2xy(const Coordinates &c)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-09-21 23:18:05 +02:00
|
|
|
qreal scale = osm::zoom2scale(_zoom, TILE_SIZE);
|
2018-09-20 07:59:47 +02:00
|
|
|
QPointF m = osm::ll2m(c);
|
2018-08-18 21:06:36 +02:00
|
|
|
return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio();
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-07-13 09:51:41 +02:00
|
|
|
Coordinates OnlineMap::xy2ll(const QPointF &p)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-09-21 23:18:05 +02:00
|
|
|
qreal scale = osm::zoom2scale(_zoom, TILE_SIZE);
|
2018-09-20 07:59:47 +02:00
|
|
|
return osm::m2ll(QPointF(p.x() * scale, -p.y() * scale)
|
|
|
|
* coordinatesRatio());
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|