diff --git a/src/GUI/app.cpp b/src/GUI/app.cpp index a2df2fa2..d9c20b4c 100644 --- a/src/GUI/app.cpp +++ b/src/GUI/app.cpp @@ -12,7 +12,7 @@ #include "map/ellipsoid.h" #include "map/gcs.h" #include "map/pcs.h" -#include "data/track.h" +#include "data/dem.h" #include "opengl.h" #include "gui.h" #include "settings.h" @@ -65,8 +65,7 @@ App::App(int &argc, char **argv) : QApplication(argc, argv) OPENGL_SET_SAMPLES(4); loadDatums(); loadPCSs(); - - Track::setDEMDir(ProgramPaths::demDir()); + DEM::setDir(ProgramPaths::demDir()); _gui = new GUI(); } diff --git a/src/GUI/gui.cpp b/src/GUI/gui.cpp index 685e6328..0f0f68b1 100644 --- a/src/GUI/gui.cpp +++ b/src/GUI/gui.cpp @@ -875,6 +875,11 @@ void GUI::openOptions() Track::action(options.option); \ reload = true; \ } +#define SET_DATA_OPTION(option, action) \ + if (options.option != _options.option) { \ + Data::action(options.option); \ + reload = true; \ + } Options options(_options); bool reload = false; @@ -913,7 +918,7 @@ void GUI::openOptions() SET_TRACK_OPTION(pauseSpeed, setPauseSpeed); SET_TRACK_OPTION(pauseInterval, setPauseInterval); SET_TRACK_OPTION(useReportedSpeed, useReportedSpeed); - SET_TRACK_OPTION(useDEMElevation, useDEMElevation); + SET_DATA_OPTION(useDEMElevation, useDEMElevation); if (options.poiRadius != _options.poiRadius) _poi->setRadius(options.poiRadius); @@ -2077,7 +2082,7 @@ void GUI::readSettings() Track::setPauseSpeed(_options.pauseSpeed); Track::setPauseInterval(_options.pauseInterval); Track::useReportedSpeed(_options.useReportedSpeed); - Track::useDEMElevation(_options.useDEMElevation); + Data::useDEMElevation(_options.useDEMElevation); _poi->setRadius(_options.poiRadius); diff --git a/src/data/data.cpp b/src/data/data.cpp index d8708a17..6a7b7703 100644 --- a/src/data/data.cpp +++ b/src/data/data.cpp @@ -11,6 +11,7 @@ #include "oziparsers.h" #include "locparser.h" #include "slfparser.h" +#include "dem.h" #include "data.h" @@ -49,6 +50,7 @@ static QHash parsers() QHash Data::_parsers = parsers(); +bool Data::_useDEMElevation = false; Data::~Data() { @@ -64,6 +66,13 @@ void Data::processData() _tracks.append(new Track(_trackData.at(i))); for (int i = 0; i < _routeData.count(); i++) _routes.append(new Route(_routeData.at(i))); + for (int i = 0; i < _waypoints.size(); i++) { + if (!_waypoints.at(i).hasElevation() || _useDEMElevation) { + qreal elevation = DEM::elevation(_waypoints.at(i).coordinates()); + if (!std::isnan(elevation)) + _waypoints[i].setElevation(elevation); + } + } } bool Data::loadFile(const QString &fileName) @@ -134,3 +143,10 @@ QStringList Data::filter() return filter; } + +void Data::useDEMElevation(bool use) +{ + _useDEMElevation = use; + Route::useDEMElevation(use); + Track::useDEMElevation(use); +} diff --git a/src/data/data.h b/src/data/data.h index 080ab0e9..071cb807 100644 --- a/src/data/data.h +++ b/src/data/data.h @@ -32,6 +32,8 @@ public: static QString formats(); static QStringList filter(); + static void useDEMElevation(bool use); + private: void processData(); @@ -46,6 +48,7 @@ private: QList _routeData; static QHash _parsers; + static bool _useDEMElevation; }; #endif // DATA_H diff --git a/src/data/dem.cpp b/src/data/dem.cpp index 3b538fbc..ed5f6d84 100644 --- a/src/data/dem.cpp +++ b/src/data/dem.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include "common/coordinates.h" #include "dem.h" @@ -48,7 +50,11 @@ static qreal height(const Coordinates &c, const QByteArray data) return interpolate(dx, dy, p0, p1, p2, p3); } -QString DEM::fileName(const Key &key) const + +QString DEM::_dir; +QMap DEM::_data; + +QString DEM::fileName(const Key &key) { const char ns = (key.lat >= 0) ? 'N' : 'S'; const char ew = (key.lon >= 0) ? 'E' : 'W'; @@ -59,6 +65,11 @@ QString DEM::fileName(const Key &key) const return QDir(_dir).absoluteFilePath(basename); } +void DEM::setDir(const QString &path) +{ + _dir = path; +} + qreal DEM::elevation(const Coordinates &c) { if (_dir.isEmpty()) diff --git a/src/data/dem.h b/src/data/dem.h index 6cd14674..a58f8ed4 100644 --- a/src/data/dem.h +++ b/src/data/dem.h @@ -1,19 +1,18 @@ #ifndef DEM_H #define DEM_H +#include #include #include -#include -class QDir; +class QString; class Coordinates; class DEM { public: - DEM() {} - DEM(const QString &dir) : _dir(dir) {} - qreal elevation(const Coordinates &c); + static void setDir(const QString &path); + static qreal elevation(const Coordinates &c); private: struct Key { @@ -31,13 +30,12 @@ private: else return (lat < other.lat); } - }; - QString fileName(const Key &key) const; + static QString fileName(const Key &key); - QString _dir; - QMap _data; + static QString _dir; + static QMap _data; }; #endif // DEM_H diff --git a/src/data/route.cpp b/src/data/route.cpp index 40d072f6..3f1313aa 100644 --- a/src/data/route.cpp +++ b/src/data/route.cpp @@ -1,12 +1,17 @@ +#include "dem.h" #include "route.h" + +bool Route::_useDEMElevation = false; + Route::Route(const RouteData &data) : _data(data) { qreal dist = 0; - _distance.append(dist); - for (int i = 1; i < data.count(); i++) { - dist += data.at(i).coordinates().distanceTo(data.at(i-1).coordinates()); + _distance.append(0); + + for (int i = 1; i < _data.count(); i++) { + dist += _data.at(i).coordinates().distanceTo(_data.at(i-1).coordinates()); _distance.append(dist); } } @@ -25,10 +30,19 @@ Graph Route::elevation() const { Graph graph; - for (int i = 0; i < _data.size(); i++) - if (_data.at(i).hasElevation()) + for (int i = 0; i < _data.size(); i++) { + if (_data.at(i).hasElevation() && !_useDEMElevation) graph.append(GraphPoint(_distance.at(i), NAN, _data.at(i).elevation())); + else { + qreal elevation = DEM::elevation(_data.at(i).coordinates()); + if (!std::isnan(elevation)) + graph.append(GraphPoint(_distance.at(i), NAN, elevation)); + else if (_data.at(i).hasElevation()) + graph.append(GraphPoint(_distance.at(i), NAN, + _data.at(i).elevation())); + } + } return graph; } diff --git a/src/data/route.h b/src/data/route.h index e413fdb2..e79c9c6a 100644 --- a/src/data/route.h +++ b/src/data/route.h @@ -24,9 +24,13 @@ public: bool isNull() const {return (_data.count() < 2);} + static void useDEMElevation(bool use) {_useDEMElevation = use;} + private: const RouteData &_data; QVector _distance; + + static bool _useDEMElevation; }; #endif // ROUTE_H diff --git a/src/data/track.cpp b/src/data/track.cpp index 2374ae04..b634f569 100644 --- a/src/data/track.cpp +++ b/src/data/track.cpp @@ -15,8 +15,6 @@ bool Track::_outlierEliminate = true; bool Track::_useReportedSpeed = false; bool Track::_useDEMElevation = false; -DEM Track::_dem; - static qreal median(QVector &v) { @@ -160,7 +158,7 @@ Graph Track::elevation() const raw.append(GraphPoint(_distance.at(i), _time.at(i), _data.at(i).elevation())); else { - qreal elevation = _dem.elevation(_data.at(i).coordinates()); + qreal elevation = DEM::elevation(_data.at(i).coordinates()); if (!std::isnan(elevation)) raw.append(GraphPoint(_distance.at(i), _time.at(i), elevation)); else if (_data.at(i).hasElevation()) diff --git a/src/data/track.h b/src/data/track.h index 63eece20..ca5bbfd2 100644 --- a/src/data/track.h +++ b/src/data/track.h @@ -7,7 +7,6 @@ #include #include "trackdata.h" #include "graph.h" -#include "dem.h" #include "path.h" @@ -47,7 +46,6 @@ public: {_outlierEliminate = eliminate;} static void useReportedSpeed(bool use) {_useReportedSpeed = use;} static void useDEMElevation(bool use) {_useDEMElevation = use;} - static void setDEMDir(const QString &path) {_dem = DEM(path);} private: bool discardStopPoint(int i) const; @@ -73,7 +71,6 @@ private: static int _pauseInterval; static bool _useReportedSpeed; static bool _useDEMElevation; - static DEM _dem; }; #endif // TRACK_H