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

Improved error handling + code cleanup

This commit is contained in:
Martin Tůma 2018-05-24 22:11:00 +02:00
parent 31ff81576c
commit e4feeae064

View File

@ -6,6 +6,8 @@
#include "jnxmap.h" #include "jnxmap.h"
#define ic2dc(x) ((x) * 180.0 / 0x7FFFFFFF)
struct Level { struct Level {
quint32 count; quint32 count;
quint32 offset; quint32 offset;
@ -59,17 +61,19 @@ bool JNXMap::readTiles()
&& readValue(levels))) && readValue(levels)))
return false; return false;
_bounds = RectC(Coordinates(lon1 * 180.0 / 0x7FFFFFFF, lat1 * 180.0 _bounds = RectC(Coordinates(ic2dc(lon1), ic2dc(lat1)),
/ 0x7FFFFFFF), Coordinates(lon2 * 180.0 / 0x7FFFFFFF, lat2 * 180.0 Coordinates(ic2dc(lon2), ic2dc(lat2)));
/ 0x7FFFFFFF)); if (!levels || !_bounds.isValid())
return false;
if (!_file.seek(version > 3 ? 0x34 : 0x30)) if (!_file.seek(version > 3 ? 0x34 : 0x30))
return false; return false;
QVector<Level> lh(levels); QVector<Level> lh(levels);
for (int i = 0; i < lh.count(); i++) { for (int i = 0; i < lh.count(); i++) {
if (!(readValue(lh[i].count) && readValue(lh[i].offset) Level &l = lh[i];
&& readValue(lh[i].scale)))
if (!(readValue(l.count) && readValue(l.offset) && readValue(l.scale)))
return false; return false;
if (version > 3) { if (version > 3) {
QByteArray ba; QByteArray ba;
@ -80,12 +84,15 @@ bool JNXMap::readTiles()
_zooms = QVector<Zoom>(lh.size()); _zooms = QVector<Zoom>(lh.size());
for (int i = 0; i < lh.count(); i++) { for (int i = 0; i < lh.count(); i++) {
if (!_file.seek(lh.at(i).offset)) Zoom &z = _zooms[i];
const Level &l = lh.at(i);
if (!_file.seek(l.offset))
return false; return false;
_zooms[i].tiles = QVector<Tile>(lh.at(i).count); z.tiles = QVector<Tile>(l.count);
for (quint32 j = 0; j < lh.at(i).count; j++) { for (quint32 j = 0; j < l.count; j++) {
Tile &tile = _zooms[i].tiles[j]; Tile &tile = z.tiles[j];
qint32 top, right, bottom, left; qint32 top, right, bottom, left;
quint16 width, height; quint16 width, height;
@ -95,19 +102,17 @@ bool JNXMap::readTiles()
&& readValue(tile.offset))) && readValue(tile.offset)))
return false; return false;
RectD rect(PointD(left * 180.0 / 0x7FFFFFFF, top * 180.0 RectD rect(PointD(ic2dc(left), ic2dc(top)), PointD(ic2dc(right),
/ 0x7FFFFFFF), PointD(right * 180.0 / 0x7FFFFFFF, bottom * 180.0 ic2dc(bottom)));
/ 0x7FFFFFFF));
if (j == 0) { if (j == 0) {
ReferencePoint tl(PointD(0, 0), rect.topLeft()); ReferencePoint tl(PointD(0, 0), rect.topLeft());
ReferencePoint br(PointD(width, height), rect.bottomRight()); ReferencePoint br(PointD(width, height), rect.bottomRight());
_zooms[i].transform = Transform(tl, br); z.transform = Transform(tl, br);
} }
const Transform &t = _zooms.at(i).transform; QRectF trect(z.transform.proj2img(rect.topLeft()),
QRectF trect(t.proj2img(rect.topLeft()), z.transform.proj2img(rect.bottomRight()));
t.proj2img(rect.bottomRight()));
tile.pos = trect.topLeft(); tile.pos = trect.topLeft();
qreal min[2], max[2]; qreal min[2], max[2];
@ -115,7 +120,7 @@ bool JNXMap::readTiles()
min[1] = trect.top(); min[1] = trect.top();
max[0] = trect.right(); max[0] = trect.right();
max[1] = trect.bottom(); max[1] = trect.bottom();
_zooms[i].tree.Insert(min, max, &tile); z.tree.Insert(min, max, &tile);
} }
} }