1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-27 21:24:47 +01:00

Allow advanced CSV formating in CRS files

This commit is contained in:
Martin Tůma 2023-04-15 03:18:52 +02:00
parent f7865556ae
commit 2824751615
14 changed files with 149 additions and 150 deletions

View File

@ -45,6 +45,7 @@ HEADERS += src/common/config.h \
src/common/downloader.h \ src/common/downloader.h \
src/common/polygon.h \ src/common/polygon.h \
src/common/color.h \ src/common/color.h \
src/common/csv.h \
src/GUI/authenticationwidget.h \ src/GUI/authenticationwidget.h \
src/GUI/axislabelitem.h \ src/GUI/axislabelitem.h \
src/GUI/dirselectwidget.h \ src/GUI/dirselectwidget.h \
@ -253,7 +254,6 @@ HEADERS += src/common/config.h \
src/data/demloader.h \ src/data/demloader.h \
src/data/area.h \ src/data/area.h \
src/data/exifparser.h \ src/data/exifparser.h \
src/data/csv.h \
src/data/cupparser.h \ src/data/cupparser.h \
src/data/gpiparser.h \ src/data/gpiparser.h \
src/data/address.h \ src/data/address.h \
@ -274,6 +274,7 @@ SOURCES += src/main.cpp \
src/common/programpaths.cpp \ src/common/programpaths.cpp \
src/common/tifffile.cpp \ src/common/tifffile.cpp \
src/common/downloader.cpp \ src/common/downloader.cpp \
src/common/csv.cpp \
src/GUI/authenticationwidget.cpp \ src/GUI/authenticationwidget.cpp \
src/GUI/axislabelitem.cpp \ src/GUI/axislabelitem.cpp \
src/GUI/dirselectwidget.cpp \ src/GUI/dirselectwidget.cpp \
@ -448,7 +449,6 @@ SOURCES += src/main.cpp \
src/data/dem.cpp \ src/data/dem.cpp \
src/data/demloader.cpp \ src/data/demloader.cpp \
src/data/exifparser.cpp \ src/data/exifparser.cpp \
src/data/csv.cpp \
src/data/cupparser.cpp \ src/data/cupparser.cpp \
src/data/gpiparser.cpp \ src/data/gpiparser.cpp \
src/data/smlparser.cpp \ src/data/smlparser.cpp \

View File

