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()) {
QImage tile; qWarning("%s: empty tile set", qPrintable(_name));
return false;
if (_tar.isOpen()) {
QByteArray ba = _tar.file(tileName);
tile = QImage::fromData(ba);
} else
tile = QImage(tileName);
if (tile.isNull()) {
qWarning("%s: error retrieving tile size: %s: invalid image",
qPrintable(_name), qPrintable(QFileInfo(tileName).fileName()));
return false;
} }
_tileSize = tile.size(); 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.");
return true; QImage tile;
if (_tar.isOpen()) {
QByteArray ba = _tar.file(tiles.at(i));
tile = QImage::fromData(ba);
} else {
_tileName = path + "/" + _tileName;
tile = QImage(path + "/" + tiles.at(i));
}
if (tile.isNull()) {
qWarning("%s: error retrieving tile size: %s: invalid image",
qPrintable(_name), qPrintable(QFileInfo(tiles.at(i))
.fileName()));
return false;
}
_tileSize = tile.size();
if (!_size.isValid()) {
qWarning("%s: missing or invalid image size (IWH)",
qPrintable(_name));
return false;
}
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,34 +269,17 @@ 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;
}
} }
} }
@ -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;