1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-07 07:13:21 +02:00
GPXSee/src/map/imgmap.cpp

227 lines
5.4 KiB
C++
Raw Normal View History

2019-05-10 18:56:19 +02:00
#include <QFile>
#include <QPainter>
#include <QPixmapCache>
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
#include <QtCore>
#else // QT_VERSION < 5
#include <QtConcurrent>
#endif // QT_VERSION < 5
#include "common/rectc.h"
2020-01-19 17:05:26 +01:00
#include "common/range.h"
2020-04-26 01:17:54 +02:00
#include "common/wgs84.h"
2020-02-09 23:24:48 +01:00
#include "IMG/img.h"
#include "IMG/gmap.h"
2020-04-26 01:17:54 +02:00
#include "IMG/rastertile.h"
#include "osm.h"
2019-05-10 18:56:19 +02:00
#include "pcs.h"
#include "rectd.h"
#include "imgmap.h"
2019-10-05 12:41:42 +02:00
#define TILE_SIZE 384
#define TEXT_EXTENT 160
2019-05-10 18:56:19 +02:00
IMGMap::IMGMap(const QString &fileName, QObject *parent)
2020-02-09 23:24:48 +01:00
: Map(parent), _projection(PCS::pcs(3857)), _valid(false)
2019-05-10 18:56:19 +02:00
{
2020-02-09 23:24:48 +01:00
if (GMAP::isGMAP(fileName))
_data = new GMAP(fileName);
else
_data = new IMG(fileName);
if (!_data->isValid()) {
_errorString = _data->errorString();
2019-05-10 18:56:19 +02:00
return;
}
_dataBounds = _data->bounds() & OSM::BOUNDS;
_zoom = _data->zooms().min();
2019-05-10 18:56:19 +02:00
updateTransform();
_valid = true;
}
2019-07-04 18:18:03 +02:00
void IMGMap::load()
{
2020-02-09 23:24:48 +01:00
_data->load();
2019-07-04 18:18:03 +02:00
}
void IMGMap::unload()
{
2020-02-09 23:24:48 +01:00
_data->clear();
2019-07-04 18:18:03 +02:00
}
2019-05-10 18:56:19 +02:00
int IMGMap::zoomFit(const QSize &size, const RectC &rect)
{
if (rect.isValid()) {
2019-05-29 18:27:11 +02:00
RectD pr(rect, _projection, 10);
2019-05-10 18:56:19 +02:00
_zoom = _data->zooms().min();
for (int i = _data->zooms().min() + 1; i <= _data->zooms().max(); i++) {
2019-05-29 18:27:11 +02:00
Transform t(transform(i));
QRectF r(t.proj2img(pr.topLeft()), t.proj2img(pr.bottomRight()));
if (size.width() < r.width() || size.height() < r.height())
2019-05-10 18:56:19 +02:00
break;
_zoom = i;
}
} else
_zoom = _data->zooms().max();
2019-05-10 18:56:19 +02:00
updateTransform();
return _zoom;
}
int IMGMap::zoomIn()
{
_zoom = qMin(_zoom + 1, _data->zooms().max());
2019-05-10 18:56:19 +02:00
updateTransform();
return _zoom;
}
int IMGMap::zoomOut()
{
_zoom = qMax(_zoom - 1, _data->zooms().min());
2019-05-10 18:56:19 +02:00
updateTransform();
return _zoom;
}
void IMGMap::setZoom(int zoom)
{
_zoom = zoom;
updateTransform();
}
2019-05-29 18:27:11 +02:00
Transform IMGMap::transform(int zoom) const
2019-05-10 18:56:19 +02:00
{
double scale = _projection.isGeographic()
? 360.0 / (1<<zoom) : (2.0 * M_PI * WGS84_RADIUS) / (1<<zoom);
PointD topLeft(_projection.ll2xy(_dataBounds.topLeft()));
2019-05-29 18:27:11 +02:00
return Transform(ReferencePoint(PointD(0, 0), topLeft),
2019-05-10 18:56:19 +02:00
PointD(scale, scale));
}
2019-05-29 18:27:11 +02:00
void IMGMap::updateTransform()
{
_transform = transform(_zoom);
2020-03-07 19:24:39 +01:00
RectD prect(_dataBounds, _projection);
2020-03-07 19:24:39 +01:00
_bounds = QRectF(_transform.proj2img(prect.topLeft()),
_transform.proj2img(prect.bottomRight()));
2019-05-29 18:27:11 +02:00
}
2019-05-10 18:56:19 +02:00
QPointF IMGMap::ll2xy(const Coordinates &c)
{
return _transform.proj2img(_projection.ll2xy(c));
}
Coordinates IMGMap::xy2ll(const QPointF &p)
{
return _projection.xy2ll(_transform.img2proj(p));
}
2020-04-26 01:17:54 +02:00
void IMGMap::ll2xy(QList<MapData::Poly> &polys)
2019-05-10 18:56:19 +02:00
{
2020-04-26 01:17:54 +02:00
for (int i = 0; i < polys.size(); i++) {
MapData::Poly &poly = polys[i];
for (int j = 0; j < poly.points.size(); j++) {
QPointF &p = poly.points[j];
p = ll2xy(Coordinates(p.x(), p.y()));
}
}
2019-05-10 18:56:19 +02:00
}
2020-04-26 01:17:54 +02:00
void IMGMap::ll2xy(QList<MapData::Point> &points)
2019-05-10 18:56:19 +02:00
{
for (int i = 0; i < points.size(); i++) {
2020-04-26 01:17:54 +02:00
QPointF p(ll2xy(points.at(i).coordinates));
points[i].coordinates = Coordinates(p.x(), p.y());
2019-05-10 18:56:19 +02:00
}
}
static void render(RasterTile &tile)
{
2019-07-13 08:36:04 +02:00
tile.render();
2019-05-10 18:56:19 +02:00
}
void IMGMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
Q_UNUSED(flags);
QPointF tl(floor(rect.left() / TILE_SIZE)
* TILE_SIZE, floor(rect.top() / TILE_SIZE) * TILE_SIZE);
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
int width = ceil(s.width() / TILE_SIZE);
int height = ceil(s.height() / TILE_SIZE);
QList<RasterTile> tiles;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
QPixmap pm;
QPoint ttl(tl.x() + i * TILE_SIZE, tl.y() + j * TILE_SIZE);
2020-02-09 23:24:48 +01:00
QString key = _data->fileName() + "-" + QString::number(_zoom) + "_"
2019-05-10 18:56:19 +02:00
+ QString::number(ttl.x()) + "_" + QString::number(ttl.y());
if (QPixmapCache::find(key, pm))
painter->drawPixmap(ttl, pm);
else {
2020-04-26 01:17:54 +02:00
QList<MapData::Poly> polygons, lines;
QList<MapData::Point> points;
2019-05-10 18:56:19 +02:00
2020-02-16 08:43:37 +01:00
QRectF polyRect(ttl, QPointF(ttl.x() + TILE_SIZE,
ttl.y() + TILE_SIZE));
2020-02-17 19:23:36 +01:00
polyRect &= bounds().adjusted(0.5, 0.5, -0.5, -0.5);
2020-02-16 08:43:37 +01:00
RectD polyRectD(_transform.img2proj(polyRect.topLeft()),
_transform.img2proj(polyRect.bottomRight()));
_data->polys(polyRectD.toRectC(_projection, 4), _zoom,
2020-04-26 01:17:54 +02:00
&polygons, &lines);
ll2xy(polygons); ll2xy(lines);
2020-02-16 08:43:37 +01:00
QRectF pointRect(QPointF(ttl.x() - TEXT_EXTENT,
ttl.y() - TEXT_EXTENT), QPointF(ttl.x() + TILE_SIZE
+ TEXT_EXTENT, ttl.y() + TILE_SIZE + TEXT_EXTENT));
2020-02-17 19:23:36 +01:00
pointRect &= bounds().adjusted(0.5, 0.5, -0.5, -0.5);
2020-02-16 08:43:37 +01:00
RectD pointRectD(_transform.img2proj(pointRect.topLeft()),
_transform.img2proj(pointRect.bottomRight()));
_data->points(pointRectD.toRectC(_projection, 4), _zoom,
2020-04-26 01:17:54 +02:00
&points);
ll2xy(points);
tiles.append(RasterTile(_data->style(), _zoom,
QRect(ttl, QSize(TILE_SIZE, TILE_SIZE)), key, polygons, lines,
points));
2019-05-10 18:56:19 +02:00
}
}
}
QFuture<void> future = QtConcurrent::map(tiles, render);
future.waitForFinished();
for (int i = 0; i < tiles.size(); i++) {
RasterTile &mt = tiles[i];
QPixmap pm(QPixmap::fromImage(mt.img()));
if (pm.isNull())
continue;
QPixmapCache::insert(mt.key(), pm);
painter->drawPixmap(mt.xy(), pm);
}
}
void IMGMap::setProjection(const Projection &projection)
{
if (projection == _projection)
return;
_projection = projection;
// Limit the bounds for some well known Mercator projections
// (GARMIN world maps have N/S bounds up to 90/-90!)
_dataBounds = (_projection == PCS::pcs(3857)
|| _projection == PCS::pcs(3395))
? _data->bounds() & OSM::BOUNDS : _data->bounds();
updateTransform();
QPixmapCache::clear();
}