@ -1,4 +1,4 @@
#include <QStringList> #include <QByteArrayList>
#include "csv.h" #include "csv.h"
/* /*
@ -7,12 +7,14 @@ RFC 4180 parser with the following enchancements:
- allows LF line ends in addition to CRLF line ends - allows LF line ends in addition to CRLF line ends
*/ */
bool CSV::readEntry(QStringList &list) bool CSV::readEntry(QByteArrayList &list)
{ {
int state = 0; int state = 0;
char c; char c;
QByteArray field; QByteArray field;
list.clear();
while (_device->getChar(&c)) { while (_device->getChar(&c)) {
switch (state) { switch (state) {
case 0: case 0:

View File

@ -9,7 +9,7 @@ public:
CSV(QIODevice *device, char delimiter = ',') CSV(QIODevice *device, char delimiter = ',')
: _device(device), _delimiter(delimiter), _line(1) {} : _device(device), _delimiter(delimiter), _line(1) {}
bool readEntry(QStringList &list); bool readEntry(QByteArrayList &list);
bool atEnd() const {return _device->atEnd();} bool atEnd() const {return _device->atEnd();}
int line() const {return _line;} int line() const {return _line;}

View File

@ -1,5 +1,5 @@
#include <QStringList> #include <QByteArrayList>
#include "csv.h" #include "common/csv.h"
#include "csvparser.h" #include "csvparser.h"
bool CSVParser::parse(QFile *file, QList<TrackData> &tracks, bool CSVParser::parse(QFile *file, QList<TrackData> &tracks,
@ -10,10 +10,9 @@ bool CSVParser::parse(QFile *file, QList<TrackData> &tracks,
Q_UNUSED(routes); Q_UNUSED(routes);
Q_UNUSED(polygons); Q_UNUSED(polygons);
CSV csv(file); CSV csv(file);
QStringList entry; QByteArrayList entry;
bool ok; bool ok;
while (!csv.atEnd()) { while (!csv.atEnd()) {
if (!csv.readEntry(entry) || entry.size() < 3) { if (!csv.readEntry(entry) || entry.size() < 3) {
_errorString = "Parse error"; _errorString = "Parse error";
@ -21,26 +20,24 @@ bool CSVParser::parse(QFile *file, QList<TrackData> &tracks,
return false; return false;
} }
double lon = entry.at(0).trimmed().toDouble(&ok); double lon = entry.at(0).toDouble(&ok);
if (!ok || (lon < -180.0 || lon > 180.0)) { if (!ok || (lon < -180.0 || lon > 180.0)) {
_errorString = "Invalid longitude"; _errorString = "Invalid longitude";
_errorLine = csv.line(); _errorLine = csv.line();
return false; return false;
} }
double lat = entry.at(1).trimmed().toDouble(&ok); double lat = entry.at(1).toDouble(&ok);
if (!ok || (lat < -90.0 || lat > 90.0)) { if (!ok || (lat < -90.0 || lat > 90.0)) {
_errorString = "Invalid latitude"; _errorString = "Invalid latitude";
_errorLine = csv.line(); _errorLine = csv.line();
return false; return false;
} }
Waypoint wp(Coordinates(lon, lat)); Waypoint wp(Coordinates(lon, lat));
wp.setName(entry.at(2).trimmed()); wp.setName(entry.at(2));
if (entry.size() > 3) if (entry.size() > 3)
wp.setDescription(entry.at(3).trimmed()); wp.setDescription(entry.at(3));
waypoints.append(wp); waypoints.append(wp);
entry.clear();
} }
return true; return true;

View File

@ -1,6 +1,6 @@
#include <cmath> #include <cmath>
#include <QStringList> #include <QStringList>
#include "csv.h" #include "common/csv.h"
#include "cupparser.h" #include "cupparser.h"
@ -58,7 +58,8 @@ static double elevation(const QString &str)
} }
bool CUPParser::waypoint(const QStringList &entry, QVector<Waypoint> &waypoints) bool CUPParser::waypoint(const QByteArrayList &entry,
QVector<Waypoint> &waypoints)
{ {
if (entry.size() < 11) { if (entry.size() < 11) {
_errorString = "Invalid number of fields"; _errorString = "Invalid number of fields";
@ -85,7 +86,7 @@ bool CUPParser::waypoint(const QStringList &entry, QVector<Waypoint> &waypoints)
return true; return true;
} }
bool CUPParser::task(const QStringList &entry, bool CUPParser::task(const QByteArrayList &entry,
const QVector<Waypoint> &waypoints, QList<RouteData> &routes) const QVector<Waypoint> &waypoints, QList<RouteData> &routes)
{ {
if (entry.size() < 3) { if (entry.size() < 3) {
@ -125,10 +126,9 @@ bool CUPParser::parse(QFile *file, QList<TrackData> &tracks,
Q_UNUSED(tracks); Q_UNUSED(tracks);
Q_UNUSED(polygons); Q_UNUSED(polygons);
CSV csv(file); CSV csv(file);
QStringList entry; QByteArrayList entry;
SegmentType segment = Header; SegmentType segment = Header;
while (!csv.atEnd()) { while (!csv.atEnd()) {
if (!csv.readEntry(entry)) { if (!csv.readEntry(entry)) {
_errorString = "CSV parse error"; _errorString = "CSV parse error";
@ -159,7 +159,6 @@ bool CUPParser::parse(QFile *file, QList<TrackData> &tracks,
return false; return false;
} }
entry.clear();
_errorLine = csv.line(); _errorLine = csv.line();
} }

View File

@ -14,8 +14,8 @@ public:
int errorLine() const {return _errorLine;} int errorLine() const {return _errorLine;}
private: private:
bool waypoint(const QStringList &entry, QVector<Waypoint> &waypoints); bool waypoint(const QByteArrayList &entry, QVector<Waypoint> &waypoints);
bool task(const QStringList &entry, const QVector<Waypoint> &waypoints, bool task(const QByteArrayList &entry, const QVector<Waypoint> &waypoints,
QList<RouteData> &routes); QList<RouteData> &routes);
QString _errorString; QString _errorString;

View File

@ -1,4 +1,5 @@
#include <QFile> #include <QFile>
#include "common/csv.h"
#include "conversion.h" #include "conversion.h"
static bool parameter(int key, double val, int units, Projection::Setup &setup) static bool parameter(int key, double val, int units, Projection::Setup &setup)
@ -64,19 +65,18 @@ static bool parameter(int key, double val, int units, Projection::Setup &setup)
} }
} }
static int projectionSetup(const QList<QByteArray> &list, static int projectionSetup(const QByteArrayList &list, Projection::Setup &setup)
Projection::Setup &setup)
{ {
bool r1, r2, r3; bool r1, r2, r3;
for (int i = 5; i < 26; i += 3) { for (int i = 5; i < 26; i += 3) {
QString ks = list[i].trimmed(); const QByteArray &ks = list.at(i);
if (ks.isEmpty()) if (ks.isEmpty())
break; break;
int key = ks.toInt(&r1); int key = ks.toInt(&r1);
double val = list[i+1].trimmed().toDouble(&r2); double val = list.at(i+1).toDouble(&r2);
int un = list[i+2].trimmed().toInt(&r3); int un = list.at(i+2).toInt(&r3);
if (!r1 || !r2 || !r3) if (!r1 || !r2 || !r3)
return (i - 5)/3 + 1; return (i - 5)/3 + 1;
@ -109,77 +109,79 @@ Conversion Conversion::conversion(int id)
} }
} }
void Conversion::loadList(const QString &path) bool Conversion::loadList(const QString &path)
{ {
QFile file(path); QFile file(path);
CSV csv(&file);
QByteArrayList entry;
bool res; bool res;
int ln = 0, pn;
if (!file.open(QFile::ReadOnly)) { if (!file.open(QFile::ReadOnly)) {
qWarning("Error opening projections file: %s: %s", qPrintable(path), qWarning("Error opening projections file: %s: %s", qPrintable(path),
qPrintable(file.errorString())); qPrintable(file.errorString()));
return; return false;
} }
while (!file.atEnd()) { while (!csv.atEnd()) {
ln++; if (!csv.readEntry(entry) || entry.size() < 26) {
qWarning("%s:%d: Parse error", qPrintable(path), csv.line());
QByteArray line = file.readLine(4096); return false;
QList<QByteArray> list = line.split(',');
if (list.size() != 26) {
qWarning("%s:%d: Format error", qPrintable(path), ln);
continue;
} }
QString name(list.at(0).trimmed()); QString name(entry.at(0));
int proj = list.at(1).trimmed().toInt(&res); int proj = entry.at(1).toInt(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid projection code", qPrintable(path), ln); qWarning("%s:%d: Invalid projection code", qPrintable(path),
csv.line());
continue; continue;
} }
int units = list.at(2).trimmed().toInt(&res); int units = entry.at(2).toInt(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid linear units code", qPrintable(path), ln); qWarning("%s:%d: Invalid linear units code", qPrintable(path),
csv.line());
continue; continue;
} }
int transform = list.at(3).trimmed().toInt(&res); int transform = entry.at(3).toInt(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid coordinate transformation code", qWarning("%s:%d: Invalid coordinate transformation code",
qPrintable(path), ln); qPrintable(path), csv.line());
continue; continue;
} }
int cs = list.at(4).trimmed().toInt(&res); int cs = entry.at(4).toInt(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid coordinate system code", qWarning("%s:%d: Invalid coordinate system code",
qPrintable(path), ln); qPrintable(path), csv.line());
continue; continue;
} }
if (!LinearUnits(units).isValid()) { if (!LinearUnits(units).isValid()) {
qWarning("%s:%d: Unknown linear units code", qPrintable(path), ln); qWarning("%s:%d: Unknown linear units code", qPrintable(path),
csv.line());
continue; continue;
} }
if (!Projection::Method(transform).isValid()) { if (!Projection::Method(transform).isValid()) {
qWarning("%s:%d: Unknown coordinate transformation code", qWarning("%s:%d: Unknown coordinate transformation code",
qPrintable(path), ln); qPrintable(path), csv.line());
continue; continue;
} }
if (!CoordinateSystem(cs).isValid()) { if (!CoordinateSystem(cs).isValid()) {
qWarning("%s:%d: Unknown coordinate system code", qPrintable(path), qWarning("%s:%d: Unknown coordinate system code", qPrintable(path),
ln); csv.line());
continue; continue;
} }
Projection::Setup setup; Projection::Setup setup;
if ((pn = projectionSetup(list, setup))) { int pn = projectionSetup(entry, setup);
if (pn) {
qWarning("%s: %d: Invalid projection parameter #%d", qWarning("%s: %d: Invalid projection parameter #%d",
qPrintable(path), ln, pn); qPrintable(path), csv.line(), pn);
continue; continue;
} }
_conversions.insert(proj, Entry(name, transform, setup, units, cs)); _conversions.insert(proj, Entry(name, transform, setup, units, cs));
} }
return true;
} }
QList<KV<int, QString> > Conversion::list() QList<KV<int, QString> > Conversion::list()

View File

@ -27,7 +27,7 @@ public:
return (_units.isValid() && _method.isValid()); return (_units.isValid() && _method.isValid());
} }
static void loadList(const QString &path); static bool loadList(const QString &path);
static Conversion conversion(int id); static Conversion conversion(int id);
static QList<KV<int, QString> > list(); static QList<KV<int, QString> > list();

