#include #include #include #include #include #include #include "common/rectc.h" #include "common/wgs84.h" #include "osm.h" #include "config.h" #include "mbtilesmap.h" #define META_TYPE(type) static_cast(type) static double index2mercator(int index, int zoom) { return rad2deg(-M_PI + 2 * M_PI * ((double)index / (1< _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<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()); }