1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 19:55: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 <QFileInfo>
#include <QDir>
#include <QTemporaryDir> #include <QTemporaryDir>
#include <QCryptographicHash>
#include <QtEndian>
#include <QUrl>
#include <QRegularExpression>
#include <private/qzipreader_p.h> #include <private/qzipreader_p.h>
#include "kmlparser.h" #include "kmlparser.h"
static const QTemporaryDir &tempDir() #define ZIP_MAGIC 0x04034b50
{
static QTemporaryDir dir;
return dir;
}
static bool isZIP(QFile *file) static inline uint readUInt(const uchar *data)
{ {
quint32 magic; return (data[0]) + (data[1]<<8) + (data[2]<<16) + (data[3]<<24);
return (file->read((char *)&magic, sizeof(magic)) == (qint64)sizeof(magic)
&& qFromLittleEndian(magic) == 0x04034b50);
} }
qreal KMLParser::number() 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, void KMLParser::placemark(QList<TrackData> &tracks, QList<Area> &areas,
QMap<QString, QPixmap> &icons) QVector<Waypoint> &waypoints, const 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)
{ {
QString name, desc, phone, address, id; QString name, desc, phone, address, id;
QDateTime timestamp; QDateTime timestamp;
@ -604,8 +529,6 @@ void KMLParser::placemark(const Ctx &ctx, QList<TrackData> &tracks,
address = _reader.readElementText(); address = _reader.readElementText();
else if (_reader.name() == QLatin1String("TimeStamp")) else if (_reader.name() == QLatin1String("TimeStamp"))
timestamp = timeStamp(); timestamp = timeStamp();
else if (_reader.name() == QLatin1String("Style"))
style(ctx.dir, icons);
else if (_reader.name() == QLatin1String("MultiGeometry")) else if (_reader.name() == QLatin1String("MultiGeometry"))
multiGeometry(tracks, areas, waypoints, name, desc, timestamp); multiGeometry(tracks, areas, waypoints, name, desc, timestamp);
else if (_reader.name() == QLatin1String("Point")) { else if (_reader.name() == QLatin1String("Point")) {
@ -616,6 +539,7 @@ void KMLParser::placemark(const Ctx &ctx, QList<TrackData> &tracks,
w.setTimestamp(timestamp); w.setTimestamp(timestamp);
w.setAddress(address); w.setAddress(address);
w.setPhone(phone); w.setPhone(phone);
if (!id.isNull())
w.setIcon(icons.value(id)); w.setIcon(icons.value(id));
point(w); point(w);
} else if (_reader.name() == QLatin1String("LineString") } else if (_reader.name() == QLatin1String("LineString")
@ -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; QString path;
@ -664,7 +589,8 @@ QString KMLParser::icon()
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
return path; if (!path.isNull())
icons.insert(id, QPixmap(dir.absoluteFilePath(path)));
} }
void KMLParser::iconStyle(const QDir &dir, const QString &id, 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()) { while (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("Icon")) if (_reader.name() == QLatin1String("Icon"))
icons.insert(id, QPixmap(dir.absoluteFilePath(icon()))); icon(dir, id, icons);
else else
_reader.skipCurrentElement(); _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, QList<Area> &areas, QVector<Waypoint> &waypoints,
QMap<QString, QPixmap> &icons) const QMap<QString, QPixmap> &icons)
{ {
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("Document")) if (_reader.name() == QLatin1String("Document"))
document(ctx, tracks, areas, waypoints); document(dir, tracks, areas, waypoints);
else if (_reader.name() == QLatin1String("Folder"))
folder(ctx, tracks, areas, waypoints, icons);
else if (_reader.name() == QLatin1String("Placemark")) else if (_reader.name() == QLatin1String("Placemark"))
placemark(ctx, tracks, areas, waypoints, icons); placemark(tracks, areas, waypoints, icons);
else if (_reader.name() == QLatin1String("PhotoOverlay")) else if (_reader.name() == QLatin1String("Folder"))
photoOverlay(ctx, waypoints, icons); folder(dir, tracks, areas, waypoints, icons);
else else
_reader.skipCurrentElement(); _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) QList<Area> &areas, QVector<Waypoint> &waypoints)
{ {
QMap<QString, QPixmap> icons; QMap<QString, QPixmap> icons;
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("Document")) if (_reader.name() == QLatin1String("Document"))
document(ctx, tracks, areas, waypoints); document(dir, tracks, areas, waypoints);
else if (_reader.name() == QLatin1String("Folder"))
folder(ctx, tracks, areas, waypoints, icons);
else if (_reader.name() == QLatin1String("Placemark")) else if (_reader.name() == QLatin1String("Placemark"))
placemark(ctx, tracks, areas, waypoints, icons); placemark(tracks, areas, waypoints, icons);
else if (_reader.name() == QLatin1String("PhotoOverlay")) else if (_reader.name() == QLatin1String("Folder"))
photoOverlay(ctx, waypoints, icons); folder(dir, tracks, areas, waypoints, icons);
else if (_reader.name() == QLatin1String("Style")) else if (_reader.name() == QLatin1String("Style"))
style(ctx.dir, icons); style(dir, icons);
else else
_reader.skipCurrentElement(); _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) QList<Area> &areas, QVector<Waypoint> &waypoints)
{ {
QMap<QString, QPixmap> icons;
while (_reader.readNextStartElement()) { while (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("Document")) if (_reader.name() == QLatin1String("Document"))
document(ctx, tracks, areas, waypoints); document(dir, tracks, areas, waypoints);
else if (_reader.name() == QLatin1String("Folder"))
folder(ctx, tracks, areas, waypoints, icons);
else if (_reader.name() == QLatin1String("Placemark")) else if (_reader.name() == QLatin1String("Placemark"))
placemark(ctx, tracks, areas, waypoints, icons); placemark(tracks, areas, waypoints, QMap<QString, QPixmap>());
else if (_reader.name() == QLatin1String("PhotoOverlay")) else if (_reader.name() == QLatin1String("Folder"))
photoOverlay(ctx, waypoints, icons); folder(dir, tracks, areas, waypoints, QMap<QString, QPixmap>());
else else
_reader.skipCurrentElement(); _reader.skipCurrentElement();
} }
@ -752,14 +670,15 @@ bool KMLParser::parse(QFile *file, QList<TrackData> &tracks,
QList<RouteData> &routes, QList<Area> &areas, QVector<Waypoint> &waypoints) QList<RouteData> &routes, QList<Area> &areas, QVector<Waypoint> &waypoints)
{ {
Q_UNUSED(routes); Q_UNUSED(routes);
uchar magic[4];
QFileInfo fi(*file); QFileInfo fi(*file);
_reader.clear(); _reader.clear();
if (isZIP(file)) { if (file->read((char *)magic, 4) == 4 && readUInt(magic) == ZIP_MAGIC) {
QZipReader zip(fi.absoluteFilePath(), QIODevice::ReadOnly); QZipReader zip(fi.absoluteFilePath(), QIODevice::ReadOnly);
QTemporaryDir tempDir; QTemporaryDir tempDir;
if (!tempDir.isValid() || !zip.extractAll(tempDir.path())) if (!zip.extractAll(tempDir.path()))
_reader.raiseError("Error extracting ZIP file"); _reader.raiseError("Error extracting ZIP file");
else { else {
QDir zipDir(tempDir.path()); QDir zipDir(tempDir.path());
@ -777,8 +696,7 @@ bool KMLParser::parse(QFile *file, QList<TrackData> &tracks,
if (_reader.readNextStartElement()) { if (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("kml")) if (_reader.name() == QLatin1String("kml"))
kml(Ctx(fi.absoluteFilePath(), zipDir, true), kml(zipDir, tracks, areas, waypoints);
tracks, areas, waypoints);
else else
_reader.raiseError("Not a KML file"); _reader.raiseError("Not a KML file");
} }
@ -791,8 +709,7 @@ bool KMLParser::parse(QFile *file, QList<TrackData> &tracks,
if (_reader.readNextStartElement()) { if (_reader.readNextStartElement()) {
if (_reader.name() == QLatin1String("kml")) if (_reader.name() == QLatin1String("kml"))
kml(Ctx(fi.absoluteFilePath(), fi.absoluteDir(), false), tracks, kml(fi.absoluteDir(), tracks, areas, waypoints);
areas, waypoints);
else else
_reader.raiseError("Not a KML file"); _reader.raiseError("Not a KML file");
} }

View File

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