1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-25 04:05:53 +01:00
GPXSee/src/map/mapsource.cpp

278 lines
8.3 KiB
C++
Raw Normal View History

#include <QFile>
#include <QXmlStreamReader>
#include "onlinemap.h"
2018-02-20 23:37:19 +01:00
#include "wmtsmap.h"
#include "wmsmap.h"
2018-09-22 14:17:24 +02:00
#include "osm.h"
#include "invalidmap.h"
2018-01-29 00:19:57 +01:00
#include "mapsource.h"
2018-09-25 21:07:44 +02:00
MapSource::Config::Config() : type(OSM), zooms(OSM::ZOOMS), bounds(OSM::BOUNDS),
format("image/png"), rest(false), tileRatio(1.0), tileSize(256),
scalable(false) {}
2018-04-07 18:42:25 +02:00
static CoordinateSystem coordinateSystem(QXmlStreamReader &reader)
{
QXmlStreamAttributes attr = reader.attributes();
if (attr.value("axis") == QLatin1String("yx"))
2018-04-05 21:13:48 +02:00
return CoordinateSystem::YX;
else if (attr.value("axis") == QLatin1String("xy"))
2018-04-05 21:13:48 +02:00
return CoordinateSystem::XY;
else
2018-04-05 21:13:48 +02:00
return CoordinateSystem::Unknown;
}
2018-02-27 21:50:29 +01:00
Range MapSource::zooms(QXmlStreamReader &reader)
{
const QXmlStreamAttributes &attr = reader.attributes();
int min, max;
bool res;
if (attr.hasAttribute("min")) {
min = attr.value("min").toString().toInt(&res);
if (!res || min < 0) {
reader.raiseError("Invalid minimal zoom level");
2018-02-27 21:50:29 +01:00
return Range();
}
} else
2018-09-25 21:07:44 +02:00
min = OSM::ZOOMS.min();
if (attr.hasAttribute("max")) {
max = attr.value("max").toString().toInt(&res);
if (!res || max < min) {
reader.raiseError("Invalid maximal zoom level");
2018-02-27 21:50:29 +01:00
return Range();
}
} else
2018-09-25 21:07:44 +02:00
max = OSM::ZOOMS.max();
2018-02-27 21:50:29 +01:00
return Range(min, max);
}
2018-02-27 21:50:29 +01:00
RectC MapSource::bounds(QXmlStreamReader &reader)
{
const QXmlStreamAttributes &attr = reader.attributes();
double top, left, bottom, right;
bool res;
if (attr.hasAttribute("top")) {
top = attr.value("top").toString().toDouble(&res);
2018-09-25 21:07:44 +02:00
if (!res || (top < OSM::BOUNDS.bottom() || top > OSM::BOUNDS.top())) {
reader.raiseError("Invalid bounds top value");
2018-02-27 21:50:29 +01:00
return RectC();
}
} else
2018-09-25 21:07:44 +02:00
top = OSM::BOUNDS.top();
if (attr.hasAttribute("bottom")) {
bottom = attr.value("bottom").toString().toDouble(&res);
2018-09-25 21:07:44 +02:00
if (!res || (bottom < OSM::BOUNDS.bottom()
|| bottom > OSM::BOUNDS.top())) {
reader.raiseError("Invalid bounds bottom value");
2018-02-27 21:50:29 +01:00
return RectC();
}
} else
2018-09-25 21:07:44 +02:00
bottom = OSM::BOUNDS.bottom();
if (attr.hasAttribute("left")) {
left = attr.value("left").toString().toDouble(&res);
2018-09-25 21:07:44 +02:00
if (!res || (left < OSM::BOUNDS.left() || left > OSM::BOUNDS.right())) {
reader.raiseError("Invalid bounds left value");
2018-02-27 21:50:29 +01:00
return RectC();
}
} else
2018-09-25 21:07:44 +02:00
left = OSM::BOUNDS.left();
if (attr.hasAttribute("right")) {
right = attr.value("right").toString().toDouble(&res);
2018-09-25 21:07:44 +02:00
if (!res || (right < OSM::BOUNDS.left()
|| right > OSM::BOUNDS.right())) {
reader.raiseError("Invalid bounds right value");
2018-02-27 21:50:29 +01:00
return RectC();
}
} else
2018-09-25 21:07:44 +02:00
right = OSM::BOUNDS.right();
2018-02-27 21:50:29 +01:00
if (bottom >= top) {
reader.raiseError("Invalid bottom/top bounds combination");
2018-02-27 21:50:29 +01:00
return RectC();
}
2018-02-27 21:50:29 +01:00
if (left >= right) {
reader.raiseError("Invalid left/right bounds combination");
2018-02-27 21:50:29 +01:00
return RectC();
}
2018-02-27 21:50:29 +01:00
return RectC(Coordinates(left, top), Coordinates(right, bottom));
}
2018-12-08 02:27:27 +01:00
void MapSource::tile(QXmlStreamReader &reader, Config &config)
{
QXmlStreamAttributes attr = reader.attributes();
bool ok;
if (attr.hasAttribute("size")) {
int size = attr.value("size").toString().toInt(&ok);
if (!ok || size < 0) {
reader.raiseError("Invalid tile size");
return;
} else
config.tileSize = size;
}
if (attr.hasAttribute("type")) {
if (attr.value("type") == QLatin1String("raster"))
2018-12-08 02:27:27 +01:00
config.scalable = false;
else if (attr.value("type") == QLatin1String("vector"))
2018-12-08 02:27:27 +01:00
config.scalable = true;
else {
reader.raiseError("Invalid tile type");
return;
}
}
if (attr.hasAttribute("pixelRatio")) {
qreal ratio = attr.value("pixelRatio").toString().toDouble(&ok);
if (!ok || ratio < 0) {
reader.raiseError("Invalid tile pixelRatio");
return;
} else
config.tileRatio = ratio;
}
}
void MapSource::map(QXmlStreamReader &reader, Config &config)
{
const QXmlStreamAttributes &attr = reader.attributes();
QStringView type = attr.value("type");
2018-09-22 13:32:54 +02:00
if (type == QLatin1String("WMTS"))
2018-09-22 13:32:54 +02:00
config.type = WMTS;
else if (type == QLatin1String("WMS"))
2018-09-22 13:32:54 +02:00
config.type = WMS;
else if (type == QLatin1String("TMS"))
2018-09-22 13:32:54 +02:00
config.type = TMS;
else if (type == QLatin1String("QuadTiles"))
2019-05-20 23:23:24 +02:00
config.type = QuadTiles;
else if (type == QLatin1String("OSM") || type.isEmpty())
2018-09-22 13:32:54 +02:00
config.type = OSM;
else {
reader.raiseError("Invalid map type");
return;
}
2018-02-20 23:37:19 +01:00
while (reader.readNextStartElement()) {
if (reader.name() == QLatin1String("name"))
config.name = reader.readElementText();
else if (reader.name() == QLatin1String("url")) {
config.rest = (reader.attributes().value("type")
== QLatin1String("REST")) ? true : false;
config.url = reader.readElementText();
} else if (reader.name() == QLatin1String("zoom")) {
config.zooms = zooms(reader);
reader.skipCurrentElement();
} else if (reader.name() == QLatin1String("bounds")) {
config.bounds = bounds(reader);
reader.skipCurrentElement();
} else if (reader.name() == QLatin1String("format"))
config.format = reader.readElementText();
else if (reader.name() == QLatin1String("layer"))
config.layer = reader.readElementText();
else if (reader.name() == QLatin1String("style"))
config.style = reader.readElementText();
else if (reader.name() == QLatin1String("set")) {
2018-04-07 18:42:25 +02:00
config.coordinateSystem = coordinateSystem(reader);
config.set = reader.readElementText();
} else if (reader.name() == QLatin1String("dimension")) {
2018-03-11 10:31:41 +01:00
QXmlStreamAttributes attr = reader.attributes();
if (!attr.hasAttribute("id"))
reader.raiseError("Missing dimension id");
else
config.dimensions.append(KV<QString, QString>
(attr.value("id").toString(), reader.readElementText()));
} else if (reader.name() == QLatin1String("crs")) {
2018-04-07 18:42:25 +02:00
config.coordinateSystem = coordinateSystem(reader);
config.crs = reader.readElementText();
} else if (reader.name() == QLatin1String("authorization")) {
QXmlStreamAttributes attr = reader.attributes();
config.authorization = Authorization(
attr.value("username").toString(),
attr.value("password").toString());
reader.skipCurrentElement();
} else if (reader.name() == QLatin1String("tile")) {
2018-12-08 02:27:27 +01:00
tile(reader, config);
reader.skipCurrentElement();
} else
reader.skipCurrentElement();
}
}
Map *MapSource::create(const QString &path, bool *isDir)
{
Config config;
2018-05-22 22:40:15 +02:00
QFile file(path);
if (isDir)
*isDir = false;
if (!file.open(QFile::ReadOnly | QFile::Text))
return new InvalidMap(path, file.errorString());
2018-05-22 22:40:15 +02:00
QXmlStreamReader reader(&file);
if (reader.readNextStartElement()) {
if (reader.name() == QLatin1String("map"))
map(reader, config);
else
2018-01-29 00:19:57 +01:00
reader.raiseError("Not an online map source file");
}
if (reader.error())
return new InvalidMap(path, QString("%1: %2").arg(reader.lineNumber())
.arg(reader.errorString()));
if (config.name.isEmpty())
return new InvalidMap(path, "Missing name definition");
if (config.url.isEmpty())
return new InvalidMap(path, "Missing URL definition");
if (config.type == WMTS || config.type == WMS) {
if (config.layer.isEmpty())
return new InvalidMap(path, "Missing layer definition");
if (config.format.isEmpty())
return new InvalidMap(path, "Missing format definition");
}
if (config.type == WMTS) {
if (config.set.isEmpty())
return new InvalidMap(path, "Missing set definiton");
}
if (config.type == WMS) {
if (config.crs.isEmpty())
return new InvalidMap(path, "Missing CRS definiton");
}
2018-09-22 13:32:54 +02:00
switch (config.type) {
case WMTS:
2020-12-02 23:58:11 +01:00
return new WMTSMap(path, config.name, WMTS::Setup(config.url,
config.layer, config.set, config.style, config.format, config.rest,
2018-09-22 13:32:54 +02:00
config.coordinateSystem, config.dimensions, config.authorization),
config.tileRatio);
case WMS:
2020-12-02 23:58:11 +01:00
return new WMSMap(path, config.name, WMS::Setup(config.url,
config.layer, config.style, config.format, config.crs,
config.coordinateSystem, config.dimensions, config.authorization),
config.tileSize);
2018-09-22 13:32:54 +02:00
case TMS:
2020-12-02 23:58:11 +01:00
return new OnlineMap(path, config.name, config.url, config.zooms,
config.bounds, config.tileRatio, config.authorization,
2019-05-20 23:23:24 +02:00
config.tileSize, config.scalable, true, false);
2018-09-22 13:32:54 +02:00
case OSM:
2020-12-02 23:58:11 +01:00
return new OnlineMap(path, config.name, config.url, config.zooms,
config.bounds, config.tileRatio, config.authorization,
2019-05-20 23:23:24 +02:00
config.tileSize, config.scalable, false, false);
case QuadTiles:
2020-12-02 23:58:11 +01:00
return new OnlineMap(path, config.name, config.url, config.zooms,
2019-05-20 23:23:24 +02:00
config.bounds, config.tileRatio, config.authorization,
config.tileSize, config.scalable, false, true);
2018-09-22 13:32:54 +02:00
default:
return new InvalidMap(path, "Invalid map type");
2018-09-22 13:32:54 +02:00
}
}