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

Do not depend on a specific tile name when gathering tile info.

This commit is contained in:
Martin Tůma 2017-03-21 19:02:29 +01:00
parent 60d9a172b6
commit ac4ef0631e
2 changed files with 58 additions and 55 deletions

View File

@ -4,6 +4,7 @@
#include <QMap> #include <QMap>
#include <QDir> #include <QDir>
#include <QBuffer> #include <QBuffer>
#include <QImage>
#include <QPixmapCache> #include <QPixmapCache>
#include "misc.h" #include "misc.h"
#include "rd.h" #include "rd.h"
@ -154,47 +155,64 @@ bool OfflineMap::computeResolution(QList<ReferencePoint> &points)
return true; return true;
} }
bool OfflineMap::getTileName(const QStringList &tiles) bool OfflineMap::getImageInfo(const QString &path)
{ {
if (tiles.isEmpty()) { QFileInfo ii(_imgPath);
qWarning("%s: empty tile set", qPrintable(_name)); if (ii.isRelative())
return false; _imgPath = path + "/" + _imgPath;
} ii = QFileInfo(_imgPath);
for (int i = 0; i < tiles.size(); i++) { if (!ii.exists()) {
if (tiles.at(i).contains("_0_0.")) { qWarning("%s: %s: no such image", qPrintable(_name),
_tileName = QString(tiles.at(i)).replace("_0_0.", "_%1_%2."); qPrintable(ii.absoluteFilePath()));
break;
}
}
if (_tileName.isNull()) {
qWarning("%s: invalid tile names", qPrintable(_name));
return false; return false;
} }
return true; return true;
} }
bool OfflineMap::getTileSize() bool OfflineMap::getTileInfo(const QStringList &tiles, const QString &path)
{ {
QString tileName(_tileName.arg(QString::number(0), QString::number(0))); if (tiles.isEmpty()) {
qWarning("%s: empty tile set", qPrintable(_name));
return false;
}
QRegExp rx("_[0-9]+_[0-9]+\\.");
for (int i = 0; i < tiles.size(); i++) {
if (tiles.at(i).contains(rx)) {
_tileName = QString(tiles.at(i)).replace(rx, "_%1_%2.");
QImage tile; QImage tile;
if (_tar.isOpen()) { if (_tar.isOpen()) {
QByteArray ba = _tar.file(tileName); QByteArray ba = _tar.file(tiles.at(i));
tile = QImage::fromData(ba); tile = QImage::fromData(ba);
} else } else {
tile = QImage(tileName); _tileName = path + "/" + _tileName;
tile = QImage(path + "/" + tiles.at(i));
}
if (tile.isNull()) { if (tile.isNull()) {
qWarning("%s: error retrieving tile size: %s: invalid image", qWarning("%s: error retrieving tile size: %s: invalid image",
qPrintable(_name), qPrintable(QFileInfo(tileName).fileName())); qPrintable(_name), qPrintable(QFileInfo(tiles.at(i))
.fileName()));
return false; return false;
} }
_tileSize = tile.size(); _tileSize = tile.size();
if (!_size.isValid()) {
qWarning("%s: missing or invalid image size (IWH)",
qPrintable(_name));
return false;
}
return true; return true;
}
}
qWarning("%s: invalid tile names", qPrintable(_name));
return false;
} }
OfflineMap::OfflineMap(const QString &path, QObject *parent) : Map(parent) OfflineMap::OfflineMap(const QString &path, QObject *parent) : Map(parent)
@ -213,7 +231,7 @@ OfflineMap::OfflineMap(const QString &path, QObject *parent) : Map(parent)
for (int i = 0; i < mapFiles.count(); i++) { for (int i = 0; i < mapFiles.count(); i++) {
const QString &fileName = mapFiles.at(i).fileName(); const QString &fileName = mapFiles.at(i).fileName();
if (fileName.endsWith(".tar")) { if (fileName.endsWith(".tar")) {
if (!_tar.load(mapFiles.at(0).absoluteFilePath())) { if (!_tar.load(mapFiles.at(i).absoluteFilePath())) {
qWarning("%s: %s: error loading tar file", qPrintable(_name), qWarning("%s: %s: error loading tar file", qPrintable(_name),
qPrintable(fileName)); qPrintable(fileName));
return; return;
@ -251,36 +269,19 @@ OfflineMap::OfflineMap(const QString &path, QObject *parent) : Map(parent)
} }
computeResolution(points); computeResolution(points);
if (!_size.isValid()) {
qWarning("%s: missing or invalid image size (IWH)", qPrintable(_name));
return;
}
if (_tar.isOpen()) { if (_tar.isOpen()) {
if (!getTileName(_tar.files())) if (!getTileInfo(_tar.files()))
return;
if (!getTileSize())
return; return;
} else { } else {
QDir set(fi.absoluteFilePath() + "/" + "set"); QDir set(fi.absoluteFilePath() + "/" + "set");
if (set.exists()) { if (set.exists()) {
if (!getTileName(set.entryList())) if (!getTileInfo(set.entryList(), set.absolutePath()))
return;
_tileName = set.absolutePath() + "/" + _tileName;
if (!getTileSize())
return; return;
} else { } else {
QFileInfo ii(_imgPath); if (!getImageInfo(fi.absoluteFilePath()))
if (ii.isRelative())
_imgPath = fi.absoluteFilePath() + "/" + _imgPath;
ii = QFileInfo(_imgPath);
if (!ii.exists()) {
qWarning("%s: %s: no such image", qPrintable(_name),
qPrintable(ii.absoluteFilePath()));
return; return;
} }
} }
}
_img = 0; _img = 0;
_valid = true; _valid = true;
@ -292,7 +293,10 @@ void OfflineMap::load()
return; return;
_img = new QImage(_imgPath); _img = new QImage(_imgPath);
if (_img->isNull()) if (!_img->isNull()) {
if (!_size.isValid())
_size = _img->size();
} else
qWarning("%s: error loading map image", qPrintable(_name)); qWarning("%s: error loading map image", qPrintable(_name));
} }

View File

@ -1,14 +1,13 @@
#ifndef OFFLINEMAP_H #ifndef OFFLINEMAP_H
#define OFFLINEMAP_H #define OFFLINEMAP_H
#include <QImage>
#include <QTransform> #include <QTransform>
#include "map.h" #include "map.h"
#include "tar.h" #include "tar.h"
#include "coordinates.h" #include "coordinates.h"
class QIODevice; class QIODevice;
class QDir; class QImage;
class OfflineMap : public Map class OfflineMap : public Map
{ {
@ -43,8 +42,8 @@ private:
int parseMapFile(QIODevice &device, QList<ReferencePoint> &points); int parseMapFile(QIODevice &device, QList<ReferencePoint> &points);
bool computeTransformation(const QList<ReferencePoint> &points); bool computeTransformation(const QList<ReferencePoint> &points);
bool computeResolution(QList<ReferencePoint> &points); bool computeResolution(QList<ReferencePoint> &points);
bool getTileName(const QStringList &tiles); bool getTileInfo(const QStringList &tiles, const QString &path = QString());
bool getTileSize(); bool getImageInfo(const QString &path);
QString _name; QString _name;
QSize _size; QSize _size;