View File

@ -1,6 +1,7 @@
#include <QFile> #include <QFile>
#include <QDebug> #include <QDebug>
#include "common/wgs84.h" #include "common/wgs84.h"
#include "common/csv.h"
#include "ellipsoid.h" #include "ellipsoid.h"
QMap<int, Ellipsoid> Ellipsoid::_ellipsoids = defaults(); QMap<int, Ellipsoid> Ellipsoid::_ellipsoids = defaults();
@ -29,48 +30,47 @@ const Ellipsoid &Ellipsoid::ellipsoid(int id)
return it.value(); return it.value();
} }
void Ellipsoid::loadList(const QString &path) bool Ellipsoid::loadList(const QString &path)
{ {
QFile file(path); QFile file(path);
CSV csv(&file);
QByteArrayList entry;
bool res; bool res;
int ln = 0;
if (!file.open(QFile::ReadOnly)) { if (!file.open(QFile::ReadOnly)) {
qWarning("Error opening ellipsoids file: %s: %s", qPrintable(path), qWarning("Error opening ellipsoids file: %s: %s", qPrintable(path),
qPrintable(file.errorString())); qPrintable(file.errorString()));
return; return false;
} }
while (!file.atEnd()) { while (!csv.atEnd()) {
ln++; if (!csv.readEntry(entry) || entry.size() < 4) {
qWarning("%s:%d: Parse error", qPrintable(path), csv.line());
QByteArray line = file.readLine(4096); return false;
QList<QByteArray> list = line.split(',');
if (list.size() != 4) {
qWarning("%s: %d: Format error", qPrintable(path), ln);
continue;
} }
int id = list.at(1).trimmed().toInt(&res); int id = entry.at(1).toInt(&res);
if (!res) { if (!res) {
qWarning("%s: %d: Invalid ellipsoid code", qPrintable(path), ln); qWarning("%s: %d: Invalid ellipsoid code", qPrintable(path),
csv.line());
continue; continue;
} }
double radius = list.at(2).trimmed().toDouble(&res); double radius = entry.at(2).toDouble(&res);
if (!res) { if (!res) {
qWarning("%s: %d: Invalid radius", qPrintable(path), ln); qWarning("%s: %d: Invalid radius", qPrintable(path), csv.line());
continue; continue;
} }
double flattening = list.at(3).trimmed().toDouble(&res); double flattening = entry.at(3).toDouble(&res);
if (!res) { if (!res) {
qWarning("%s: %d: Invalid flattening", qPrintable(path), ln); qWarning("%s: %d: Invalid flattening", qPrintable(path), csv.line());
continue; continue;
} }
Ellipsoid e(radius, 1.0/flattening); Ellipsoid e(radius, 1.0/flattening);
_ellipsoids.insert(id, e); _ellipsoids.insert(id, e);
} }
return true;
} }
Ellipsoid::Ellipsoid(double radius, double flattening) Ellipsoid::Ellipsoid(double radius, double flattening)

