1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-31 17:15:14 +01:00
GPXSee/src/map/gmifile.cpp

85 lines
1.7 KiB
C++
Raw Normal View History

2023-09-14 19:02:19 +02:00
#include <QIODevice>
#include "gmifile.h"
static CalibrationPoint calibrationPoint(const QByteArray line)
{
bool xOk, yOk, lonOk, latOk;
QList<QByteArray> list = line.split(';');
if (list.size() != 4)
return CalibrationPoint();
int x = list.at(0).toInt(&xOk);
int y = list.at(1).toInt(&yOk);
double lon = list.at(2).toDouble(&lonOk);
double lat = list.at(3).toDouble(&latOk);
return (xOk && yOk && latOk && lonOk)
? CalibrationPoint(PointD(x, y), Coordinates(lon, lat))
: CalibrationPoint();
}
bool GmiFile::parse(QIODevice &device)
{
int ln = 1;
int width, height;
bool ok;
while (!device.atEnd()) {
QByteArray line = device.readLine(4096);
if (ln == 1) {
if (!line.startsWith("Map Calibration data file")) {
_errorString = "Not a GMI file";
return false;
}
} else if (ln == 2)
_image = line.trimmed();
else if (ln == 3) {
width = line.toInt(&ok);
2023-12-29 18:01:28 +01:00
if (!ok || width <= 0) {
2024-01-02 19:06:10 +01:00
_errorString = line + ": invalid image width";
return false;
}
} else if (ln == 4) {
height = line.toInt(&ok);
2023-12-29 18:01:28 +01:00
if (!ok || height <= 0) {
2024-01-02 19:06:10 +01:00
_errorString = line + ": invalid image height";
return false;
}
_size = QSize(width, height);
} else {
CalibrationPoint cp(calibrationPoint(line));
if (cp.isValid())
_points.append(cp);
2024-01-02 19:06:10 +01:00
else {
if (_points.size() < 2) {
_errorString = line + ": invalid calibration point";
return false;
} else
break;
}
}
ln++;
}
2024-01-02 19:06:10 +01:00
if (ln < 6) {
_errorString = "Unexpected EOF";
return false;
}
2024-01-02 19:06:10 +01:00
return true;
}
2024-01-02 19:06:10 +01:00
GmiFile::GmiFile(QIODevice &file) : _valid(false)
{
2024-01-02 19:06:10 +01:00
if (!file.open(QIODevice::ReadOnly)) {
_errorString = file.errorString();
return;
}
_valid = parse(file);
2024-01-02 19:06:10 +01:00
file.close();
}