1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-30 22:51:16 +01:00

Added some more map definition checking

This commit is contained in:
Martin Tůma 2018-02-27 01:02:22 +01:00
parent fa99d01a77
commit e1e49b32e6
3 changed files with 140 additions and 59 deletions

View File

@ -14,6 +14,8 @@ public:
int max() const {return _max;} int max() const {return _max;}
int size() const {return (_max - _min);} int size() const {return (_max - _min);}
bool isValid() const {return size() >= 0;}
private: private:
int _min, _max; int _min, _max;
}; };
@ -28,6 +30,8 @@ public:
qreal max() const {return _max;} qreal max() const {return _max;}
qreal size() const {return (_max - _min);} qreal size() const {return (_max - _min);}
bool isValid() const {return size() >= 0;}
void resize(qreal size); void resize(qreal size);
private: private:

View File

@ -4,7 +4,6 @@
#include "wmtsmap.h" #include "wmtsmap.h"
#include "mapsource.h" #include "mapsource.h"
#define ZOOM_MAX 19 #define ZOOM_MAX 19
#define ZOOM_MIN 0 #define ZOOM_MIN 0
#define BOUNDS_LEFT -180 #define BOUNDS_LEFT -180
@ -12,7 +11,12 @@
#define BOUNDS_RIGHT 180 #define BOUNDS_RIGHT 180
#define BOUNDS_BOTTOM -85.0511 #define BOUNDS_BOTTOM -85.0511
Range MapSource::zooms(QXmlStreamReader &reader) MapSource::TMSConfig::TMSConfig()
: zooms(ZOOM_MIN, ZOOM_MAX), bounds(Coordinates(BOUNDS_LEFT, BOUNDS_TOP),
Coordinates(BOUNDS_RIGHT, BOUNDS_BOTTOM)) {}
void MapSource::zooms(QXmlStreamReader &reader, Range &val)
{ {
const QXmlStreamAttributes &attr = reader.attributes(); const QXmlStreamAttributes &attr = reader.attributes();
int min, max; int min, max;
@ -26,7 +30,7 @@ Range MapSource::zooms(QXmlStreamReader &reader)
#endif // QT_VERSION < 5 #endif // QT_VERSION < 5
if (!res || (min < ZOOM_MIN || min > ZOOM_MAX)) { if (!res || (min < ZOOM_MIN || min > ZOOM_MAX)) {
reader.raiseError("Invalid minimal zoom level"); reader.raiseError("Invalid minimal zoom level");
return Range(); return;
} }
} else } else
min = ZOOM_MIN; min = ZOOM_MIN;
@ -39,20 +43,17 @@ Range MapSource::zooms(QXmlStreamReader &reader)
#endif // QT_VERSION < 5 #endif // QT_VERSION < 5
if (!res || (max < ZOOM_MIN || max > ZOOM_MAX)) { if (!res || (max < ZOOM_MIN || max > ZOOM_MAX)) {
reader.raiseError("Invalid maximal zoom level"); reader.raiseError("Invalid maximal zoom level");
return Range(); return;
} }
} else } else
max = ZOOM_MAX; max = ZOOM_MAX;
if (min > max || max < min) { val = Range(min, max);
if (!val.isValid())
reader.raiseError("Invalid maximal/minimal zoom level combination"); reader.raiseError("Invalid maximal/minimal zoom level combination");
return Range();
}
return Range(min, max);
} }
RectC MapSource::bounds(QXmlStreamReader &reader) void MapSource::bounds(QXmlStreamReader &reader, RectC &val)
{ {
const QXmlStreamAttributes &attr = reader.attributes(); const QXmlStreamAttributes &attr = reader.attributes();
double top, left, bottom, right; double top, left, bottom, right;
@ -66,7 +67,7 @@ RectC MapSource::bounds(QXmlStreamReader &reader)
#endif // QT_VERSION < 5 #endif // QT_VERSION < 5
if (!res || (top < BOUNDS_BOTTOM || top > BOUNDS_TOP)) { if (!res || (top < BOUNDS_BOTTOM || top > BOUNDS_TOP)) {
reader.raiseError("Invalid bounds top value"); reader.raiseError("Invalid bounds top value");
return RectC(); return;
} }
} else } else
top = BOUNDS_TOP; top = BOUNDS_TOP;
@ -79,7 +80,7 @@ RectC MapSource::bounds(QXmlStreamReader &reader)
#endif // QT_VERSION < 5 #endif // QT_VERSION < 5
if (!res || (bottom < BOUNDS_BOTTOM || bottom > BOUNDS_TOP)) { if (!res || (bottom < BOUNDS_BOTTOM || bottom > BOUNDS_TOP)) {
reader.raiseError("Invalid bounds bottom value"); reader.raiseError("Invalid bounds bottom value");
return RectC(); return;
} }
} else } else
bottom = BOUNDS_BOTTOM; bottom = BOUNDS_BOTTOM;
@ -92,7 +93,7 @@ RectC MapSource::bounds(QXmlStreamReader &reader)
#endif // QT_VERSION < 5 #endif // QT_VERSION < 5
if (!res || (left < BOUNDS_LEFT || left > BOUNDS_RIGHT)) { if (!res || (left < BOUNDS_LEFT || left > BOUNDS_RIGHT)) {
reader.raiseError("Invalid bounds left value"); reader.raiseError("Invalid bounds left value");
return RectC(); return;
} }
} else } else
left = BOUNDS_LEFT; left = BOUNDS_LEFT;
@ -105,95 +106,138 @@ RectC MapSource::bounds(QXmlStreamReader &reader)
#endif // QT_VERSION < 5 #endif // QT_VERSION < 5
if (!res || (right < BOUNDS_LEFT || right > BOUNDS_RIGHT)) { if (!res || (right < BOUNDS_LEFT || right > BOUNDS_RIGHT)) {
reader.raiseError("Invalid bounds right value"); reader.raiseError("Invalid bounds right value");
return RectC(); return;
} }
} else } else
right = BOUNDS_RIGHT; right = BOUNDS_RIGHT;
if (bottom > top || top < bottom) { if (bottom > top || top < bottom) {
reader.raiseError("Invalid bottom/top bounds combination"); reader.raiseError("Invalid bottom/top bounds combination");
return RectC(); return;
} }
if (left > right || right < left) { if (left > right || right < left) {
reader.raiseError("Invalid left/right bounds combination"); reader.raiseError("Invalid left/right bounds combination");
return RectC(); return;
} }
return RectC(Coordinates(left, top), Coordinates(right, bottom)); val = RectC(Coordinates(left, top), Coordinates(right, bottom));
} }
Map *MapSource::map(QXmlStreamReader &reader) void MapSource::map(QXmlStreamReader &reader, Config &config)
{ {
QString name, url, layer, style, set; config.type = (reader.attributes().value("type") == "WMTS") ? WMTS : TMS;
QString format("image/png");
bool wmts, rest = false, yx = false;
Range z(ZOOM_MIN, ZOOM_MAX);
RectC b(Coordinates(BOUNDS_LEFT, BOUNDS_TOP), Coordinates(BOUNDS_RIGHT,
BOUNDS_BOTTOM));
wmts = (reader.attributes().value("type") == "WMTS") ? true : false;
while (reader.readNextStartElement()) { while (reader.readNextStartElement()) {
if (reader.name() == "name") if (reader.name() == "name")
name = reader.readElementText(); config.name = reader.readElementText();
else if (reader.name() == "url") { else if (reader.name() == "url") {
rest = (reader.attributes().value("type") == "REST") ? true : false; if (config.type == WMTS)
url = reader.readElementText(); config.wmts.rest = (reader.attributes().value("type") == "REST")
? true : false;
config.url = reader.readElementText();
} else if (reader.name() == "zoom") { } else if (reader.name() == "zoom") {
z = zooms(reader); if (config.type == TMS)
zooms(reader, config.tms.zooms);
else
reader.raiseError("Illegal zoom element");
reader.skipCurrentElement(); reader.skipCurrentElement();
} else if (reader.name() == "bounds") { } else if (reader.name() == "bounds") {
b = bounds(reader); if (config.type == TMS)
bounds(reader, config.tms.bounds);
else
reader.raiseError("Illegal bounds element");
reader.skipCurrentElement(); reader.skipCurrentElement();
} else if (reader.name() == "format") } else if (reader.name() == "format") {
format = reader.readElementText(); if (config.type == WMTS)
else if (reader.name() == "layer") config.wmts.format = reader.readElementText();
layer = reader.readElementText(); else
reader.raiseError("Illegal format element");
} else if (reader.name() == "layer")
if (config.type == WMTS)
config.wmts.layer = reader.readElementText();
else
reader.raiseError("Illegal layer element");
else if (reader.name() == "style") else if (reader.name() == "style")
style = reader.readElementText(); if (config.type == WMTS)
config.wmts.style = reader.readElementText();
else
reader.raiseError("Illegal style element");
else if (reader.name() == "set") { else if (reader.name() == "set") {
yx = (reader.attributes().value("axis") == "yx") ? true : false; if (config.type == WMTS) {
set = reader.readElementText(); config.wmts.yx = (reader.attributes().value("axis") == "yx")
? true : false;
config.wmts.set = reader.readElementText();
} else
reader.raiseError("Illegal set element");
} else } else
reader.skipCurrentElement(); reader.skipCurrentElement();
} }
if (reader.error())
return 0;
else if (wmts)
return new WMTSMap(name, WMTS::Setup(url, layer, set, style, format,
rest, yx));
else
return new OnlineMap(name, url, z, b);
} }
Map *MapSource::loadFile(const QString &path) Map *MapSource::loadFile(const QString &path)
{ {
QFile file(path); QFile file(path);
QXmlStreamReader reader; QXmlStreamReader reader;
Map *map = 0; Config config;
if (!file.open(QFile::ReadOnly | QFile::Text)) { if (!file.open(QFile::ReadOnly | QFile::Text)) {
_errorString = file.errorString(); _errorString = file.errorString();
return map; return 0;
} }
reader.setDevice(&file); reader.setDevice(&file);
if (reader.readNextStartElement()) { if (reader.readNextStartElement()) {
if (reader.name() == "map") if (reader.name() == "map")
map = MapSource::map(reader); map(reader, config);
else else
reader.raiseError("Not an online map source file"); reader.raiseError("Not an online map source file");
} }
if (reader.error()) {
if (!map)
_errorString = QString("%1: %2").arg(reader.lineNumber()) _errorString = QString("%1: %2").arg(reader.lineNumber())
.arg(reader.errorString()); .arg(reader.errorString());
else if (!map->isValid()) { return 0;
_errorString = map->errorString();
delete map; map = 0;
} }
return map; if (config.name.isEmpty()) {
_errorString = "Missing name definition";
return 0;
}
if (config.url.isEmpty()) {
_errorString = "Missing URL definition";
return 0;
}
if (config.type == WMTS) {
if (config.wmts.layer.isEmpty()) {
_errorString = "Missing layer definition";
return 0;
}
if (config.wmts.style.isEmpty()) {
_errorString = "Missing style definiton";
return 0;
}
if (config.wmts.set.isEmpty()) {
_errorString = "Missing set definiton";
return 0;
}
if (config.wmts.format.isEmpty()) {
_errorString = "Missing format definition";
return 0;
}
}
Map *m;
if (config.type == WMTS)
m = new WMTSMap(config.name, WMTS::Setup(config.url, config.wmts.layer,
config.wmts.set, config.wmts.style, config.wmts.format,
config.wmts.rest, config.wmts.yx));
else
m = new OnlineMap(config.name, config.url, config.tms.zooms,
config.tms.bounds);
if (!m->isValid()) {
_errorString = m->errorString();
delete m;
return 0;
}
return m;
} }

View File

@ -15,9 +15,42 @@ public:
const QString &errorString() const {return _errorString;} const QString &errorString() const {return _errorString;}
private: private:
RectC bounds(QXmlStreamReader &reader); enum Type {
Range zooms(QXmlStreamReader &reader); TMS,
Map *map(QXmlStreamReader &reader); WMTS
};
struct TMSConfig {
Range zooms;
RectC bounds;
TMSConfig();
};
struct WMTSConfig {
QString layer;
QString style;
QString set;
QString format;
bool rest;
bool yx;
WMTSConfig() : format("image/png"), rest(false), yx(false) {}
};
struct Config {
QString name;
QString url;
Type type;
WMTSConfig wmts;
TMSConfig tms;
Config() : type(TMS) {}
};
void bounds(QXmlStreamReader &reader, RectC &val);
void zooms(QXmlStreamReader &reader, Range &val);
void map(QXmlStreamReader &reader, Config &config);
QString _errorString; QString _errorString;
}; };