View File

@ -25,7 +25,7 @@ public:
static const Ellipsoid &WGS84(); static const Ellipsoid &WGS84();
static const Ellipsoid &ellipsoid(int id); static const Ellipsoid &ellipsoid(int id);
static void loadList(const QString &path); static bool loadList(const QString &path);
private: private:
double _radius; double _radius;

View File

@ -1,5 +1,6 @@
#include <QFile> #include <QFile>
#include "common/wgs84.h" #include "common/wgs84.h"
#include "common/csv.h"
#include "gcs.h" #include "gcs.h"
@ -19,26 +20,24 @@ private:
GCS _gcs; GCS _gcs;
}; };
static int parameter(const QString &str, bool *res) static int parameter(const QByteArray &str, bool *res)
{ {
QString field = str.trimmed(); if (str.isEmpty()) {
if (field.isEmpty()) {
*res = true; *res = true;
return 0; return 0;
} }
return field.toInt(res); return str.toInt(res);
} }
static double parameterd(const QString &str, bool *res) static double parameterd(const QByteArray &str, bool *res)
{ {
QString field = str.trimmed(); if (str.isEmpty()) {
if (field.isEmpty()) {
*res = true; *res = true;
return NAN; return NAN;
} }
return field.toDouble(res); return str.toDouble(res);
} }
@ -89,102 +88,100 @@ GCS GCS::gcs(const QString &name)
return GCS(); return GCS();
} }
void GCS::loadList(const QString &path) bool GCS::loadList(const QString &path)
{ {
QFile file(path); QFile file(path);
CSV csv(&file);
QByteArrayList entry;
bool res; bool res;
int ln = 0;
if (!file.open(QFile::ReadOnly)) { if (!file.open(QFile::ReadOnly)) {
qWarning("Error opening PCS file: %s: %s", qPrintable(path), qWarning("Error opening GCS file: %s: %s", qPrintable(path),
qPrintable(file.errorString())); qPrintable(file.errorString()));
return; return false;
} }
while (!file.atEnd()) { while (!csv.atEnd()) {
ln++; if (!csv.readEntry(entry) || entry.size() < 14) {
qWarning("%s:%d: Parse error", qPrintable(path), csv.line());
QByteArray line = file.readLine(4096); return false;
QList<QByteArray> list = line.split(',');
if (list.size() != 14) {
qWarning("%s:%d: Format error", qPrintable(path), ln);
continue;
} }
int id = parameter(list[1], &res); int id = parameter(entry.at(1), &res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid GCS code", qPrintable(path), ln); qWarning("%s:%d: Invalid GCS code", qPrintable(path), csv.line());
continue; continue;
} }
int gd = parameter(list[2], &res); int gd = parameter(entry.at(2), &res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid geodetic datum code", qPrintable(path), qWarning("%s:%d: Invalid geodetic datum code", qPrintable(path),
ln); csv.line());
continue; continue;
} }
int au = list[3].trimmed().toInt(&res); int au = entry.at(3).toInt(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid angular units code", qPrintable(path), qWarning("%s:%d: Invalid angular units code", qPrintable(path),
ln); csv.line());
continue; continue;
} }
int el = list[4].trimmed().toInt(&res); int el = entry.at(4).toInt(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid ellipsoid code", qPrintable(path), ln); qWarning("%s:%d: Invalid ellipsoid code", qPrintable(path),
csv.line());
continue; continue;
} }
int pm = list[5].trimmed().toInt(&res); int pm = entry.at(5).toInt(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid prime meridian code", qPrintable(path), qWarning("%s:%d: Invalid prime meridian code", qPrintable(path),
ln); csv.line());
continue; continue;
} }
int ct = list[6].trimmed().toInt(&res); int ct = entry.at(6).toInt(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid coordinates transformation code", qWarning("%s:%d: Invalid coordinates transformation code",
qPrintable(path), ln); qPrintable(path), csv.line());
continue; continue;
} }
double dx = list[7].trimmed().toDouble(&res); double dx = entry.at(7).toDouble(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid dx", qPrintable(path), ln); qWarning("%s:%d: Invalid dx", qPrintable(path), csv.line());
continue; continue;
} }
double dy = list[8].trimmed().toDouble(&res); double dy = entry.at(8).toDouble(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid dy", qPrintable(path), ln); qWarning("%s:%d: Invalid dy", qPrintable(path), csv.line());
continue; continue;
} }
double dz = list[9].trimmed().toDouble(&res); double dz = entry.at(9).toDouble(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid dz", qPrintable(path), ln); qWarning("%s:%d: Invalid dz", qPrintable(path), csv.line());
continue; continue;
} }
double rx = parameterd(list[10], &res); double rx = parameterd(entry.at(10), &res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid rx", qPrintable(path), ln); qWarning("%s:%d: Invalid rx", qPrintable(path), csv.line());
continue; continue;
} }
double ry = parameterd(list[11], &res); double ry = parameterd(entry.at(11), &res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid ry", qPrintable(path), ln); qWarning("%s:%d: Invalid ry", qPrintable(path), csv.line());
continue; continue;
} }
double rz = parameterd(list[12], &res); double rz = parameterd(entry.at(12), &res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid rz", qPrintable(path), ln); qWarning("%s:%d: Invalid rz", qPrintable(path), csv.line());
continue; continue;
} }
double ds = parameterd(list[13], &res); double ds = parameterd(entry.at(13), &res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid ds", qPrintable(path), ln); qWarning("%s:%d: Invalid ds", qPrintable(path), csv.line());
continue; continue;
} }
const Ellipsoid &e = Ellipsoid::ellipsoid(el); const Ellipsoid &e = Ellipsoid::ellipsoid(el);
if (e.isNull()) { if (e.isNull()) {
qWarning("%s:%d: Unknown ellipsoid code", qPrintable(path), ln); qWarning("%s:%d: Unknown ellipsoid code", qPrintable(path),
csv.line());
continue; continue;
} }
@ -201,22 +198,24 @@ void GCS::loadList(const QString &path)
break; break;
default: default:
qWarning("%s:%d: Unknown coordinates transformation method", qWarning("%s:%d: Unknown coordinates transformation method",
qPrintable(path), ln); qPrintable(path), csv.line());
continue; continue;
} }
if (!datum.isValid()) { if (!datum.isValid()) {
qWarning("%s:%d: Invalid coordinates transformation parameters", qWarning("%s:%d: Invalid coordinates transformation parameters",
qPrintable(path), ln); qPrintable(path), csv.line());
continue; continue;
} }
GCS gcs(datum, pm, au); GCS gcs(datum, pm, au);
if (gcs.isValid()) if (gcs.isValid())
_gcss.append(Entry(id, gd, list[0].trimmed(), gcs)); _gcss.append(Entry(id, gd, entry.at(0), gcs));
else else
qWarning("%s:%d: Unknown prime meridian/angular units code", qWarning("%s:%d: Unknown prime meridian/angular units code",
qPrintable(path), ln); qPrintable(path), csv.line());
} }
return true;
} }
Coordinates GCS::toWGS84(const Coordinates &c) const Coordinates GCS::toWGS84(const Coordinates &c) const

