From f9abf21e6d08c1b9aa91ee97843de6b36467e604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Tue, 27 Oct 2020 16:46:09 +0100 Subject: [PATCH] Fixed warious bounds wrapping issues --- src/common/rectc.h | 3 ++- src/map/IMG/trefile.cpp | 15 +++++++++++---- src/map/imgmap.cpp | 9 ++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/common/rectc.h b/src/common/rectc.h index 848f99d2..e0fd5343 100644 --- a/src/common/rectc.h +++ b/src/common/rectc.h @@ -15,7 +15,8 @@ public: bool isNull() const {return _tl.isNull() && _br.isNull();} bool isValid() const - {return (_tl.isValid() && _br.isValid() && _tl != _br);} + {return (_tl.isValid() && _br.isValid() + && _tl.lat() > _br.lat() && _tl.lon() < _br.lon());} Coordinates topLeft() const {return _tl;} Coordinates bottomRight() const {return _br;} diff --git a/src/map/IMG/trefile.cpp b/src/map/IMG/trefile.cpp index 6273623c..745807bd 100644 --- a/src/map/IMG/trefile.cpp +++ b/src/map/IMG/trefile.cpp @@ -5,7 +5,12 @@ static inline double RB(qint32 val) { - return (val == -0x800000 || val == 0x800000) ? 180.0 : toWGS24(val); + return (val == -0x800000 || val >= 0x800000) ? 180.0 : toWGS24(val); +} + +static inline double LB(qint32 val) +{ + return (val <= -0x800000) ? -180.0 : toWGS24(val); } static void demangle(quint8 *data, quint32 size, quint32 key) @@ -60,7 +65,8 @@ bool TREFile::init() return false; _bounds = RectC(Coordinates(toWGS24(west), toWGS24(north)), Coordinates(RB(east), toWGS24(south))); - Q_ASSERT(_bounds.left() <= _bounds.right()); + if (!_bounds.isValid()) + return false; // Levels & subdivs info quint32 levelsOffset, levelsSize, subdivSize; @@ -193,9 +199,10 @@ bool TREFile::load(int idx) sl.append(s); double min[2], max[2]; - RectC bounds(Coordinates(toWGS24(lon - width), toWGS24(lat + height)), + RectC bounds(Coordinates(LB(lon - width), toWGS24(lat + height)), Coordinates(RB(lon + width), toWGS24(lat - height))); - Q_ASSERT(bounds.left() <= bounds.right()); + if (!bounds.isValid()) + goto error; min[0] = bounds.left(); min[1] = bounds.bottom(); diff --git a/src/map/imgmap.cpp b/src/map/imgmap.cpp index 4521c996..0607bf2d 100644 --- a/src/map/imgmap.cpp +++ b/src/map/imgmap.cpp @@ -136,6 +136,9 @@ void IMGMap::updateTransform() RectD prect(_dataBounds, _projection); _bounds = QRectF(_transform.proj2img(prect.topLeft()), _transform.proj2img(prect.bottomRight())); + // Adjust the bounds of world maps to avoid problems with wrapping + if (_dataBounds.left() == -180.0 || _dataBounds.right() == 180.0) + _bounds.adjust(0.5, 0, -0.5, 0); } QPointF IMGMap::ll2xy(const Coordinates &c) @@ -194,7 +197,7 @@ void IMGMap::draw(QPainter *painter, const QRectF &rect, Flags flags) QRectF polyRect(ttl, QPointF(ttl.x() + TILE_SIZE, ttl.y() + TILE_SIZE)); - polyRect &= bounds().adjusted(0.5, 0.5, -0.5, -0.5); + polyRect &= bounds(); RectD polyRectD(_transform.img2proj(polyRect.topLeft()), _transform.img2proj(polyRect.bottomRight())); _data.at(n)->polys(polyRectD.toRectC(_projection, 4), _zoom, @@ -204,7 +207,7 @@ void IMGMap::draw(QPainter *painter, const QRectF &rect, Flags flags) QRectF pointRect(QPointF(ttl.x() - TEXT_EXTENT, ttl.y() - TEXT_EXTENT), QPointF(ttl.x() + TILE_SIZE + TEXT_EXTENT, ttl.y() + TILE_SIZE + TEXT_EXTENT)); - pointRect &= bounds().adjusted(0.5, 0.5, -0.5, -0.5); + pointRect &= bounds(); RectD pointRectD(_transform.img2proj(pointRect.topLeft()), _transform.img2proj(pointRect.bottomRight())); _data.at(n)->points(pointRectD.toRectC(_projection, 4), @@ -241,7 +244,7 @@ void IMGMap::setProjection(const Projection &projection) _projection = projection; // Limit the bounds for some well known Mercator projections - // (GARMIN world maps have N/S bounds up to 90/-90!) + // (world maps have N/S bounds up to 90/-90!) _dataBounds = (_projection == PCS::pcs(3857) || _projection == PCS::pcs(3395)) ? _data.first()->bounds() & OSM::BOUNDS : _data.first()->bounds();