2023-09-14 19:02:19 +02:00
|
|
|
#include <QIODevice>
|
2023-09-13 20:02:24 +02:00
|
|
|
#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();
|
|
|
|
}
|
|
|
|
|
2023-09-14 18:36:03 +02:00
|
|
|
bool GmiFile::parse(QIODevice &device)
|
2023-09-13 20:02:24 +02:00
|
|
|
{
|
|
|
|
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";
|
2023-09-13 20:02:24 +02:00
|
|
|
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";
|
2023-09-13 20:02:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_size = QSize(width, height);
|
|
|
|
} else {
|
|
|
|
CalibrationPoint cp(calibrationPoint(line));
|
|
|
|
if (cp.isValid())
|
2023-09-14 18:36:03 +02:00
|
|
|
_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;
|
|
|
|
}
|
2023-09-13 20:02:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ln++;
|
|
|
|
}
|
|
|
|
|
2024-01-02 19:06:10 +01:00
|
|
|
if (ln < 6) {
|
|
|
|
_errorString = "Unexpected EOF";
|
|
|
|
return false;
|
|
|
|
}
|
2023-09-13 20:02:24 +02:00
|
|
|
|
2024-01-02 19:06:10 +01:00
|
|
|
return true;
|
2023-09-13 20:02:24 +02:00
|
|
|
}
|
|
|
|
|
2024-01-02 19:06:10 +01:00
|
|
|
GmiFile::GmiFile(QIODevice &file) : _valid(false)
|
2023-09-13 20:02:24 +02:00
|
|
|
{
|
2024-01-02 19:06:10 +01:00
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
_errorString = file.errorString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-14 18:36:03 +02:00
|
|
|
_valid = parse(file);
|
2024-01-02 19:06:10 +01:00
|
|
|
|
|
|
|
file.close();
|
2023-09-13 20:02:24 +02:00
|
|
|
}
|