View File

@ -37,7 +37,7 @@ public:
static GCS gcs(const QString &name); static GCS gcs(const QString &name);
static const GCS &WGS84(); static const GCS &WGS84();
static void loadList(const QString &path); static bool loadList(const QString &path);
static QList<KV<int, QString> > list(); static QList<KV<int, QString> > list();
static QList<KV<int, QString> > WGS84List(); static QList<KV<int, QString> > WGS84List();

View File

@ -1,4 +1,5 @@
#include <QFile> #include <QFile>
#include "common/csv.h"
#include "angularunits.h" #include "angularunits.h"
#include "pcs.h" #include "pcs.h"
@ -23,52 +24,51 @@ PCS PCS::pcs(int id)
} }
} }
void PCS::loadList(const QString &path) bool PCS::loadList(const QString &path)
{ {
QFile file(path); QFile file(path);
CSV csv(&file);
QByteArrayList entry;
bool res; bool res;
int ln = 0;
if (!file.open(QFile::ReadOnly)) { if (!file.open(QFile::ReadOnly)) {
qWarning("Error opening PCS file: %s: %s", qPrintable(path), qWarning("Error opening PCS file: %s: %s", qPrintable(path),
qPrintable(file.errorString())); qPrintable(file.errorString()));
return; return false;
} }
while (!file.atEnd()) { while (!csv.atEnd()) {
ln++; if (!csv.readEntry(entry) || entry.size() < 4) {
qWarning("%s:%d: Parse error", qPrintable(path), csv.line());
QByteArray line = file.readLine(4096); return false;
QList<QByteArray> list = line.split(','); }
if (list.size() != 4) { QString name(entry.at(0));
qWarning("%s:%d: Format error", qPrintable(path), ln); int id = entry.at(1).toInt(&res);
if (!res) {
qWarning("%s:%d: Invalid PCS code", qPrintable(path), csv.line());
continue; continue;
} }
int gcs = entry.at(2).toInt(&res);
QString name(list.at(0).trimmed());
int id = list.at(1).trimmed().toInt(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid PCS code", qPrintable(path), ln); qWarning("%s:%d: Invalid GCS code", qPrintable(path), csv.line());
continue; continue;
} }
int gcs = list.at(2).trimmed().toInt(&res); int proj = entry.at(3).toInt(&res);
if (!res) { if (!res) {
qWarning("%s:%d: Invalid GCS code", qPrintable(path), ln); qWarning("%s:%d: Invalid projection code", qPrintable(path),
continue; csv.line());
}
int proj = list.at(3).trimmed().toInt(&res);
if (!res) {
qWarning("%s:%d: Invalid projection code", qPrintable(path), ln);
continue; continue;
} }
if (GCS::gcs(gcs).isNull()) { if (GCS::gcs(gcs).isNull()) {
qWarning("%s:%d: Unknown GCS code", qPrintable(path), ln); qWarning("%s:%d: Unknown GCS code", qPrintable(path), csv.line());
continue; continue;
} }
_pcss.insert(id, Entry(name, gcs, proj)); _pcss.insert(id, Entry(name, gcs, proj));
} }
return true;
} }
QList<KV<int, QString> > PCS::list() QList<KV<int, QString> > PCS::list()

View File

@ -22,7 +22,7 @@ public:
bool isNull() const {return (_gcs.isNull() && _conversion.isNull());} bool isNull() const {return (_gcs.isNull() && _conversion.isNull());}
bool isValid() const {return (_gcs.isValid() && _conversion.isValid());} bool isValid() const {return (_gcs.isValid() && _conversion.isValid());}
static void loadList(const QString &path); static bool loadList(const QString &path);
static PCS pcs(int id); static PCS pcs(int id);
static QList<KV<int, QString> > list(); static QList<KV<int, QString> > list();