2018-01-09 09:35:56 +01:00
|
|
|
#include <QFile>
|
2023-04-15 03:18:52 +02:00
|
|
|
#include "common/csv.h"
|
2018-01-20 23:51:39 +01:00
|
|
|
#include "angularunits.h"
|
2018-01-08 23:47:45 +01:00
|
|
|
#include "pcs.h"
|
|
|
|
|
2023-04-13 08:39:33 +02:00
|
|
|
QMap<int, PCS::Entry> PCS::_pcss = defaults();
|
2018-01-08 23:47:45 +01:00
|
|
|
|
2023-04-13 08:39:33 +02:00
|
|
|
QMap<int, PCS::Entry> PCS::defaults()
|
2018-06-28 22:12:16 +02:00
|
|
|
{
|
2023-04-13 08:39:33 +02:00
|
|
|
QMap<int, Entry> map;
|
|
|
|
map.insert(3857, Entry("WGS 84 / Pseudo-Mercator", 4326, 3856));
|
|
|
|
return map;
|
2018-06-28 22:12:16 +02:00
|
|
|
}
|
2018-01-08 23:47:45 +01:00
|
|
|
|
2021-07-01 08:54:48 +02:00
|
|
|
PCS PCS::pcs(int id)
|
2018-01-08 23:47:45 +01:00
|
|
|
{
|
2023-04-13 08:39:33 +02:00
|
|
|
QMap<int, Entry>::const_iterator it = _pcss.find(id);
|
2018-01-20 20:13:56 +01:00
|
|
|
|
2023-04-13 08:39:33 +02:00
|
|
|
if (it == _pcss.constEnd())
|
|
|
|
return PCS();
|
|
|
|
else {
|
|
|
|
const Entry &e = it.value();
|
|
|
|
return PCS(GCS::gcs(e.gcs()), Conversion::conversion(e.proj()));
|
|
|
|
}
|
2018-01-09 23:19:35 +01:00
|
|
|
}
|
|
|
|
|
2023-04-15 03:18:52 +02:00
|
|
|
bool PCS::loadList(const QString &path)
|
2018-01-08 23:47:45 +01:00
|
|
|
{
|
|
|
|
QFile file(path);
|
2023-04-15 03:18:52 +02:00
|
|
|
CSV csv(&file);
|
|
|
|
QByteArrayList entry;
|
2018-01-20 20:13:56 +01:00
|
|
|
bool res;
|
2018-01-08 23:47:45 +01:00
|
|
|
|
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
2018-01-20 20:13:56 +01:00
|
|
|
qWarning("Error opening PCS file: %s: %s", qPrintable(path),
|
|
|
|
qPrintable(file.errorString()));
|
2023-04-15 03:18:52 +02:00
|
|
|
return false;
|
2018-01-08 23:47:45 +01: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;
|
2018-01-08 23:47:45 +01:00
|
|
|
}
|
2023-04-15 03:18:52 +02:00
|
|
|
QString name(entry.at(0));
|
|
|
|
int id = entry.at(1).toInt(&res);
|
2018-01-20 20:13:56 +01:00
|
|
|
if (!res) {
|
2023-04-15 03:18:52 +02:00
|
|
|
qWarning("%s:%d: Invalid PCS code", qPrintable(path), csv.line());
|
2018-01-20 20:13:56 +01:00
|
|
|
continue;
|
2018-01-08 23:47:45 +01:00
|
|
|
}
|
2023-04-15 03:18:52 +02:00
|
|
|
int gcs = entry.at(2).toInt(&res);
|
2018-01-20 20:13:56 +01:00
|
|
|
if (!res) {
|
2023-04-15 03:18:52 +02:00
|
|
|
qWarning("%s:%d: Invalid GCS code", qPrintable(path), csv.line());
|
2018-01-20 20:13:56 +01:00
|
|
|
continue;
|
2018-01-08 23:47:45 +01:00
|
|
|
}
|
2023-04-15 03:18:52 +02:00
|
|
|
int proj = entry.at(3).toInt(&res);
|
2018-01-20 20:13:56 +01:00
|
|
|
if (!res) {
|
2023-04-15 03:18:52 +02:00
|
|
|
qWarning("%s:%d: Invalid projection code", qPrintable(path),
|
|
|
|
csv.line());
|
2018-01-20 20:13:56 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-01-20 23:51:39 +01:00
|
|
|
|
2023-04-13 08:39:33 +02:00
|
|
|
if (GCS::gcs(gcs).isNull()) {
|
2023-04-15 03:18:52 +02:00
|
|
|
qWarning("%s:%d: Unknown GCS code", qPrintable(path), csv.line());
|
2018-01-20 20:13:56 +01:00
|
|
|
continue;
|
2018-01-08 23:47:45 +01:00
|
|
|
}
|
|
|
|
|
2023-04-13 08:39:33 +02:00
|
|
|
_pcss.insert(id, Entry(name, gcs, proj));
|
2018-01-08 23:47:45 +01:00
|
|
|
}
|
2023-04-15 03:18:52 +02:00
|
|
|
|
|
|
|
return true;
|
2018-01-08 23:47:45 +01:00
|
|
|
}
|
|
|
|
|
2019-08-01 08:36:58 +02:00
|
|
|
QList<KV<int, QString> > PCS::list()
|
2019-05-14 23:01:24 +02:00
|
|
|
{
|
2019-08-01 08:36:58 +02:00
|
|
|
QList<KV<int, QString> > list;
|
2019-05-14 23:01:24 +02:00
|
|
|
|
2023-04-13 08:39:33 +02:00
|
|
|
for (QMap<int, Entry>::const_iterator it = _pcss.constBegin();
|
|
|
|
it != _pcss.constEnd(); ++it)
|
|
|
|
list.append(KV<int, QString>(it.key(), it.value().name()));
|
2019-05-14 23:01:24 +02:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
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 PCS &pcs)
|
|
|
|
{
|
2023-04-13 08:39:33 +02:00
|
|
|
dbg.nospace() << "PCS(" << pcs.gcs() << ", " << pcs.conversion() << ")";
|
2018-01-21 11:17:32 +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
|