2017-04-17 18:03:04 +02:00
|
|
|
#include <QFile>
|
2018-01-08 23:47:45 +01:00
|
|
|
#include <QDebug>
|
2018-01-20 20:13:56 +01:00
|
|
|
#include "common/wgs84.h"
|
2023-04-15 03:18:52 +02:00
|
|
|
#include "common/csv.h"
|
2017-04-03 20:29:35 +02:00
|
|
|
#include "ellipsoid.h"
|
|
|
|
|
2018-06-28 22:12:16 +02:00
|
|
|
QMap<int, Ellipsoid> Ellipsoid::_ellipsoids = defaults();
|
2017-04-03 20:29:35 +02:00
|
|
|
|
2018-06-28 22:12:16 +02:00
|
|
|
const Ellipsoid &Ellipsoid::WGS84()
|
|
|
|
{
|
|
|
|
static Ellipsoid e(WGS84_RADIUS, WGS84_FLATTENING);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMap<int, Ellipsoid> Ellipsoid::defaults()
|
2018-01-20 20:13:56 +01:00
|
|
|
{
|
|
|
|
QMap<int, Ellipsoid> map;
|
2018-06-28 22:12:16 +02:00
|
|
|
map.insert(7030, WGS84());
|
2018-01-20 20:13:56 +01:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2021-06-17 21:58:25 +02:00
|
|
|
const Ellipsoid &Ellipsoid::ellipsoid(int id)
|
2017-04-03 20:29:35 +02:00
|
|
|
{
|
2018-03-29 00:29:08 +02:00
|
|
|
QMap<int, Ellipsoid>::const_iterator it(_ellipsoids.find(id));
|
2021-06-17 21:58:25 +02:00
|
|
|
static const Ellipsoid null;
|
2017-04-17 18:03:04 +02:00
|
|
|
|
2018-03-29 00:29:08 +02:00
|
|
|
if (it == _ellipsoids.constEnd())
|
2021-06-17 21:58:25 +02:00
|
|
|
return null;
|
2018-01-08 23:47:45 +01:00
|
|
|
else
|
2021-06-17 21:58:25 +02:00
|
|
|
return it.value();
|
2018-01-09 23:19:35 +01:00
|
|
|
}
|
|
|
|
|
2023-04-15 03:18:52 +02:00
|
|
|
bool Ellipsoid::loadList(const QString &path)
|
2017-04-03 20:29:35 +02:00
|
|
|
{
|
2017-04-17 18:03:04 +02:00
|
|
|
QFile file(path);
|
2023-04-15 03:18:52 +02:00
|
|
|
CSV csv(&file);
|
|
|
|
QByteArrayList entry;
|
2017-04-17 18:03:04 +02:00
|
|
|
bool res;
|
2018-01-08 23:47:45 +01:00
|
|
|
|
2017-04-17 18:03:04 +02:00
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
2018-01-20 20:13:56 +01:00
|
|
|
qWarning("Error opening ellipsoids file: %s: %s", qPrintable(path),
|
|
|
|
qPrintable(file.errorString()));
|
2023-04-15 03:18:52 +02:00
|
|
|
return false;
|
2017-04-17 18:03:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-15 03:18:52 +02:00
|
|
|
while (!csv.atEnd()) {
|
|
|
|
if (!csv.readEntry(entry) || entry.size() < 4) {
|
|
|
|
qWarning("%s:%d: Parse error", qPrintable(path), csv.line());
|
|
|
|
return false;
|
2017-04-17 18:03:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-15 03:18:52 +02:00
|
|
|
int id = entry.at(1).toInt(&res);
|
2017-04-17 18:03:04 +02:00
|
|
|
if (!res) {
|
2023-04-15 03:18:52 +02:00
|
|
|
qWarning("%s: %d: Invalid ellipsoid code", qPrintable(path),
|
|
|
|
csv.line());
|
2018-01-20 20:13:56 +01:00
|
|
|
continue;
|
2017-04-17 18:03:04 +02:00
|
|
|
}
|
2023-04-15 03:18:52 +02:00
|
|
|
double radius = entry.at(2).toDouble(&res);
|
2017-04-17 18:03:04 +02:00
|
|
|
if (!res) {
|
2023-04-15 03:18:52 +02:00
|
|
|
qWarning("%s: %d: Invalid radius", qPrintable(path), csv.line());
|
2018-01-20 20:13:56 +01:00
|
|
|
continue;
|
2017-04-17 18:03:04 +02:00
|
|
|
}
|
2023-04-15 03:18:52 +02:00
|
|
|
double flattening = entry.at(3).toDouble(&res);
|
2017-04-17 18:03:04 +02:00
|
|
|
if (!res) {
|
2023-04-15 03:18:52 +02:00
|
|
|
qWarning("%s: %d: Invalid flattening", qPrintable(path), csv.line());
|
2018-01-20 20:13:56 +01:00
|
|
|
continue;
|
2017-04-17 18:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ellipsoid e(radius, 1.0/flattening);
|
|
|
|
_ellipsoids.insert(id, e);
|
|
|
|
}
|
2023-04-15 03:18:52 +02:00
|
|
|
|
|
|
|
return true;
|
2017-04-03 20:29:35 +02:00
|
|
|
}
|
2018-01-08 23:47:45 +01:00
|
|
|
|
2018-05-17 22:41:56 +02:00
|
|
|
Ellipsoid::Ellipsoid(double radius, double flattening)
|
|
|
|
: _radius(radius), _flattening(flattening)
|
|
|
|
{
|
|
|
|
_es = 2.0 * flattening - flattening * flattening;
|
2018-05-18 22:10:43 +02:00
|
|
|
_e2s = (1.0 / (1.0 - _es)) - 1.0;
|
2018-05-17 22:41:56 +02:00
|
|
|
_b = radius * (1.0 - flattening);
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:03:18 +01:00
|
|
|
#ifndef QT_NO_DEBUG
|
2018-01-08 23:47:45 +01:00
|
|
|
QDebug operator<<(QDebug dbg, const Ellipsoid &ellipsoid)
|
|
|
|
{
|
|
|
|
dbg.nospace() << "Ellipsoid(" << ellipsoid.radius() << ", "
|
|
|
|
<< 1.0 / ellipsoid.flattening() << ")";
|
2018-01-21 11:19:46 +01:00
|
|
|
return dbg.space();
|
2018-01-08 23:47:45 +01:00
|
|
|
}
|
2018-02-13 23:03:18 +01:00
|
|
|
#endif // QT_NO_DEBUG
|