1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 05:34:47 +01:00

Added support for IMG maps overlays

This commit is contained in:
Martin Tůma 2020-04-26 15:46:42 +02:00
parent ddf865834a
commit a958544667
2 changed files with 80 additions and 51 deletions

View File

@ -21,21 +21,45 @@
#define TILE_SIZE 384 #define TILE_SIZE 384
#define TEXT_EXTENT 160 #define TEXT_EXTENT 160
static QList<MapData*> overlays(const QString &fileName)
{
QList<MapData*> list;
for (int i = 1; ; i++) {
QString ol(fileName + "." + QString::number(i));
if (QFileInfo(ol).isFile()) {
MapData *data = new IMG(ol);
if (data->isValid())
list.append(data);
else {
qWarning("%s: %s", qPrintable(data->fileName()),
qPrintable(data->errorString()));
delete data;
}
} else
break;
}
return list;
}
IMGMap::IMGMap(const QString &fileName, QObject *parent) IMGMap::IMGMap(const QString &fileName, QObject *parent)
: Map(parent), _projection(PCS::pcs(3857)), _valid(false) : Map(parent), _projection(PCS::pcs(3857)), _valid(false)
{ {
if (GMAP::isGMAP(fileName)) if (GMAP::isGMAP(fileName))
_data = new GMAP(fileName); _data.append(new GMAP(fileName));
else else {
_data = new IMG(fileName); _data.append(new IMG(fileName));
_data.append(overlays(fileName));
}
if (!_data->isValid()) { if (!_data.first()->isValid()) {
_errorString = _data->errorString(); _errorString = _data.first()->errorString();
return; return;
} }
_dataBounds = _data->bounds() & OSM::BOUNDS; _dataBounds = _data.first()->bounds() & OSM::BOUNDS;
_zoom = _data->zooms().min(); _zoom = _data.first()->zooms().min();
updateTransform(); updateTransform();
_valid = true; _valid = true;
@ -43,21 +67,25 @@ IMGMap::IMGMap(const QString &fileName, QObject *parent)
void IMGMap::load() void IMGMap::load()
{ {
_data->load(); for (int i = 0; i < _data.size(); i++)
_data.at(i)->load();
} }
void IMGMap::unload() void IMGMap::unload()
{ {
_data->clear(); for (int i = 0; i < _data.size(); i++)
_data.at(i)->clear();
} }
int IMGMap::zoomFit(const QSize &size, const RectC &rect) int IMGMap::zoomFit(const QSize &size, const RectC &rect)
{ {
const Range &zooms = _data.first()->zooms();
if (rect.isValid()) { if (rect.isValid()) {
RectD pr(rect, _projection, 10); RectD pr(rect, _projection, 10);
_zoom = _data->zooms().min(); _zoom = zooms.min();
for (int i = _data->zooms().min() + 1; i <= _data->zooms().max(); i++) { for (int i = zooms.min() + 1; i <= zooms.max(); i++) {
Transform t(transform(i)); Transform t(transform(i));
QRectF r(t.proj2img(pr.topLeft()), t.proj2img(pr.bottomRight())); QRectF r(t.proj2img(pr.topLeft()), t.proj2img(pr.bottomRight()));
if (size.width() < r.width() || size.height() < r.height()) if (size.width() < r.width() || size.height() < r.height())
@ -65,7 +93,7 @@ int IMGMap::zoomFit(const QSize &size, const RectC &rect)
_zoom = i; _zoom = i;
} }
} else } else
_zoom = _data->zooms().max(); _zoom = zooms.max();
updateTransform(); updateTransform();
@ -74,14 +102,14 @@ int IMGMap::zoomFit(const QSize &size, const RectC &rect)
int IMGMap::zoomIn() int IMGMap::zoomIn()
{ {
_zoom = qMin(_zoom + 1, _data->zooms().max()); _zoom = qMin(_zoom + 1, _data.first()->zooms().max());
updateTransform(); updateTransform();
return _zoom; return _zoom;
} }
int IMGMap::zoomOut() int IMGMap::zoomOut()
{ {
_zoom = qMax(_zoom - 1, _data->zooms().min()); _zoom = qMax(_zoom - 1, _data.first()->zooms().min());
updateTransform(); updateTransform();
return _zoom; return _zoom;
} }
@ -156,12 +184,13 @@ void IMGMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
QList<RasterTile> tiles; QList<RasterTile> tiles;
for (int n = 0; n < _data.size(); n++) {
for (int i = 0; i < width; i++) { for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) { for (int j = 0; j < height; j++) {
QPixmap pm; QPixmap pm;
QPoint ttl(tl.x() + i * TILE_SIZE, tl.y() + j * TILE_SIZE); QPoint ttl(tl.x() + i * TILE_SIZE, tl.y() + j * TILE_SIZE);
QString key = _data->fileName() + "-" + QString::number(_zoom) + "_" QString key = _data.at(n)->fileName() + "-" + QString::number(_zoom)
+ QString::number(ttl.x()) + "_" + QString::number(ttl.y()); + "_" + QString::number(ttl.x()) + "_" + QString::number(ttl.y());
if (QPixmapCache::find(key, pm)) if (QPixmapCache::find(key, pm))
painter->drawPixmap(ttl, pm); painter->drawPixmap(ttl, pm);
else { else {
@ -173,7 +202,7 @@ void IMGMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
polyRect &= bounds().adjusted(0.5, 0.5, -0.5, -0.5); polyRect &= bounds().adjusted(0.5, 0.5, -0.5, -0.5);
RectD polyRectD(_transform.img2proj(polyRect.topLeft()), RectD polyRectD(_transform.img2proj(polyRect.topLeft()),
_transform.img2proj(polyRect.bottomRight())); _transform.img2proj(polyRect.bottomRight()));
_data->polys(polyRectD.toRectC(_projection, 4), _zoom, _data.at(n)->polys(polyRectD.toRectC(_projection, 4), _zoom,
&polygons, &lines); &polygons, &lines);
ll2xy(polygons); ll2xy(lines); ll2xy(polygons); ll2xy(lines);
@ -183,13 +212,14 @@ void IMGMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
pointRect &= bounds().adjusted(0.5, 0.5, -0.5, -0.5); pointRect &= bounds().adjusted(0.5, 0.5, -0.5, -0.5);
RectD pointRectD(_transform.img2proj(pointRect.topLeft()), RectD pointRectD(_transform.img2proj(pointRect.topLeft()),
_transform.img2proj(pointRect.bottomRight())); _transform.img2proj(pointRect.bottomRight()));
_data->points(pointRectD.toRectC(_projection, 4), _zoom, _data.at(n)->points(pointRectD.toRectC(_projection, 4),
&points); _zoom, &points);
ll2xy(points); ll2xy(points);
tiles.append(RasterTile(_data->style(), _zoom, tiles.append(RasterTile(_data.at(n)->style(), _zoom,
QRect(ttl, QSize(TILE_SIZE, TILE_SIZE)), key, polygons, lines, QRect(ttl, QSize(TILE_SIZE, TILE_SIZE)), key, polygons,
points)); lines, points));
}
} }
} }
} }
@ -217,9 +247,8 @@ void IMGMap::setProjection(const Projection &projection)
_projection = projection; _projection = projection;
// Limit the bounds for some well known Mercator projections // Limit the bounds for some well known Mercator projections
// (GARMIN world maps have N/S bounds up to 90/-90!) // (GARMIN world maps have N/S bounds up to 90/-90!)
_dataBounds = (_projection == PCS::pcs(3857) _dataBounds = (_projection == PCS::pcs(3857) || _projection == PCS::pcs(3395))
|| _projection == PCS::pcs(3395)) ? _data.first()->bounds() & OSM::BOUNDS : _data.first()->bounds();
? _data->bounds() & OSM::BOUNDS : _data->bounds();
updateTransform(); updateTransform();
QPixmapCache::clear(); QPixmapCache::clear();

View File

@ -13,9 +13,9 @@ class IMGMap : public Map
public: public:
IMGMap(const QString &fileName, QObject *parent = 0); IMGMap(const QString &fileName, QObject *parent = 0);
~IMGMap() {delete _data;} ~IMGMap() {qDeleteAll(_data);}
QString name() const {return _data->name();} QString name() const {return _data.first()->name();}
QRectF bounds() {return _bounds;} QRectF bounds() {return _bounds;}
@ -44,7 +44,7 @@ private:
Transform transform(int zoom) const; Transform transform(int zoom) const;
void updateTransform(); void updateTransform();
MapData *_data; QList<MapData *> _data;
int _zoom; int _zoom;
Projection _projection; Projection _projection;
Transform _transform; Transform _transform;