1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-12-04 08:19:10 +01:00
GPXSee/src/map/worldfilemap.cpp

132 lines
2.6 KiB
C++
Raw Normal View History

2021-06-17 21:58:25 +02:00
#include <QFileInfo>
#include <QDir>
#include <QPainter>
#include <QImageReader>
#include "image.h"
#include "gcs.h"
#include "prjfile.h"
#include "wldfile.h"
#include "worldfilemap.h"
WorldFileMap::WorldFileMap(const QString &fileName, QObject *parent)
: Map(fileName, parent), _img(0), _mapRatio(1.0), _hasPRJ(false), _valid(false)
2021-06-17 21:58:25 +02:00
{
QFileInfo fi(fileName);
QDir dir(fi.absoluteDir());
QString basename(fi.completeBaseName());
2021-06-17 21:58:25 +02:00
2021-06-17 22:13:31 +02:00
// Get the transformation from the WLD file
2021-06-17 21:58:25 +02:00
WLDFile wld(fileName);
if (wld.transform().isValid())
_transform = wld.transform();
else {
_errorString = wld.errorString();
return;
}
2021-06-17 22:13:31 +02:00
// Get the projection from the corresponding PRJ file (if any)
2021-06-17 21:58:25 +02:00
QString prjFile(basename + ".prj");
if (dir.exists(prjFile)) {
PRJFile prj(dir.filePath(prjFile));
if (prj.projection().isValid()) {
_projection = prj.projection();
_hasPRJ = true;
} else {
_errorString = prjFile + ": " + prj.errorString();
return;
}
}
// Find the corresponding image file
QList<QByteArray> formats(QImageReader::supportedImageFormats());
QString imgFile;
for (int i = 0; i < formats.size(); i++) {
imgFile = basename + "." + formats.at(i);
if (dir.exists(imgFile)) {
_imgFile = dir.filePath(imgFile);
break;
}
}
if (_imgFile.isNull()) {
_errorString = "image file not found";
return;
}
QImageReader ir(_imgFile);
if (!ir.canRead()) {
_errorString = imgFile + ": unsupported/invalid image file";
return;
}
_size = ir.size();
_valid = true;
}
WorldFileMap::~WorldFileMap()
{
delete _img;
}
QPointF WorldFileMap::ll2xy(const Coordinates &c)
{
return QPointF(_transform.proj2img(_projection.ll2xy(c))) / _mapRatio;
2021-06-17 21:58:25 +02:00
}
Coordinates WorldFileMap::xy2ll(const QPointF &p)
{
return _projection.xy2ll(_transform.img2proj(p * _mapRatio));
}
RectC WorldFileMap::llBounds(const Projection &proj)
{
if (_projection.isNull())
_projection = proj;
return Map::llBounds(proj);
2021-06-17 21:58:25 +02:00
}
QRectF WorldFileMap::bounds()
{
return QRectF(QPointF(0, 0), _size / _mapRatio);
2021-06-17 21:58:25 +02:00
}
void WorldFileMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
if (_img)
_img->draw(painter, rect, flags);
}
void WorldFileMap::load(const Projection &in, const Projection &out,
qreal deviceRatio, bool hidpi)
2021-06-17 21:58:25 +02:00
{
Q_UNUSED(out);
2021-06-17 21:58:25 +02:00
if (!_hasPRJ)
_projection = in;
2021-06-17 21:58:25 +02:00
_mapRatio = hidpi ? deviceRatio : 1.0;
Q_ASSERT(!_img);
_img = new Image(_imgFile);
if (_img)
_img->setDevicePixelRatio(_mapRatio);
2021-06-17 21:58:25 +02:00
}
void WorldFileMap::unload()
{
delete _img;
_img = 0;
}
Map *WorldFileMap::create(const QString &path, bool *isDir)
{
if (isDir)
*isDir = false;
return new WorldFileMap(path);
}