1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-12-03 16:09:08 +01:00
GPXSee/src/map/ozimap.cpp

530 lines
12 KiB
C++
Raw Normal View History

#include <QPainter>
#include <QFileInfo>
#include <QMap>
#include <QDir>
2017-03-21 01:15:29 +01:00
#include <QBuffer>
2017-03-21 20:51:23 +01:00
#include <QImageReader>
#include <QPixmapCache>
#include <QRegularExpression>
2017-11-26 18:54:03 +01:00
#include "common/coordinates.h"
#include "common/rectc.h"
2018-03-08 02:24:10 +01:00
#include "tar.h"
#include "ozf.h"
2018-08-23 20:26:10 +02:00
#include "image.h"
2018-01-08 23:47:45 +01:00
#include "mapfile.h"
#include "gmifile.h"
2019-01-14 23:47:24 +01:00
#include "rectd.h"
#include "ozimap.h"
static QString tarFile(const QString &path)
{
QDir dir(path);
QFileInfoList files = dir.entryInfoList(QDir::Files);
for (int i = 0; i < files.size(); i++) {
const QFileInfo &fi = files.at(i);
if (fi.suffix().toLower() == "tar")
return fi.absoluteFilePath();
}
return QString();
}
QString OziMap::calibrationFile(const QStringList &files, const QString path,
CalibrationType &type)
{
for (int i = 0; i < files.size(); i++) {
QFileInfo fi(files.at(i));
QString suffix(fi.suffix().toLower());
if (path.endsWith(fi.path())) {
if (suffix == "map") {
type = MAP;
return files.at(i);
} else if (suffix == "gmi") {
type = GMI;
return files.at(i);
}
}
}
type = Unknown;
return QString();
}
OziMap::OziMap(const QString &fileName, CalibrationType type,
const Projection &proj, QObject *parent) : Map(fileName, parent), _img(0),
_tar(0), _ozf(0), _zoom(0), _mapRatio(1.0), _valid(false)
{
// TAR maps
if (type == Unknown) {
2018-03-08 02:24:10 +01:00
_tar = new Tar(fileName);
if (!_tar->open()) {
2023-12-31 13:20:04 +01:00
_errorString = _tar->errorString();
return;
2017-03-21 01:15:29 +01:00
}
QStringList files(_tar->files());
QString cf(calibrationFile(files, ".", type));
if (type == GMI) {
QByteArray ba(_tar->file(cf));
QBuffer buffer(&ba);
GmiFile gmi(buffer);
if (!gmi.isValid()) {
_errorString = gmi.errorString();
return;
} else {
_name = Util::file2name(fileName);
_map.size = gmi.size();
_map.path = gmi.image();
_calibrationPoints = gmi.calibrationPoints();
_projection = proj;
computeTransform();
}
} else if (type == MAP) {
QByteArray ba(_tar->file(cf));
QBuffer buffer(&ba);
MapFile mf(buffer);
if (!mf.isValid()) {
_errorString = mf.errorString();
return;
} else {
_name = mf.name();
_map.size = mf.size();
_map.path = mf.image();
_projection = mf.projection();
_transform = mf.transform();
}
2018-01-08 23:47:45 +01:00
} else {
_errorString = "No calibration file found";
return;
2018-01-08 23:47:45 +01:00
}
if (!setTileInfo(files))
return;
_tar->close();
// regular MAP or GMI maps
} else {
2018-03-21 19:09:37 +01:00
QFile file(fileName);
if (type == MAP) {
MapFile mf(file);
if (!mf.isValid()) {
_errorString = mf.errorString();
return;
} else {
_name = mf.name();
_map.size = mf.size();
_map.path = mf.image();
_projection = mf.projection();
_transform = mf.transform();
}
} else if (type == GMI) {
GmiFile gmi(file);
if (!gmi.isValid()) {
_errorString = gmi.errorString();
return;
} else {
_name = Util::file2name(fileName);
_map.size = gmi.size();
_map.path = gmi.image();
_calibrationPoints = gmi.calibrationPoints();
_projection = proj;
computeTransform();
}
2018-01-08 23:47:45 +01:00
}
QFileInfo fi(fileName);
QDir set(fi.absolutePath() + "/" + "set");
2017-03-21 01:15:29 +01:00
if (set.exists()) {
2018-03-08 19:08:39 +01:00
if (!setTileInfo(set.entryList(), set.absolutePath()))
2017-03-27 10:31:41 +02:00
return;
2017-03-21 01:15:29 +01:00
} else {
2018-03-08 19:08:39 +01:00
if (!setImageInfo(fi.absolutePath()))
2017-03-21 01:15:29 +01:00
return;
2017-03-20 10:05:07 +01:00
}
}
_valid = true;
}
OziMap::OziMap(const QString &dirName, Tar &tar, const Projection &proj,
QObject *parent) : Map(dirName, parent), _img(0), _tar(0), _ozf(0), _zoom(0),
_mapRatio(1.0), _valid(false)
2017-03-27 02:41:30 +02:00
{
CalibrationType type;
QString cf(calibrationFile(tar.files(), dirName, type));
if (type == MAP) {
QByteArray ba = tar.file(cf);
QBuffer buffer(&ba);
MapFile mf(buffer);
if (!mf.isValid()) {
_errorString = mf.errorString();
return;
}
_name = mf.name();
_map.size = mf.size();
_projection = mf.projection();
_transform = mf.transform();
} else if (type == GMI) {
QByteArray ba = tar.file(cf);
QBuffer buffer(&ba);
GmiFile gmi(buffer);
if (!gmi.isValid()) {
_errorString = gmi.errorString();
return;
}
_name = Util::file2name(cf);
_map.size = gmi.size();
_calibrationPoints = gmi.calibrationPoints();
_projection = proj;
computeTransform();
} else {
_errorString = "No calibration file found";
2017-03-27 10:31:41 +02:00
return;
2018-01-08 23:47:45 +01:00
}
QString tf(tarFile(dirName));
if (tf.isNull()) {
_errorString = "No map tar file found";
return;
}
_tar = new Tar(tf);
if (!_tar->open()) {
2023-11-26 10:51:37 +01:00
_errorString = _tar->fileName() + ": " + _tar->errorString();
return;
}
if (!setTileInfo(_tar->files())) {
_errorString = _tar->fileName() + ": " + _errorString;
return;
}
_tar->close();
2017-03-27 02:41:30 +02:00
_valid = true;
}
OziMap::~OziMap()
{
2018-01-08 23:47:45 +01:00
delete _img;
2018-03-08 02:24:10 +01:00
delete _tar;
delete _ozf;
}
bool OziMap::setImageInfo(const QString &path)
{
QFileInfo ii(_map.path);
if (ii.isRelative())
ii.setFile(path + "/" + _map.path);
if (!ii.exists()) {
int last = _map.path.lastIndexOf('\\');
if (last >= 0 && last < _map.path.length() - 1) {
QString fn(_map.path.mid(last + 1, _map.path.length() - last - 1));
ii.setFile(path + "/" + fn);
}
}
if (ii.exists())
_map.path = ii.absoluteFilePath();
else {
_errorString = QString("%1: No such image file").arg(_map.path);
return false;
}
if (OZF::isOZF(_map.path)) {
_ozf = new OZF(_map.path);
2023-11-26 10:51:37 +01:00
if (!_ozf->open()) {
_errorString = QString("%1: %2").arg(_map.path, _ozf->errorString());
return false;
}
_scale = _ozf->scale(_zoom);
_ozf->close();
} else {
2018-08-23 20:26:10 +02:00
QImageReader ir(_map.path);
if (!ir.canRead()) {
_errorString = QString("%1: Unsupported/invalid image file")
.arg(_map.path);
return false;
}
2018-08-23 20:26:10 +02:00
_map.size = ir.size();
}
return true;
}
bool OziMap::setTileInfo(const QStringList &tiles, const QString &path)
{
2023-12-31 13:57:43 +01:00
static const QRegularExpression rx("_[0-9]+_[0-9]+\\.");
2023-12-31 13:20:04 +01:00
if (!_map.size.isValid()) {
_errorString = "Missing total image size (IWH)";
return false;
}
for (int i = 0; i < tiles.size(); i++) {
2023-12-31 13:20:04 +01:00
const QString &tile = tiles.at(i);
if (tile.contains(rx)) {
2024-01-01 13:02:42 +01:00
QString pattern(QString(tile).replace(rx, "_%1_%2."));
if (_tar) {
2023-12-31 13:20:04 +01:00
QByteArray ba(_tar->file(tile));
QBuffer buffer(&ba);
2024-01-01 13:02:42 +01:00
_tile.path = pattern;
_tile.size = QImageReader(&buffer).size();
} else {
2024-01-01 13:02:42 +01:00
_tile.path = path + "/" + pattern;
2023-12-31 13:20:04 +01:00
_tile.size = QImageReader(path + "/" + tile).size();
}
2024-01-01 13:02:42 +01:00
if (_tile.size.isValid())
2024-01-01 10:58:23 +01:00
return true;
2024-01-01 13:02:42 +01:00
else
2024-01-01 10:58:23 +01:00
qWarning("%s: error reading tile image", qPrintable(tile));
}
}
_errorString = "Invalid/missing tile set";
return false;
}
void OziMap::load(const Projection &in, const Projection &out,
qreal deviceRatio, bool hidpi)
{
Q_UNUSED(out);
_mapRatio = hidpi ? deviceRatio : 1.0;
if (!_calibrationPoints.isEmpty()) {
_projection = in;
computeTransform();
}
if (_tar) {
Q_ASSERT(!_tar->isOpen());
2023-11-26 10:51:37 +01:00
if (!_tar->open()) {
qWarning("%s: %s", qPrintable(_tar->fileName()),
qPrintable(_tar->errorString()));
2017-03-27 02:41:30 +02:00
return;
2023-11-26 10:51:37 +01:00
}
2017-03-27 02:41:30 +02:00
}
if (_ozf) {
Q_ASSERT(!_ozf->isOpen());
2023-11-26 10:51:37 +01:00
if (!_ozf->open()) {
qWarning("%s: %s", qPrintable(_ozf->fileName()),
qPrintable(_ozf->errorString()));
return;
2023-11-26 10:51:37 +01:00
}
}
if (!_tile.isValid() && !_ozf) {
Q_ASSERT(!_img);
2018-08-23 20:26:10 +02:00
_img = new Image(_map.path);
2023-11-26 10:51:37 +01:00
_img->setDevicePixelRatio(_mapRatio);
}
}
void OziMap::unload()
{
2018-01-08 23:47:45 +01:00
delete _img;
_img = 0;
if (_tar && _tar->isOpen())
_tar->close();
if (_ozf && _ozf->isOpen())
_ozf->close();
}
void OziMap::drawTiled(QPainter *painter, const QRectF &rect) const
{
QSizeF ts(_tile.size.width() / _mapRatio, _tile.size.height() / _mapRatio);
2018-08-18 21:06:36 +02:00
QPointF tl(floor(rect.left() / ts.width()) * ts.width(),
floor(rect.top() / ts.height()) * ts.height());
2017-04-15 08:59:31 +02:00
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
2018-08-18 21:06:36 +02:00
for (int i = 0; i < ceil(s.width() / ts.width()); i++) {
for (int j = 0; j < ceil(s.height() / ts.height()); j++) {
int x = round(tl.x() * _mapRatio + i * _tile.size.width());
int y = round(tl.y() * _mapRatio + j * _tile.size.height());
2017-04-15 08:59:31 +02:00
2018-03-08 02:24:10 +01:00
QString tileName(_tile.path.arg(QString::number(x),
2017-04-15 08:59:31 +02:00
QString::number(y)));
QPixmap pixmap;
2018-03-08 02:24:10 +01:00
if (_tar) {
QString key = _tar->fileName() + "/" + tileName;
2017-04-14 22:39:33 +02:00
if (!QPixmapCache::find(key, &pixmap)) {
2018-03-08 02:24:10 +01:00
QByteArray ba = _tar->file(tileName);
2017-04-15 08:59:31 +02:00
pixmap = QPixmap::fromImage(QImage::fromData(ba));
2017-04-14 22:39:33 +02:00
if (!pixmap.isNull())
QPixmapCache::insert(key, pixmap);
}
2017-04-15 08:59:31 +02:00
} else
pixmap = QPixmap(tileName);
2018-05-22 22:40:15 +02:00
if (pixmap.isNull())
qWarning("%s: error loading tile image", qPrintable(tileName));
2018-08-18 21:06:36 +02:00
else {
pixmap.setDevicePixelRatio(_mapRatio);
2018-08-18 21:06:36 +02:00
QPointF tp(tl.x() + i * ts.width(), tl.y() + j * ts.height());
painter->drawPixmap(tp, pixmap);
}
2017-04-14 22:39:33 +02:00
}
2017-04-15 08:59:31 +02:00
}
}
void OziMap::drawOZF(QPainter *painter, const QRectF &rect) const
2017-04-15 08:59:31 +02:00
{
QSizeF ts(_ozf->tileSize().width() / _mapRatio, _ozf->tileSize().height()
/ _mapRatio);
2018-08-18 21:06:36 +02:00
QPointF tl(floor(rect.left() / ts.width()) * ts.width(),
floor(rect.top() / ts.height()) * ts.height());
2017-04-15 08:59:31 +02:00
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
2018-08-18 21:06:36 +02:00
for (int i = 0; i < ceil(s.width() / ts.width()); i++) {
for (int j = 0; j < ceil(s.height() / ts.height()); j++) {
int x = round(tl.x() * _mapRatio + i * _ozf->tileSize().width());
int y = round(tl.y() * _mapRatio + j * _ozf->tileSize().height());
2017-04-15 08:59:31 +02:00
QPixmap pixmap;
2023-12-31 13:20:04 +01:00
QString key(_ozf->fileName() + "/" + QString::number(_zoom) + "_"
+ QString::number(x) + "_" + QString::number(y));
2017-04-15 08:59:31 +02:00
if (!QPixmapCache::find(key, &pixmap)) {
2018-03-08 02:24:10 +01:00
pixmap = _ozf->tile(_zoom, x, y);
2017-04-15 08:59:31 +02:00
if (!pixmap.isNull())
QPixmapCache::insert(key, pixmap);
}
2018-05-22 22:40:15 +02:00
if (pixmap.isNull())
2017-04-15 08:59:31 +02:00
qWarning("%s: error loading tile image", qPrintable(key));
2018-08-18 21:06:36 +02:00
else {
pixmap.setDevicePixelRatio(_mapRatio);
2018-08-18 21:06:36 +02:00
QPointF tp(tl.x() + i * ts.width(), tl.y() + j * ts.height());
painter->drawPixmap(tp, pixmap);
}
2017-03-20 10:05:07 +01:00
}
}
}
2017-04-15 08:59:31 +02:00
2018-08-23 20:26:10 +02:00
void OziMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
2017-04-15 08:59:31 +02:00
{
2018-08-23 20:26:10 +02:00
Q_UNUSED(flags);
2018-04-28 16:07:32 +02:00
2018-03-08 02:24:10 +01:00
if (_ozf)
drawOZF(painter, rect);
2018-08-23 20:26:10 +02:00
else if (_img)
_img->draw(painter, rect, flags);
2018-03-08 02:24:10 +01:00
else if (_tile.isValid())
drawTiled(painter, rect);
2017-04-15 08:59:31 +02:00
}
2017-08-11 00:22:21 +02:00
QPointF OziMap::ll2xy(const Coordinates &c)
2017-08-11 00:22:21 +02:00
{
2018-04-15 17:32:25 +02:00
QPointF p(_transform.proj2img(_projection.ll2xy(c)));
2018-08-18 21:06:36 +02:00
return _ozf
? QPointF(p.x() * _scale.x(), p.y() * _scale.y()) / _mapRatio
: p / _mapRatio;
2017-08-11 00:22:21 +02:00
}
Coordinates OziMap::xy2ll(const QPointF &p)
2017-08-11 00:22:21 +02:00
{
2018-03-22 20:00:30 +01:00
return _ozf
? _projection.xy2ll(_transform.img2proj(QPointF(p.x() / _scale.x(),
p.y() / _scale.y()) * _mapRatio))
: _projection.xy2ll(_transform.img2proj(p * _mapRatio));
2017-08-11 00:22:21 +02:00
}
QRectF OziMap::bounds()
2017-08-11 00:22:21 +02:00
{
2018-03-22 20:00:30 +01:00
return _ozf
? QRectF(QPointF(0, 0), _ozf->size(_zoom) / _mapRatio)
: QRectF(QPointF(0, 0), _map.size / _mapRatio);
2017-08-11 00:22:21 +02:00
}
int OziMap::zoomFit(const QSize &size, const RectC &rect)
2017-08-11 00:22:21 +02:00
{
2018-03-22 20:00:30 +01:00
if (!_ozf)
return _zoom;
if (!rect.isValid())
rescale(0);
else {
2019-01-14 23:47:24 +01:00
RectD prect(rect, _projection);
QRectF sbr(_transform.proj2img(prect.topLeft()),
_transform.proj2img(prect.bottomRight()));
2018-03-22 20:00:30 +01:00
for (int i = 0; i < _ozf->zooms(); i++) {
rescale(i);
if (sbr.size().width() * _scale.x() <= size.width()
&& sbr.size().height() * _scale.y() <= size.height())
break;
2017-08-11 00:22:21 +02:00
}
}
return _zoom;
}
int OziMap::zoomIn()
2017-08-11 00:22:21 +02:00
{
2018-03-08 02:24:10 +01:00
if (_ozf)
2017-08-11 00:22:21 +02:00
rescale(qMax(_zoom - 1, 0));
return _zoom;
}
int OziMap::zoomOut()
2017-08-11 00:22:21 +02:00
{
2018-03-08 02:24:10 +01:00
if (_ozf)
rescale(qMin(_zoom + 1, _ozf->zooms() - 1));
2017-08-11 00:22:21 +02:00
return _zoom;
}
void OziMap::rescale(int zoom)
2017-08-11 00:22:21 +02:00
{
_zoom = zoom;
2018-03-08 02:24:10 +01:00
_scale = _ozf->scale(zoom);
2017-08-11 00:22:21 +02:00
}
2018-08-23 20:26:10 +02:00
void OziMap::computeTransform()
{
QList<ReferencePoint> rp;
for (int i = 0; i < _calibrationPoints.size(); i++)
rp.append(_calibrationPoints.at(i).rp(_projection));
_transform = Transform(rp);
}
Map *OziMap::createTAR(const QString &path, const Projection &proj, bool *isDir)
2022-06-10 08:09:07 +02:00
{
if (isDir)
*isDir = false;
2022-06-10 08:09:07 +02:00
return new OziMap(path, Unknown, proj);
2022-06-10 08:09:07 +02:00
}
Map *OziMap::createMAP(const QString &path, const Projection &proj, bool *isDir)
{
if (isDir)
*isDir = false;
return new OziMap(path, MAP, proj);
}
Map *OziMap::createGMI(const QString &path, const Projection &proj, bool *isDir)
{
if (isDir)
*isDir = false;
return new OziMap(path, GMI, proj);
}