mirror of
https://github.com/tumic0/GPXSee.git
synced 2024-11-27 21:24:47 +01:00
Added initial MBTiles support
This commit is contained in:
parent
c2e301f4e8
commit
db98f381b5
@ -1,4 +1,4 @@
|
||||
version: 5.18.{build}
|
||||
version: 6.0.{build}
|
||||
configuration: Release
|
||||
platform: Any CPU
|
||||
environment:
|
||||
|
13
gpxsee.pro
13
gpxsee.pro
@ -1,9 +1,10 @@
|
||||
TARGET = GPXSee
|
||||
VERSION = 5.18
|
||||
VERSION = 6.0
|
||||
|
||||
QT += core \
|
||||
gui \
|
||||
network
|
||||
network \
|
||||
sql
|
||||
greaterThan(QT_MAJOR_VERSION, 4) {
|
||||
QT += widgets
|
||||
QT += printsupport
|
||||
@ -142,7 +143,9 @@ HEADERS += src/config.h \
|
||||
src/data/slfparser.h \
|
||||
src/map/geotiffmap.h \
|
||||
src/map/image.h \
|
||||
src/common/greatcircle.h
|
||||
src/common/greatcircle.h \
|
||||
src/map/mbtilesmap.h \
|
||||
src/map/osm.h
|
||||
SOURCES += src/main.cpp \
|
||||
src/common/coordinates.cpp \
|
||||
src/common/rectc.cpp \
|
||||
@ -248,7 +251,9 @@ SOURCES += src/main.cpp \
|
||||
src/data/slfparser.cpp \
|
||||
src/map/geotiffmap.cpp \
|
||||
src/map/image.cpp \
|
||||
src/common/greatcircle.cpp
|
||||
src/common/greatcircle.cpp \
|
||||
src/map/mbtilesmap.cpp \
|
||||
src/map/osm.cpp
|
||||
|
||||
RESOURCES += gpxsee.qrc
|
||||
TRANSLATIONS = lang/gpxsee_en.ts \
|
||||
|
@ -30,7 +30,7 @@
|
||||
; The name of the installer
|
||||
Name "GPXSee"
|
||||
; Program version
|
||||
!define VERSION "5.18"
|
||||
!define VERSION "6.0"
|
||||
|
||||
; The file to write
|
||||
OutFile "GPXSee-${VERSION}.exe"
|
||||
|
@ -30,7 +30,7 @@
|
||||
; The name of the installer
|
||||
Name "GPXSee"
|
||||
; Program version
|
||||
!define VERSION "5.18"
|
||||
!define VERSION "6.0"
|
||||
|
||||
; The file to write
|
||||
OutFile "GPXSee-${VERSION}_x64.exe"
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include "jnxmap.h"
|
||||
#include "geotiffmap.h"
|
||||
#include "mapsource.h"
|
||||
#include "mbtilesmap.h"
|
||||
#include "maplist.h"
|
||||
|
||||
|
||||
@ -56,6 +57,8 @@ bool MapList::loadFile(const QString &path, bool *atlas, bool dir)
|
||||
return loadMap(new JNXMap(path, this), path, dir);
|
||||
else if (suffix == "tif" || suffix == "tiff")
|
||||
return loadMap(new GeoTIFFMap(path, this), path, dir);
|
||||
else if (suffix == "mbtiles")
|
||||
return loadMap(new MBTilesMap(path, this), path, dir);
|
||||
else
|
||||
return loadMap(new OziMap(path, this), path, dir);
|
||||
}
|
||||
@ -111,7 +114,9 @@ void MapList::clear()
|
||||
QString MapList::formats()
|
||||
{
|
||||
return
|
||||
tr("Supported files") + " (*.jnx *.map *.tar *.tba *.tif *.tiff *.xml);;"
|
||||
tr("Supported files")
|
||||
+ " (*.jnx *.map *.mbtiles *.tar *.tba *.tif *.tiff *.xml);;"
|
||||
+ tr("MBTiles maps") + " (*.mbtiles);;"
|
||||
+ tr("Garmin JNX maps") + " (*.jnx);;"
|
||||
+ tr("OziExplorer maps") + " (*.map);;"
|
||||
+ tr("TrekBuddy maps/atlases") + " (*.tar *.tba);;"
|
||||
|
230
src/map/mbtilesmap.cpp
Normal file
230
src/map/mbtilesmap.cpp
Normal file
@ -0,0 +1,230 @@
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlRecord>
|
||||
#include <QSqlField>
|
||||
#include <QFileInfo>
|
||||
#include <QPainter>
|
||||
#include <QPixmapCache>
|
||||
#include "common/rectc.h"
|
||||
#include "common/wgs84.h"
|
||||
#include "osm.h"
|
||||
#include "config.h"
|
||||
#include "mbtilesmap.h"
|
||||
|
||||
|
||||
#define META_TYPE(type) static_cast<QMetaType::Type>(type)
|
||||
|
||||
static double index2mercator(int index, int zoom)
|
||||
{
|
||||
return rad2deg(-M_PI + 2 * M_PI * ((double)index / (1<<zoom)));
|
||||
}
|
||||
|
||||
MBTilesMap::MBTilesMap(const QString &fileName, QObject *parent)
|
||||
: Map(parent), _fileName(fileName), _deviceRatio(1.0), _tileRatio(1.0),
|
||||
_valid(false)
|
||||
{
|
||||
_db = QSqlDatabase::addDatabase("QSQLITE", fileName);
|
||||
_db.setDatabaseName(fileName);
|
||||
|
||||
if (!_db.open()) {
|
||||
_errorString = fileName + ": Error opening database file";
|
||||
return;
|
||||
}
|
||||
|
||||
QSqlRecord r = _db.record("tiles");
|
||||
if (r.isEmpty()
|
||||
|| r.field(0).name() != "zoom_level"
|
||||
|| META_TYPE(r.field(0).type()) != QMetaType::Int
|
||||
|| r.field(1).name() != "tile_column"
|
||||
|| META_TYPE(r.field(1).type()) != QMetaType::Int
|
||||
|| r.field(2).name() != "tile_row"
|
||||
|| META_TYPE(r.field(2).type()) != QMetaType::Int
|
||||
|| r.field(3).name() != "tile_data"
|
||||
|| META_TYPE(r.field(3).type()) != QMetaType::QByteArray) {
|
||||
_errorString = "Invalid table format";
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
QSqlQuery query("SELECT min(zoom_level), max(zoom_level) FROM tiles",
|
||||
_db);
|
||||
if (!query.first()) {
|
||||
_errorString = "Empty tile set";
|
||||
return;
|
||||
}
|
||||
_zooms = Range(query.value(0).toInt(), query.value(1).toInt());
|
||||
}
|
||||
_zoom = _zooms.max();
|
||||
|
||||
{
|
||||
QString sql = QString("SELECT min(tile_column), min(tile_row), "
|
||||
"max(tile_column), max(tile_row) FROM tiles WHERE zoom_level = %1")
|
||||
.arg(_zooms.min());
|
||||
QSqlQuery query(sql, _db);
|
||||
query.first();
|
||||
|
||||
double minX = index2mercator(qMin((1<<_zooms.min()),
|
||||
qMax(0, query.value(0).toInt())), _zooms.min());
|
||||
double minY = index2mercator(qMin((1<<_zooms.min()),
|
||||
qMax(0, query.value(1).toInt())), _zooms.min());
|
||||
double maxX = index2mercator(qMin((1<<_zooms.min()),
|
||||
qMax(0, query.value(2).toInt())) + 1, _zooms.min());
|
||||
double maxY = index2mercator(qMin((1<<_zooms.min()),
|
||||
qMax(0, query.value(3).toInt())) + 1, _zooms.min());
|
||||
Coordinates tl(osm::m2ll(QPointF(minX, maxY)));
|
||||
Coordinates br(osm::m2ll(QPointF(maxX, minY)));
|
||||
// Workaround of broken zoom level 0 and 1 due to numerical instability
|
||||
tl.rlat() = qMin(tl.lat(), 85.0511);
|
||||
br.rlat() = qMax(br.lat(), -85.0511);
|
||||
_bounds = RectC(tl, br);
|
||||
}
|
||||
|
||||
_db.close();
|
||||
|
||||
_valid = true;
|
||||
}
|
||||
|
||||
QString MBTilesMap::name() const
|
||||
{
|
||||
return QFileInfo(_fileName).fileName();;
|
||||
}
|
||||
|
||||
void MBTilesMap::load()
|
||||
{
|
||||
_db.open();
|
||||
}
|
||||
|
||||
void MBTilesMap::unload()
|
||||
{
|
||||
_db.close();
|
||||
}
|
||||
|
||||
QRectF MBTilesMap::bounds()
|
||||
{
|
||||
return QRectF(ll2xy(_bounds.topLeft()), ll2xy(_bounds.bottomRight()));
|
||||
}
|
||||
|
||||
int MBTilesMap::limitZoom(int zoom) const
|
||||
{
|
||||
if (zoom < _zooms.min())
|
||||
return _zooms.min();
|
||||
if (zoom > _zooms.max())
|
||||
return _zooms.max();
|
||||
|
||||
return zoom;
|
||||
}
|
||||
|
||||
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()));
|
||||
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
|
||||
_zoom = limitZoom(osm::scale2zoom(qMax(sc.x(), -sc.y())
|
||||
/ coordinatesRatio()));
|
||||
}
|
||||
|
||||
return _zoom;
|
||||
}
|
||||
|
||||
qreal MBTilesMap::resolution(const QRectF &rect)
|
||||
{
|
||||
qreal scale = osm::zoom2scale(_zoom);
|
||||
|
||||
return (WGS84_RADIUS * 2.0 * M_PI * scale / 360.0
|
||||
* cos(2.0 * atan(exp(deg2rad(-rect.center().y() * scale))) - M_PI/2));
|
||||
}
|
||||
|
||||
int MBTilesMap::zoomIn()
|
||||
{
|
||||
_zoom = qMin(_zoom + 1, _zooms.max());
|
||||
return _zoom;
|
||||
}
|
||||
|
||||
int MBTilesMap::zoomOut()
|
||||
{
|
||||
_zoom = qMax(_zoom - 1, _zooms.min());
|
||||
return _zoom;
|
||||
}
|
||||
|
||||
qreal MBTilesMap::coordinatesRatio() const
|
||||
{
|
||||
return _deviceRatio > 1.0 ? _deviceRatio / _tileRatio : 1.0;
|
||||
}
|
||||
|
||||
qreal MBTilesMap::imageRatio() const
|
||||
{
|
||||
return _deviceRatio > 1.0 ? _deviceRatio : _tileRatio;
|
||||
}
|
||||
|
||||
qreal MBTilesMap::tileSize() const
|
||||
{
|
||||
return (TILE_SIZE / coordinatesRatio());
|
||||
}
|
||||
|
||||
QByteArray MBTilesMap::tileData(int zoom, const QPoint &tile) const
|
||||
{
|
||||
QSqlQuery query(_db);
|
||||
query.prepare("SELECT tile_data FROM tiles "
|
||||
"WHERE zoom_level=:zoom AND tile_column=:x AND tile_row=:y");
|
||||
query.bindValue(":zoom", zoom);
|
||||
query.bindValue(":x", tile.x());
|
||||
query.bindValue(":y", (1<<zoom) - tile.y() - 1);
|
||||
query.exec();
|
||||
|
||||
if (query.first())
|
||||
return query.value(0).toByteArray();
|
||||
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
|
||||
{
|
||||
Q_UNUSED(flags);
|
||||
qreal scale = osm::zoom2scale(_zoom);
|
||||
QRectF b(bounds());
|
||||
|
||||
|
||||
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());
|
||||
|
||||
QSizeF s(qMin(rect.right() - tl.x(), b.width()),
|
||||
qMin(rect.bottom() - tl.y(), b.height()));
|
||||
for (int i = 0; i < ceil(s.width() / tileSize()); i++) {
|
||||
for (int j = 0; j < ceil(s.height() / tileSize()); j++) {
|
||||
QPixmap pm;
|
||||
QPoint t(tile.x() + i, tile.y() + j);
|
||||
QString key = _fileName + "-" + QString::number(_zoom) + "_"
|
||||
+ QString::number(t.x()) + "_" + QString::number(t.y());
|
||||
|
||||
if (!QPixmapCache::find(key, &pm))
|
||||
if (pm.loadFromData(tileData(_zoom, t)))
|
||||
QPixmapCache::insert(key, pm);
|
||||
|
||||
QPointF tp(qMax(tl.x(), b.left()) + (t.x() - tile.x()) * tileSize(),
|
||||
qMax(tl.y(), b.top()) + (t.y() - tile.y()) * tileSize());
|
||||
if (!pm.isNull()) {
|
||||
#ifdef ENABLE_HIDPI
|
||||
pm.setDevicePixelRatio(imageRatio());
|
||||
#endif // ENABLE_HIDPI
|
||||
painter->drawPixmap(tp, pm);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QPointF MBTilesMap::ll2xy(const Coordinates &c)
|
||||
{
|
||||
qreal scale = osm::zoom2scale(_zoom);
|
||||
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);
|
||||
return osm::m2ll(QPointF(p.x() * scale, -p.y() * scale)
|
||||
* coordinatesRatio());
|
||||
}
|
56
src/map/mbtilesmap.h
Normal file
56
src/map/mbtilesmap.h
Normal file
@ -0,0 +1,56 @@
|
||||
#ifndef MBTILESMAP_H
|
||||
#define MBTILESMAP_H
|
||||
|
||||
#include <QSqlDatabase>
|
||||
#include <QByteArray>
|
||||
#include "common/range.h"
|
||||
#include "map.h"
|
||||
|
||||
class MBTilesMap : public Map
|
||||
{
|
||||
public:
|
||||
MBTilesMap(const QString &fileName, QObject *parent = 0);
|
||||
|
||||
QString name() const;
|
||||
|
||||
QRectF bounds();
|
||||
qreal resolution(const QRectF &rect);
|
||||
|
||||
int zoom() const {return _zoom;}
|
||||
void setZoom(int zoom) {_zoom = zoom;}
|
||||
int zoomFit(const QSize &size, const RectC &rect);
|
||||
int zoomIn();
|
||||
int zoomOut();
|
||||
|
||||
QPointF ll2xy(const Coordinates &c);
|
||||
Coordinates xy2ll(const QPointF &p);
|
||||
|
||||
void draw(QPainter *painter, const QRectF &rect, Flags flags);
|
||||
|
||||
void load();
|
||||
void unload();
|
||||
void setDevicePixelRatio(qreal ratio) {_deviceRatio = ratio;}
|
||||
|
||||
bool isValid() const {return _valid;}
|
||||
QString errorString() const {return _errorString;}
|
||||
|
||||
private:
|
||||
int limitZoom(int zoom) const;
|
||||
qreal tileSize() const;
|
||||
qreal coordinatesRatio() const;
|
||||
qreal imageRatio() const;
|
||||
QByteArray tileData(int zoom, const QPoint &tile) const;
|
||||
|
||||
QSqlDatabase _db;
|
||||
|
||||
QString _fileName;
|
||||
RectC _bounds;
|
||||
Range _zooms;
|
||||
int _zoom;
|
||||
qreal _deviceRatio, _tileRatio;
|
||||
|
||||
bool _valid;
|
||||
QString _errorString;
|
||||
};
|
||||
|
||||
#endif // MBTILESMAP_H
|
@ -3,44 +3,11 @@
|
||||
#include "common/rectc.h"
|
||||
#include "common/wgs84.h"
|
||||
#include "downloader.h"
|
||||
#include "osm.h"
|
||||
#include "config.h"
|
||||
#include "onlinemap.h"
|
||||
|
||||
|
||||
#define TILE_SIZE 256
|
||||
#define EPSILON 1e-6
|
||||
|
||||
static QPointF ll2m(const Coordinates &c)
|
||||
{
|
||||
return QPointF(c.lon(), rad2deg(log(tan(M_PI_4 + deg2rad(c.lat())/2.0))));
|
||||
}
|
||||
|
||||
static Coordinates m2ll(const QPointF &p)
|
||||
{
|
||||
return Coordinates(p.x(), rad2deg(2.0 * atan(exp(deg2rad(p.y()))) - M_PI_2));
|
||||
}
|
||||
|
||||
static QPoint mercator2tile(const QPointF &m, int z)
|
||||
{
|
||||
QPoint tile;
|
||||
|
||||
tile.setX((int)(floor((m.x() + 180.0) / 360.0 * (1<<z))));
|
||||
tile.setY((int)(floor((1.0 - (m.y() / 180.0)) / 2.0 * (1<<z))));
|
||||
|
||||
return tile;
|
||||
}
|
||||
|
||||
static qreal zoom2scale(int zoom)
|
||||
{
|
||||
return (360.0/(qreal)((1<<zoom) * TILE_SIZE));
|
||||
}
|
||||
|
||||
static int scale2zoom(qreal scale)
|
||||
{
|
||||
return (int)(log2(360.0/(scale * (qreal)TILE_SIZE)) + EPSILON);
|
||||
}
|
||||
|
||||
|
||||
OnlineMap::OnlineMap(const QString &name, const QString &url,
|
||||
const Range &zooms, const RectC &bounds, qreal tileRatio,
|
||||
const Authorization &authorization, QObject *parent)
|
||||
@ -83,9 +50,10 @@ int OnlineMap::zoomFit(const QSize &size, const RectC &rect)
|
||||
if (!rect.isValid())
|
||||
_zoom = _zooms.max();
|
||||
else {
|
||||
QRectF tbr(ll2m(rect.topLeft()), 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(scale2zoom(qMax(sc.x(), -sc.y()) / coordinatesRatio()));
|
||||
_zoom = limitZoom(osm::scale2zoom(qMax(sc.x(), -sc.y())
|
||||
/ coordinatesRatio()));
|
||||
}
|
||||
|
||||
return _zoom;
|
||||
@ -93,7 +61,7 @@ int OnlineMap::zoomFit(const QSize &size, const RectC &rect)
|
||||
|
||||
qreal OnlineMap::resolution(const QRectF &rect)
|
||||
{
|
||||
qreal scale = zoom2scale(_zoom);
|
||||
qreal scale = osm::zoom2scale(_zoom);
|
||||
|
||||
return (WGS84_RADIUS * 2.0 * M_PI * scale / 360.0
|
||||
* cos(2.0 * atan(exp(deg2rad(-rect.center().y() * scale))) - M_PI/2));
|
||||
@ -128,10 +96,10 @@ qreal OnlineMap::tileSize() const
|
||||
|
||||
void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
|
||||
{
|
||||
qreal scale = zoom2scale(_zoom);
|
||||
qreal scale = osm::zoom2scale(_zoom);
|
||||
QRectF b(bounds());
|
||||
|
||||
QPoint tile = 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());
|
||||
@ -163,13 +131,14 @@ void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
|
||||
|
||||
QPointF OnlineMap::ll2xy(const Coordinates &c)
|
||||
{
|
||||
qreal scale = zoom2scale(_zoom);
|
||||
QPointF m = ll2m(c);
|
||||
qreal scale = osm::zoom2scale(_zoom);
|
||||
QPointF m = osm::ll2m(c);
|
||||
return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio();
|
||||
}
|
||||
|
||||
Coordinates OnlineMap::xy2ll(const QPointF &p)
|
||||
{
|
||||
qreal scale = zoom2scale(_zoom);
|
||||
return m2ll(QPointF(p.x() * scale, -p.y() * scale) * coordinatesRatio());
|
||||
qreal scale = osm::zoom2scale(_zoom);
|
||||
return osm::m2ll(QPointF(p.x() * scale, -p.y() * scale)
|
||||
* coordinatesRatio());
|
||||
}
|
||||
|
29
src/map/osm.cpp
Normal file
29
src/map/osm.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include "osm.h"
|
||||
|
||||
#define EPSILON 1e-6
|
||||
|
||||
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)
|
||||
{
|
||||
return Coordinates(p.x(), rad2deg(2.0 * atan(exp(deg2rad(p.y()))) - M_PI_2));
|
||||
}
|
||||
|
||||
QPoint osm::mercator2tile(const QPointF &m, int z)
|
||||
{
|
||||
return QPoint((int)(floor((m.x() + 180.0) / 360.0 * (1<<z))),
|
||||
(int)(floor((1.0 - (m.y() / 180.0)) / 2.0 * (1<<z))));
|
||||
}
|
||||
|
||||
qreal osm::zoom2scale(int zoom)
|
||||
{
|
||||
return (360.0/(qreal)((1<<zoom) * TILE_SIZE));
|
||||
}
|
||||
|
||||
int osm::scale2zoom(qreal scale)
|
||||
{
|
||||
return (int)(log2(360.0/(scale * (qreal)TILE_SIZE)) + EPSILON);
|
||||
}
|
18
src/map/osm.h
Normal file
18
src/map/osm.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef OSM_H
|
||||
#define OSM_H
|
||||
|
||||
#include <QPointF>
|
||||
#include <common/coordinates.h>
|
||||
|
||||
#define TILE_SIZE 256
|
||||
|
||||
namespace osm
|
||||
{
|
||||
QPointF ll2m(const Coordinates &c);
|
||||
Coordinates m2ll(const QPointF &p);
|
||||
QPoint mercator2tile(const QPointF &m, int z);
|
||||
qreal zoom2scale(int zoom);
|
||||
int scale2zoom(qreal scale);
|
||||
}
|
||||
|
||||
#endif // OSM_H
|
Loading…
Reference in New Issue
Block a user