123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674 |
- //==============================================================================
- //
- // GPXPlot - the gpx plotter class
- //
- // Copyright (C) 2018 Dick van Oudheusden
- //
- // This library is free software; you can redistribute it and/or
- // modify it under the terms of the GNU Lesser General Public
- // License as published by the Free Software Foundation; either
- // version 3 of the License, or (at your option) any later version.
- //
- // This library is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- // Lesser General Public License for more details.
- //
- // You should have received a copy of the GNU Lesser General Public
- // License along with this library; if not, write to the Free
- // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- //
- //==============================================================================
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <ctime>
- #include <iomanip>
- #include <limits>
- #include <algorithm>
- #include "Arguments.h"
- #include "XMLParser.h"
- #include "Algorithm.h"
- namespace gpxtools
- {
- // -- PlotAxis interface ------------------------------------------
- class PlotAxis
- {
- public:
- PlotAxis() { }
- virtual ~PlotAxis() {}
- virtual bool values(const std::string &latitude, const std::string &longitude, const std::string &elevation, const std::string ×tamp) = 0;
- virtual void rollback() = 0;
- virtual void heading(std::ostream &str, const std::string &xyz) const = 0;
- virtual bool start() = 0;
- virtual bool next(std::ostream &str) = 0;
- };
- // -- DistanceAxis ------------------------------------------------
- class DistanceAxis : public PlotAxis
- {
- public:
- DistanceAxis() :
- PlotAxis(),
- _prevLat(0.0),
- _prevLon(0.0) { }
- ~DistanceAxis() { }
- virtual bool values(const std::string &latitude, const std::string &longitude, const std::string &, const std::string &)
- {
- if (latitude.empty()) return false;
- if (longitude.empty()) return false;
- double lat;
- double lon;
- try
- {
- lat = std::stod(latitude);
- lon = std::stod(longitude);
- }
- catch(...)
- {
- return false;
- }
- if (_distances.empty())
- {
- _distances.push_back(0.0);
- }
- else
- {
- _distances.push_back(_distances.back() + gpx::calcDistance(_prevLat, _prevLon, lat, lon)); // Cumulative distance
- }
- _prevLat = lat;
- _prevLon = lon;
- return true;
- }
- virtual void rollback()
- {
- _distances.pop_back();
- }
- virtual void heading(std::ostream &str, const std::string &xyz) const
- {
- str << "set " << xyz << "label \"Distance (m)\"" << std::endl;
- str << "set " << xyz << "range ["
- << std::fixed << std::setprecision(1) << _distances.front() << ':'
- << std::fixed << std::setprecision(1) << _distances.back() << ']' << std::endl;
- }
- virtual bool start()
- {
- _current = _distances.begin();
- return (_current != _distances.end());
- }
- virtual bool next(std::ostream &str)
- {
- if (_current == _distances.end()) return false;
- str << std::fixed << std::setprecision(1) << *_current;
- _current++;
- return true;
- }
- private:
- double _prevLat;
- double _prevLon;
- std::vector<double> _distances;
- std::vector<double>::iterator _current;
- };
- // -- ElevationAxis -----------------------------------------------
- class ElevationAxis : public PlotAxis
- {
- public:
- ElevationAxis() :
- PlotAxis() { }
- ~ElevationAxis() { }
- virtual bool values(const std::string &, const std::string &, const std::string &elevation, const std::string &)
- {
- if (elevation.empty()) return false;
- double ele;
- try
- {
- ele = std::stod(elevation);
- }
- catch(...)
- {
- return false;
- }
- _elevations.push_back(ele);
- return true;
- }
- virtual void rollback()
- {
- _elevations.pop_back();
- }
- virtual void heading(std::ostream &str, const std::string &xyz) const
- {
- str << "set " << xyz << "label \"Elevation (m)\"" << std::endl;
- double minEle = std::numeric_limits<double>::max();
- double maxEle = -std::numeric_limits<double>::max();
- for (auto elevation = _elevations.begin(); elevation != _elevations.end(); ++elevation)
- {
- minEle = std::min(minEle, *elevation);
- maxEle = std::max(maxEle, *elevation);
- }
- str << "set " << xyz << "range ["
- << std::fixed << std::setprecision(1) << minEle << ':'
- << std::fixed << std::setprecision(1) << maxEle << ']' << std::endl;
- }
- virtual bool start()
- {
- _current = _elevations.begin();
- return (_current != _elevations.end());
- }
- virtual bool next(std::ostream &str)
- {
- if (_current == _elevations.end()) return false;
- str << std::fixed << std::setprecision(1) << *_current;
- _current++;
- return true;
- }
- private:
- std::vector<double> _elevations;
- std::vector<double>::iterator _current;
- };
- // -- SpeedAxis ------------------------------------------------
- class SpeedAxis : public PlotAxis
- {
- public:
- SpeedAxis() :
- PlotAxis(),
- _prevLat(0.0),
- _prevLon(0.0),
- _prevTime(-1) { }
- ~SpeedAxis() { }
- virtual bool values(const std::string &latitude, const std::string &longitude, const std::string &, const std::string ×tamp)
- {
- if (latitude.empty()) return false;
- if (longitude.empty()) return false;
- if (timestamp.empty()) return false;
- time_t time;
- double lat;
- double lon;
- // Time format: 2009-10-17T18:37:31Z
- struct tm fields = {0};
- if (strptime(timestamp.c_str(), "%Y-%m-%dT%TZ", &fields) == nullptr) return false;
- time = mktime(&fields);
- if (time == time_t(-1)) return false;
- try
- {
- lat = std::stod(latitude);
- lon = std::stod(longitude);
- }
- catch(...)
- {
- return false;
- }
- if (_speeds.empty())
- {
- _speeds.push_back(0.0);
- }
- else
- {
- double distance = gpx::calcDistance(_prevLat, _prevLon, lat, lon);
- if ((_prevTime != time_t(-1)) && (time != _prevTime))
- {
- _speeds.push_back((distance / difftime(time, _prevTime)) * 3.6);
- }
- else
- {
- _speeds.push_back(0.0);
- }
- }
- _prevLat = lat;
- _prevLon = lon;
- _prevTime = time;
- return true;
- }
- virtual void rollback()
- {
- _speeds.pop_back();
- }
- virtual void heading(std::ostream &str, const std::string &xyz) const
- {
- str << "set " << xyz << "label \"Speed (km/h)\"" << std::endl;
- double minSpeed = std::numeric_limits<double>::max();
- double maxSpeed = -std::numeric_limits<double>::max();
- for (auto speed = _speeds.begin(); speed != _speeds.end(); ++speed)
- {
- minSpeed = std::min(minSpeed, *speed);
- maxSpeed = std::max(maxSpeed, *speed);
- }
- str << "set " << xyz << "range ["
- << std::fixed << std::setprecision(1) << minSpeed << ':'
- << std::fixed << std::setprecision(1) << maxSpeed << ']' << std::endl;
- }
- virtual bool start()
- {
- _current = _speeds.begin();
- return (_current != _speeds.end());
- }
- virtual bool next(std::ostream &str)
- {
- if (_current == _speeds.end()) return false;
- str << std::fixed << std::setprecision(1) << *_current;
- _current++;
- return true;
- }
- private:
- double _prevLat;
- double _prevLon;
- time_t _prevTime;
- std::vector<double> _speeds;
- std::vector<double>::iterator _current;
- };
- // -- TimeAxis ------------------------------------------------
- class TimeAxis : public PlotAxis
- {
- public:
- TimeAxis() :
- PlotAxis() { }
- ~TimeAxis() { }
- virtual bool values(const std::string &, const std::string &, const std::string &, const std::string ×tamp)
- {
- time_t time;
- // Time format: 2009-10-17T18:37:31Z
- if (timestamp.empty()) return false;
- struct tm fields = {0};
- if (strptime(timestamp.c_str(), "%Y-%m-%dT%TZ", &fields) == nullptr) return false;
- time = timegm(&fields); // trick: gnuplot uses UTC
- if (time == time_t(-1)) return false;
- _times.push_back(time);
- return true;
- }
- virtual void rollback()
- {
- _times.pop_back();
- }
- virtual void heading(std::ostream &str, const std::string &xyz) const
- {
- str << "set " << xyz << "label \"Time\"" << std::endl;
- str << "set timefmt \"" << "%s" << "\"" << std::endl;
- str << "set " << xyz << "data time" << std::endl;
- str << "set format " << xyz << " \"%H:%M\"" << std::endl;
- str << "set " << xyz << "range [\"" << _times.front() << "\":\"" << _times.back() << "\"]" << std::endl;
- }
- virtual bool start()
- {
- _current = _times.begin();
- return (_current != _times.end());
- }
- virtual bool next(std::ostream &str)
- {
- if (_current == _times.end()) return false;
- str << *_current;
- _current++;
- return true;
- }
- private:
- std::vector<time_t> _times;
- std::vector<time_t>::iterator _current;
- };
- // -- GPXPlot -----------------------------------------------------
- class GPXPlot : public XMLParserHandler
- {
- public:
- GPXPlot() :
- _arguments("gpxplot [OPTION].. [FILE]\nGenerate a GnuPlot command file from a GPX-file on standard output.\n", "gpxplot v0.1",
- "Generate a GnuPlot command file from the distance, elevation, speed and time in a GPX-file on standard output."),
- _title (_arguments, true, 't', "title", "TITLE", "set the title of the plot", ""),
- _xTitle (_arguments, true, 'x', "x", "SOURCE", "set the x-axis source (distance, time) (def:time)", "time"),
- _yTitle (_arguments, true, 'y', "y", "SOURCE", "set the y-axis source (elevation, distance, speed) (def:speed)", "speed"),
- _style (_arguments, true, 'p', "plot", "STYLE", "set the plot style (lines,..) (def:lines)", "lines"),
- _color (_arguments, true, 'c', "color", "COLOR", "set the line color", ""),
- _term (_arguments, true, 'm', "term", "x11|qt|..", "set the output device and options", ""),
- _width (_arguments, true, 'w', "width", "WIDTH", "set the width of the plot", "1024"),
- _height (_arguments, true, 'h', "height", "HEIGHT", "set the height of the plot", "768"),
- _output (_arguments, true, 'o', "output", "FILE", "set the output file of the plot", ""),
- _xmlParser(this),
- _xAxis(nullptr),
- _yAxis(nullptr)
- {
- }
- ~GPXPlot()
- {
- }
- bool parseArguments(int argc, char *argv[])
- {
- std::vector<std::string> filenames;
- if (!_arguments.parse(argc,argv, filenames))
- {
- return false;
- }
- else if (!checkArguments(filenames))
- {
- return false;
- }
- else if (filenames.empty())
- {
- return _xmlParser.parse(std::cin);
- }
- else
- {
- return parseFile(filenames.front());
- }
- }
- bool checkArguments(const std::vector<std::string> &filenames)
- {
- if (filenames.size() > 1)
- {
- std::cerr << "Too many input files." << std::endl;
- return false;
- }
- if (_xTitle.value() == "distance")
- {
- _xAxis = new DistanceAxis();
- }
- else if (_xTitle.value() == "time")
- {
- _xAxis = new TimeAxis();
- }
- else
- {
- std::cerr << "Invalid value for --" << _xTitle.longOption() << ": " << _xTitle.value() << std::endl;
- return false;
- }
- if (_yTitle.value() == "elevation")
- {
- _yAxis = new ElevationAxis();
- }
- else if (_yTitle.value() == "distance")
- {
- _yAxis = new DistanceAxis();
- }
- else if (_yTitle.value() == "speed")
- {
- _yAxis = new SpeedAxis();
- }
- else
- {
- std::cerr << "Invalid value for --" << _yTitle.longOption() << ": " << _yTitle.value() << std::endl;
- return false;
- }
- if (_style.value() != "lines")
- {
- std::cerr << "Invalid value for --" << _style.longOption() << ": " << _style.value() << std::endl;
- return false;
- }
- return true;
- }
- bool parseFile(const std::string &filename)
- {
- bool ok =false;
- std::ifstream file(filename);
- if (file.is_open())
- {
- ok = _xmlParser.parse(file);
- file.close();
- }
- else
- {
- std::cerr << "Unable to open: " << filename << std::endl;
- }
- return ok;
- }
- void generate(std::ostream &output)
- {
- if ((!_xAxis->start()) || (!_yAxis->start()))
- {
- std::cerr << "No values present for the plot." << std::endl;
- return;
- }
- // Export the data
- output << "$DATA << EOD" << std::endl;
- while(true)
- {
- if (!_xAxis->next(output)) break;
- output << ' ';
- if (!_yAxis->next(output)) break;
- output << std::endl;
- }
- output << "EOD" <<std::endl;
- if (!_title.value().empty())
- output << "set title \"" << _title.value() << "\"" << std::endl;
- output << "unset key" << std::endl;
- _xAxis->heading(output, "x");
- _yAxis->heading(output, "y");
- if (!_output.value().empty())
- {
- output << "set output \"" << _output.value() << "\"" << std::endl;
- if (!_term.value().empty())
- {
- output << "set term " << _term.value() << std::endl;
- }
- else
- {
- std::size_t pos = _output.value().find_last_of('.');
- if (pos != std::string::npos)
- {
- std::string extension = _output.value().substr(pos+1);
- std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
- if (extension == "jpg")
- {
- extension = "jpeg";
- }
- output << "set term " << extension;
- if ((!_width.value().empty()) && (!_height.value().empty()))
- {
- output << " size " << _width.value() << ',' << _height.value();
- }
- output << std::endl;
- }
- }
- }
- else if (!_term.value().empty())
- {
- output << "set term " << _term.value() << std::endl;
- }
- output << "plot $DATA using 1:2 with " << _style.value();
- if (!_color.value().empty())
- {
- output << " lt rgb \"" << _color.value() << "\"";
- }
- output << std::endl;
- if (_output.value().empty())
- {
- output << "pause -1" << std::endl;
- }
- }
- virtual void startElement(const std::string &path, const std::string &, const Attributes &attributes)
- {
- if (path == "/gpx/trk/trkseg/trkpt")
- {
- _lat.clear();
- _lon.clear();
- _elevation.clear();
- _time.clear();
- _lat = fetch(attributes, "lat");
- _lon = fetch(attributes, "lon");
- }
- }
- virtual void text(const std::string &path, const std::string &text)
- {
- if (path == "/gpx/trk/trkseg/trkpt/ele")
- {
- _elevation.append(text);
- }
- if (path == "/gpx/trk/trkseg/trkpt/time")
- {
- _time.append(text);
- }
- }
- virtual void endElement(const std::string &path, const std::string &)
- {
- if (path == "/gpx/trk/trkseg/trkpt")
- {
- XMLParser::trim(_lat);
- XMLParser::trim(_lon);
- XMLParser::trim(_elevation);
- XMLParser::trim(_time);
- if (_xAxis->values(_lat, _lon, _elevation, _time))
- {
- if (!_yAxis->values(_lat, _lon, _elevation, _time))
- {
- _xAxis->rollback();
- }
- }
- }
- }
- private:
- std::string fetch(const Attributes &attributes, const std::string &key)
- {
- auto attribute = attributes.find(key);
- return (attribute != attributes.end() ? attribute->second : "");
- }
- private:
- arg::Arguments _arguments;
- arg::Argument _title;
- arg::Argument _xTitle;
- arg::Argument _yTitle;
- arg::Argument _style;
- arg::Argument _color;
- arg::Argument _term;
- arg::Argument _width;
- arg::Argument _height;
- arg::Argument _output;
- XMLParser _xmlParser;
- std::string _lat;
- std::string _lon;
- std::string _elevation;
- std::string _time;
- PlotAxis *_xAxis;
- PlotAxis *_yAxis;
- };
- }
- int main(int argc, char *argv[])
- {
- gpxtools::GPXPlot gpxPlot;
- if (gpxPlot.parseArguments(argc, argv))
- {
- gpxPlot.generate(std::cout);
- return 0;
- }
- return 1;
- }
|