1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 05:34:47 +01:00
GPXSee/src/data/data.cpp

211 lines
5.4 KiB
C++
Raw Normal View History

2019-01-18 00:17:28 +01:00
#include <QApplication>
#include <QFile>
#include <QFileInfo>
2016-10-23 11:09:20 +02:00
#include <QLineF>
2019-03-21 23:45:04 +01:00
#include "common/config.h"
2016-10-23 11:09:20 +02:00
#include "gpxparser.h"
#include "tcxparser.h"
#include "csvparser.h"
#include "kmlparser.h"
#include "fitparser.h"
2016-11-10 00:08:11 +01:00
#include "igcparser.h"
2016-11-16 23:54:15 +01:00
#include "nmeaparser.h"
#include "oziparsers.h"
#include "locparser.h"
2018-08-10 22:46:04 +02:00
#include "slfparser.h"
2019-03-21 23:45:04 +01:00
#ifdef ENABLE_GEOJSON
2019-01-25 22:18:21 +01:00
#include "geojsonparser.h"
2019-03-21 23:45:04 +01:00
#endif // ENABLE_GEOJSON
2019-03-13 08:42:18 +01:00
#include "exifparser.h"
2019-08-15 21:27:55 +02:00
#include "cupparser.h"
2019-10-25 22:30:12 +02:00
#include "gpiparser.h"
#include "dem.h"
2016-10-23 11:09:20 +02:00
#include "data.h"
2017-09-14 20:19:19 +02:00
static GPXParser gpx;
static TCXParser tcx;
static KMLParser kml;
static FITParser fit;
static CSVParser csv;
static IGCParser igc;
static NMEAParser nmea;
static PLTParser plt;
static WPTParser wpt;
static RTEParser rte;
static LOCParser loc;
2018-08-10 22:46:04 +02:00
static SLFParser slf;
2019-03-21 23:45:04 +01:00
#ifdef ENABLE_GEOJSON
2019-01-25 22:18:21 +01:00
static GeoJSONParser geojson;
2019-03-21 23:45:04 +01:00
#endif // ENABLE_GEOJSON
2019-03-13 00:25:46 +01:00
static EXIFParser exif;
2019-08-15 21:27:55 +02:00
static CUPParser cup;
2019-10-25 22:30:12 +02:00
static GPIParser gpi;
2017-09-14 20:19:19 +02:00
2017-07-27 19:47:46 +02:00
static QHash<QString, Parser*> parsers()
2016-10-23 11:09:20 +02:00
{
2017-07-27 19:47:46 +02:00
QHash<QString, Parser*> hash;
2018-03-09 18:57:23 +01:00
hash.insert("gpx", &gpx);
hash.insert("tcx", &tcx);
hash.insert("kml", &kml);
hash.insert("fit", &fit);
hash.insert("csv", &csv);
hash.insert("igc", &igc);
hash.insert("nmea", &nmea);
hash.insert("plt", &plt);
hash.insert("wpt", &wpt);
hash.insert("rte", &rte);
hash.insert("loc", &loc);
2018-08-10 22:46:04 +02:00
hash.insert("slf", &slf);
2019-03-21 23:45:04 +01:00
#ifdef ENABLE_GEOJSON
2019-01-25 22:18:21 +01:00
hash.insert("json", &geojson);
hash.insert("geojson", &geojson);
2019-03-21 23:45:04 +01:00
#endif // ENABLE_GEOJSON
2019-03-13 00:25:46 +01:00
hash.insert("jpeg", &exif);
hash.insert("jpg", &exif);
2019-08-15 21:27:55 +02:00
hash.insert("cup", &cup);
2019-10-25 22:30:12 +02:00
hash.insert("gpi", &gpi);
2017-07-27 19:47:46 +02:00
return hash;
2016-10-23 11:09:20 +02:00
}
2017-09-14 20:19:19 +02:00
2017-07-27 19:47:46 +02:00
QHash<QString, Parser*> Data::_parsers = parsers();
bool Data::_useDEM = false;
2017-07-27 19:47:46 +02:00
void Data::processData(QList<TrackData> &trackData, QList<RouteData> &routeData)
2016-10-23 11:09:20 +02:00
{
for (int i = 0; i < trackData.count(); i++) {
TrackData &track = trackData[i];
for (int j = 0; j < track.size(); j++) {
SegmentData &segment = track[j];
for (int k = 0; k < segment.size(); k++) {
Trackpoint &t = segment[k];
if (!t.hasElevation() || _useDEM) {
qreal elevation = DEM::elevation(t.coordinates());
if (!std::isnan(elevation))
t.setElevation(elevation);
}
}
}
2019-01-31 01:46:53 +01:00
_tracks.append(Track(trackData.at(i)));
}
for (int i = 0; i < routeData.count(); i++) {
RouteData &route = routeData[i];
for (int j = 0; j < route.size(); j++) {
Waypoint &w = route[j];
if (!w.hasElevation() || _useDEM) {
qreal elevation = DEM::elevation(w.coordinates());
if (!std::isnan(elevation))
w.setElevation(elevation);
}
}
2019-01-31 01:46:53 +01:00
_routes.append(Route(routeData.at(i)));
}
for (int i = 0; i < _waypoints.size(); i++) {
if (!_waypoints.at(i).hasElevation() || _useDEM) {
qreal elevation = DEM::elevation(_waypoints.at(i).coordinates());
if (!std::isnan(elevation))
_waypoints[i].setElevation(elevation);
}
}
}
Data::Data(const QString &fileName, bool poi)
2016-10-23 11:09:20 +02:00
{
QFile file(fileName);
QFileInfo fi(fileName);
2019-01-31 01:46:53 +01:00
QList<TrackData> trackData;
QList<RouteData> routeData;
2019-01-18 00:17:28 +01:00
_valid = false;
2016-10-23 11:09:20 +02:00
_errorLine = 0;
if (!file.open(QFile::ReadOnly)) {
2016-10-23 11:09:20 +02:00
_errorString = qPrintable(file.errorString());
2019-01-18 00:17:28 +01:00
return;
2016-10-23 11:09:20 +02:00
}
QHash<QString, Parser*>::iterator it;
if ((it = _parsers.find(fi.suffix().toLower())) != _parsers.end()) {
2019-01-31 01:46:53 +01:00
if (it.value()->parse(&file, trackData, routeData, _polygons,
_waypoints)) {
if (!poi)
2019-01-31 01:46:53 +01:00
processData(trackData, routeData);
2019-01-18 00:17:28 +01:00
_valid = true;
return;
} else {
_errorLine = it.value()->errorLine();
_errorString = it.value()->errorString();
2016-10-23 11:09:20 +02:00
}
} else {
for (it = _parsers.begin(); it != _parsers.end(); it++) {
2019-01-31 01:46:53 +01:00
if (it.value()->parse(&file, trackData, routeData, _polygons,
_waypoints)) {
if (!poi)
2019-01-31 01:46:53 +01:00
processData(trackData, routeData);
2019-01-18 00:17:28 +01:00
_valid = true;
return;
}
file.reset();
2016-10-23 11:09:20 +02:00
}
2018-09-24 23:07:11 +02:00
qWarning("Error loading data file: %s:", qPrintable(fileName));
for (it = _parsers.begin(); it != _parsers.end(); it++)
2018-09-24 23:07:11 +02:00
qWarning("%s: line %d: %s", qPrintable(it.key()),
it.value()->errorLine(), qPrintable(it.value()->errorString()));
_errorLine = 0;
_errorString = "Unknown format";
2016-10-23 11:09:20 +02:00
}
}
2017-07-27 19:47:46 +02:00
QString Data::formats()
{
2019-01-25 22:18:21 +01:00
QStringList l(filter());
qSort(l);
QString supported;
for (int i = 0; i < l.size(); i++) {
supported += l.at(i);
if (i != l.size() - 1)
supported += " ";
}
2018-02-04 15:30:54 +01:00
return
2019-01-25 22:18:21 +01:00
qApp->translate("Data", "Supported files") + " (" + supported + ");;"
2019-01-18 00:17:28 +01:00
+ qApp->translate("Data", "CSV files") + " (*.csv);;"
2019-08-15 21:27:55 +02:00
+ qApp->translate("Data", "CUP files") + " (*.cup);;"
2019-01-18 00:17:28 +01:00
+ qApp->translate("Data", "FIT files") + " (*.fit);;"
2019-03-21 23:45:04 +01:00
#ifdef ENABLE_GEOJSON
2019-01-25 22:18:21 +01:00
+ qApp->translate("Data", "GeoJSON files") + " (*.geojson *.json);;"
2019-03-21 23:45:04 +01:00
#endif // ENABLE_GEOJSON
2019-10-25 22:30:12 +02:00
+ qApp->translate("Data", "GPI files") + " (*.gpi);;"
2019-01-18 00:17:28 +01:00
+ qApp->translate("Data", "GPX files") + " (*.gpx);;"
+ qApp->translate("Data", "IGC files") + " (*.igc);;"
2019-03-13 00:25:46 +01:00
+ qApp->translate("Data", "JPEG images") + " (*.jpg *.jpeg);;"
2019-01-18 00:17:28 +01:00
+ qApp->translate("Data", "KML files") + " (*.kml);;"
+ qApp->translate("Data", "LOC files") + " (*.loc);;"
+ qApp->translate("Data", "NMEA files") + " (*.nmea);;"
+ qApp->translate("Data", "OziExplorer files") + " (*.plt *.rte *.wpt);;"
+ qApp->translate("Data", "SLF files") + " (*.slf);;"
+ qApp->translate("Data", "TCX files") + " (*.tcx);;"
+ qApp->translate("Data", "All files") + " (*)";
2017-07-27 19:47:46 +02:00
}
QStringList Data::filter()
{
QStringList filter;
QHash<QString, Parser*>::iterator it;
for (it = _parsers.begin(); it != _parsers.end(); it++)
filter << QString("*.%1").arg(it.key());
return filter;
}
void Data::useDEM(bool use)
{
_useDEM = use;
}