1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 11:45:53 +01:00

Compare commits

..

No commits in common. "b156e2502351a8db4c7a4065df7041b99047935a" and "589c602d2117c1f86d64a2961e5be359ce4e4ebe" have entirely different histories.

3 changed files with 44 additions and 2660 deletions

File diff suppressed because it is too large Load Diff

View File

@ -12,26 +12,16 @@
*/
#include <QFileInfo>
#include <QDir>
#include <QTemporaryDir>
#include <QCryptographicHash>
#include <QtEndian>
#include <QUrl>
#include <QRegularExpression>
#include <private/qzipreader_p.h>
#include "kmlparser.h"
static const QTemporaryDir &tempDir()
{
static QTemporaryDir dir;
return dir;
}
#define ZIP_MAGIC 0x04034b50
static bool isZIP(QFile *file)
static inline uint readUInt(const uchar *data)
{
quint32 magic;
return (file->read((char *)&magic, sizeof(magic)) == (qint64)sizeof(magic)
&& qFromLittleEndian(magic) == 0x04034b50);
return (data[0]) + (data[1]<<8) + (data[2]<<16) + (data[3]<<24);
}
qreal KMLParser::number()
@ -522,73 +512,8 @@ void KMLParser::multiGeometry(QList<TrackData> &tracks, QList<Area> &areas,
}
}
void KMLParser::photoOverlay(const Ctx &ctx, QVector<Waypoint> &waypoints,
QMap<QString, QPixmap> &icons)
{
QString name, desc, phone, address, path, link, id;
QDateTime timestamp;
while (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("name"))
name = _reader.readElementText();
else if (_reader.name() == QLatin1String("description"))
desc = _reader.readElementText();
else if (_reader.name() == QLatin1String("phoneNumber"))
phone = _reader.readElementText();
else if (_reader.name() == QLatin1String("address"))
address = _reader.readElementText();
else if (_reader.name() == QLatin1String("TimeStamp"))
timestamp = timeStamp();
else if (_reader.name() == QLatin1String("Style"))
style(ctx.dir, icons);
else if (_reader.name() == QLatin1String("Icon")) {
QString image(icon());
QRegularExpression re("\\$\\[[^\\]]+\\]");
image.replace(re, "0");
QUrl url(image);
if (url.scheme() == "http" || url.scheme() == "https")
link = image;
else {
if (ctx.zip) {
if (tempDir().isValid()) {
QFileInfo fi(image);
QByteArray id(ctx.path.toUtf8() + image.toUtf8());
path = tempDir().path() + "/" + QString("%0.%1").arg(
QCryptographicHash::hash(id, QCryptographicHash::Sha1)
.toHex(), QString(fi.suffix()));
QFile::rename(ctx.dir.absoluteFilePath(image), path);
}
} else
path = ctx.dir.absoluteFilePath(image);
}
} else if (_reader.name() == QLatin1String("Point")) {
waypoints.append(Waypoint());
Waypoint &w = waypoints.last();
w.setName(name);
w.setDescription(desc);
w.setTimestamp(timestamp);
w.setAddress(address);
w.setPhone(phone);
w.setIcon(icons.value(id));
if (!path.isNull())
w.addImage(path);
if (!link.isNull())
w.addLink(Link(link, "Photo Overlay"));
point(w);
} else if (_reader.name() == QLatin1String("styleUrl")) {
id = _reader.readElementText();
id = (id.at(0) == '#') ? id.right(id.size() - 1) : QString();
} else
_reader.skipCurrentElement();
}
}
void KMLParser::placemark(const Ctx &ctx, QList<TrackData> &tracks,
QList<Area> &areas, QVector<Waypoint> &waypoints,
QMap<QString, QPixmap> &icons)
void KMLParser::placemark(QList<TrackData> &tracks, QList<Area> &areas,
QVector<Waypoint> &waypoints, const QMap<QString, QPixmap> &icons)
{
QString name, desc, phone, address, id;
QDateTime timestamp;
@ -604,8 +529,6 @@ void KMLParser::placemark(const Ctx &ctx, QList<TrackData> &tracks,
address = _reader.readElementText();
else if (_reader.name() == QLatin1String("TimeStamp"))
timestamp = timeStamp();
else if (_reader.name() == QLatin1String("Style"))
style(ctx.dir, icons);
else if (_reader.name() == QLatin1String("MultiGeometry"))
multiGeometry(tracks, areas, waypoints, name, desc, timestamp);
else if (_reader.name() == QLatin1String("Point")) {
@ -616,7 +539,8 @@ void KMLParser::placemark(const Ctx &ctx, QList<TrackData> &tracks,
w.setTimestamp(timestamp);
w.setAddress(address);
w.setPhone(phone);
w.setIcon(icons.value(id));
if (!id.isNull())
w.setIcon(icons.value(id));
point(w);
} else if (_reader.name() == QLatin1String("LineString")
|| _reader.name() == QLatin1String("LinearRing")) {
@ -653,7 +577,8 @@ void KMLParser::placemark(const Ctx &ctx, QList<TrackData> &tracks,
}
}
QString KMLParser::icon()
void KMLParser::icon(const QDir &dir, const QString &id,
QMap<QString, QPixmap> &icons)
{
QString path;
@ -664,7 +589,8 @@ QString KMLParser::icon()
_reader.skipCurrentElement();
}
return path;
if (!path.isNull())
icons.insert(id, QPixmap(dir.absoluteFilePath(path)));
}
void KMLParser::iconStyle(const QDir &dir, const QString &id,
@ -672,7 +598,7 @@ void KMLParser::iconStyle(const QDir &dir, const QString &id,
{
while (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("Icon"))
icons.insert(id, QPixmap(dir.absoluteFilePath(icon())));
icon(dir, id, icons);
else
_reader.skipCurrentElement();
}
@ -690,59 +616,51 @@ void KMLParser::style(const QDir &dir, QMap<QString, QPixmap> &icons)
}
}
void KMLParser::folder(const Ctx &ctx, QList<TrackData> &tracks,
void KMLParser::folder(const QDir &dir, QList<TrackData> &tracks,
QList<Area> &areas, QVector<Waypoint> &waypoints,
QMap<QString, QPixmap> &icons)
const QMap<QString, QPixmap> &icons)
{
while (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("Document"))
document(ctx, tracks, areas, waypoints);
else if (_reader.name() == QLatin1String("Folder"))
folder(ctx, tracks, areas, waypoints, icons);
document(dir, tracks, areas, waypoints);
else if (_reader.name() == QLatin1String("Placemark"))
placemark(ctx, tracks, areas, waypoints, icons);
else if (_reader.name() == QLatin1String("PhotoOverlay"))
photoOverlay(ctx, waypoints, icons);
placemark(tracks, areas, waypoints, icons);
else if (_reader.name() == QLatin1String("Folder"))
folder(dir, tracks, areas, waypoints, icons);
else
_reader.skipCurrentElement();
}
}
void KMLParser::document(const Ctx &ctx, QList<TrackData> &tracks,
void KMLParser::document(const QDir &dir, QList<TrackData> &tracks,
QList<Area> &areas, QVector<Waypoint> &waypoints)
{
QMap<QString, QPixmap> icons;
while (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("Document"))
document(ctx, tracks, areas, waypoints);
else if (_reader.name() == QLatin1String("Folder"))
folder(ctx, tracks, areas, waypoints, icons);
document(dir, tracks, areas, waypoints);
else if (_reader.name() == QLatin1String("Placemark"))
placemark(ctx, tracks, areas, waypoints, icons);
else if (_reader.name() == QLatin1String("PhotoOverlay"))
photoOverlay(ctx, waypoints, icons);
placemark(tracks, areas, waypoints, icons);
else if (_reader.name() == QLatin1String("Folder"))
folder(dir, tracks, areas, waypoints, icons);
else if (_reader.name() == QLatin1String("Style"))
style(ctx.dir, icons);
style(dir, icons);
else
_reader.skipCurrentElement();
}
}
void KMLParser::kml(const Ctx &ctx, QList<TrackData> &tracks,
void KMLParser::kml(const QDir &dir, QList<TrackData> &tracks,
QList<Area> &areas, QVector<Waypoint> &waypoints)
{
QMap<QString, QPixmap> icons;
while (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("Document"))
document(ctx, tracks, areas, waypoints);
else if (_reader.name() == QLatin1String("Folder"))
folder(ctx, tracks, areas, waypoints, icons);
document(dir, tracks, areas, waypoints);
else if (_reader.name() == QLatin1String("Placemark"))
placemark(ctx, tracks, areas, waypoints, icons);
else if (_reader.name() == QLatin1String("PhotoOverlay"))
photoOverlay(ctx, waypoints, icons);
placemark(tracks, areas, waypoints, QMap<QString, QPixmap>());
else if (_reader.name() == QLatin1String("Folder"))
folder(dir, tracks, areas, waypoints, QMap<QString, QPixmap>());
else
_reader.skipCurrentElement();
}
@ -752,14 +670,15 @@ bool KMLParser::parse(QFile *file, QList<TrackData> &tracks,
QList<RouteData> &routes, QList<Area> &areas, QVector<Waypoint> &waypoints)
{
Q_UNUSED(routes);
uchar magic[4];
QFileInfo fi(*file);
_reader.clear();
if (isZIP(file)) {
if (file->read((char *)magic, 4) == 4 && readUInt(magic) == ZIP_MAGIC) {
QZipReader zip(fi.absoluteFilePath(), QIODevice::ReadOnly);
QTemporaryDir tempDir;
if (!tempDir.isValid() || !zip.extractAll(tempDir.path()))
if (!zip.extractAll(tempDir.path()))
_reader.raiseError("Error extracting ZIP file");
else {
QDir zipDir(tempDir.path());
@ -777,8 +696,7 @@ bool KMLParser::parse(QFile *file, QList<TrackData> &tracks,
if (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("kml"))
kml(Ctx(fi.absoluteFilePath(), zipDir, true),
tracks, areas, waypoints);
kml(zipDir, tracks, areas, waypoints);
else
_reader.raiseError("Not a KML file");
}
@ -791,8 +709,7 @@ bool KMLParser::parse(QFile *file, QList<TrackData> &tracks,
if (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("kml"))
kml(Ctx(fi.absoluteFilePath(), fi.absoluteDir(), false), tracks,
areas, waypoints);
kml(fi.absoluteDir(), tracks, areas, waypoints);
else
_reader.raiseError("Not a KML file");
}

View File

@ -3,9 +3,10 @@
#include <QXmlStreamReader>
#include <QDateTime>
#include <QDir>
#include "parser.h"
class QDir;
class KMLParser : public Parser
{
public:
@ -15,28 +16,17 @@ public:
int errorLine() const {return _reader.lineNumber();}
private:
struct Ctx {
Ctx(const QString &path, const QDir &dir, bool zip)
: path(path), dir(dir), zip(zip) {}
QString path;
QDir dir;
bool zip;
};
void kml(const Ctx &ctx, QList<TrackData> &tracks, QList<Area> &areas,
void kml(const QDir &dir, QList<TrackData> &tracks, QList<Area> &areas,
QVector<Waypoint> &waypoints);
void document(const Ctx &ctx, QList<TrackData> &tracks, QList<Area> &areas,
void document(const QDir &dir, QList<TrackData> &tracks, QList<Area> &areas,
QVector<Waypoint> &waypoints);
void folder(const Ctx &ctx, QList<TrackData> &tracks, QList<Area> &areas,
QVector<Waypoint> &waypoints, QMap<QString, QPixmap> &icons);
void placemark(const Ctx &ctx, QList<TrackData> &tracks, QList<Area> &areas,
QVector<Waypoint> &waypoints, QMap<QString, QPixmap> &icons);
void folder(const QDir &dir, QList<TrackData> &tracks, QList<Area> &areas,
QVector<Waypoint> &waypoints, const QMap<QString, QPixmap> &icons);
void placemark(QList<TrackData> &tracks, QList<Area> &areas,
QVector<Waypoint> &waypoints, const QMap<QString, QPixmap> &icons);
void multiGeometry(QList<TrackData> &tracks, QList<Area> &areas,
QVector<Waypoint> &waypoints, const QString &name, const QString &desc,
const QDateTime &timestamp);
void photoOverlay(const Ctx &ctx, QVector<Waypoint> &waypoints,
QMap<QString, QPixmap> &icons);
void track(SegmentData &segment);
void multiTrack(TrackData &t);
void lineString(SegmentData &segment);
@ -57,10 +47,10 @@ private:
QDateTime timeStamp();
qreal number();
QDateTime time();
QString icon();
void style(const QDir &dir, QMap<QString, QPixmap> &icons);
void iconStyle(const QDir &dir, const QString &id,
QMap<QString, QPixmap> &icons);
void icon(const QDir &dir, const QString &id, QMap<QString, QPixmap> &icons);
QXmlStreamReader _reader;
};