123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- #include <iostream>
- #include <cstring>
- #include <fstream>
- #include <vector>
- #include <cmath>
- #include <limits>
- #include <iomanip>
- #include "XMLParser.h"
- #include "Arguments.h"
- #include "Algorithm.h"
- // ----------------------------------------------------------------------------
- namespace gpxtools
- {
- class GPXJson : public XMLParserHandler
- {
- public:
- // -- Constructor -----------------------------------------------------------
- GPXJson() :
- _arguments("gpxjson [OPTION].. [FILE]\nDisplay the GeoJson data from a GPX-file on standard output.\n", "gpxjson v0.1", "Display the waypoints, routes and/or tracks in GeoJson format from a GPX-file on standard output."),
- _waypoints(_arguments, true, 'w', "waypoints", "convert the waypoints"),
- _tracks (_arguments, true, 't', "tracks", "convert the tracks"),
- _routes (_arguments, true, 'r', "routes", "convert the routes"),
- _mode (_arguments, true, 'm', "mode", "compact|normal", "set the output mode (def:normal)", "normal"),
- _number (_arguments, true, 'p', "points", "NUMBER", "set the number of points per line in normal mode (def:4)", "4"),
- _simplify (_arguments, true, 's', "simplify", "METRES", "simplify the route by track distance", ""),
- _xmlParser(this)
- {
- }
- // -- Deconstructor ---------------------------------------------------------
- virtual ~GPXJson()
- {
- }
- // -- Properties ------------------------------------------------------------
- // -- Parse arguments -------------------------------------------------------
- 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());
- }
- }
- // -- Check arguments -------------------------------------------------------
- bool checkArguments(const std::vector<std::string> &filenames)
- {
- if (filenames.size() > 1)
- {
- std::cerr << "Too many input files." << std::endl;
- return false;
- }
- if ((_mode.value() != "compact") && (_mode.value() != "normal"))
- {
- std::cerr << "Invalid value for --" << _mode.longOption() << ": " << _mode.value() << std::endl;
- return false;
- }
- try
- {
- int number = std::stoi(_number.value());
- if (number <= 0)
- {
- std::cerr << "Invalid value for --" << _number.longOption() << ": " << _number.value() << std::endl;
- return false;
- }
- }
- catch (...)
- {
- std::cerr << "Invalid number for --" << _number.longOption() << ": " << _number.value() << std::endl;
- return false;
- }
- if (_simplify.active())
- {
- try
- {
- double distance = std::stod(_simplify.value());
- if (distance <= 0.0)
- {
- std::cerr << "Invalid value for --" << _number.longOption() << ": " << _simplify.value() << std::endl;
- return false;
- }
- }
- catch(...)
- {
- std::cerr << "Invalid value for --" << _number.longOption() << ": " << _simplify.value() << std::endl;
- return false;
- }
- }
- return true;
- }
- // -- Parse a file ----------------------------------------------------------
- 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 outputJson(std::ostream &output)
- {
- std::size_t points = _points.size();
- std::size_t lines = _lines.size();
- if (points == 1) outputOnePoint(output);
- if (points > 1) outputMultiplePoints(output);
- if (lines == 1) outputOneLine(output);
- if (lines > 1) outputMultipleLines(output);
- }
- void simplify()
- {
- if (!_simplify.active()) return;
- double setting = std::stod(_simplify.value());
- for (auto line = _lines.begin(); line != _lines.end(); ++line)
- {
- while (true)
- {
- auto p1 = line->end();
- auto p2 = line->end();
- auto p3 = line->begin();
- auto ps = line->end();
- double low = 9999.99;
- while (p3 != line->end())
- {
- if ((p1 != line->end()) && (p2 != line->end()))
- {
- double d = (gpx::calcDistance(p1->_lat, p1->_lon, p2->_lat, p2->_lon) +
- gpx::calcDistance(p2->_lat, p2->_lon, p3->_lat, p3->_lon)) -
- gpx::calcDistance(p1->_lat, p1->_lon, p3->_lat, p3->_lon);
- if (d < low)
- {
- low = d;
- ps = p2;
- }
- }
- p1 = p2;
- p2 = p3;
- ++p3;
- }
- if ((low > setting) || (ps == line->end())) break;
- line->erase(ps);
- }
- }
- }
- private:
- void outputOnePoint(std::ostream &output)
- {
- output << std::fixed << std::setprecision(6);
- output << '{'; doEndl(output);
- doIndent();
- output << _indent << "\"type\":\"Point\","; doEndl(output);
- output << _indent << "\"coordinates\":[" << _points.front()._lon << ',' << _points.front()._lat << "]"; doEndl(output);
- doOutdent();
- output << '}'; doEndl(output);
- }
- void outputMultiplePoints(std::ostream &output)
- {
- output << std::fixed << std::setprecision(6);
- output << '{'; doEndl(output);
- doIndent();
- output << _indent << "\"type\":\"MultiPoint\","; doEndl(output);
- output << _indent << "\"coordinates\":["; doEndl(output);
- doIndent();
- int number = std::stoi(_number.value());
- auto iter = _points.begin();
- while (iter != _points.end())
- {
- output << _indent;
- int count = 0;
- while (iter != _points.end() && count < number)
- {
- auto next = iter + 1;
- output << '[' << iter->_lon << ',' << iter->_lat << ']';
- if (next != _points.end()) output << ',';
- ++iter; count++;
- }
- doEndl(output);
- }
- doOutdent();
- output << _indent << "]"; doEndl(output);
- doOutdent();
- output << '}'; doEndl(output);
- }
- void outputOneLine(std::ostream &output)
- {
- output << std::fixed << std::setprecision(6);
- output << '{'; doEndl(output);
- doIndent();
- output << _indent << "\"type\":\"LineString\","; doEndl(output);
- output << _indent << "\"coordinates\":["; doEndl(output);
- doIndent();
- int number = std::stoi(_number.value());
- auto iter = _lines.front().begin();
- auto end = _lines.front().end();
- while (iter != end)
- {
- output << _indent;
- int count = 0;
- while (iter != end && count < number)
- {
- auto next = iter + 1;
- output << '[' << iter->_lon << ',' << iter->_lat << ']';
- if (next != end) output << ',';
- ++iter; count++;
- }
- doEndl(output);
- }
- doOutdent();
- output << _indent << "]"; doEndl(output);
- doOutdent();
- output << '}'; doEndl(output);
- }
- void outputMultipleLines(std::ostream &output)
- {
- output << std::fixed << std::setprecision(6);
- output << '{'; doEndl(output);
- doIndent();
- output << _indent << "\"type\":\"MultiLineString\","; doEndl(output);
- output << _indent << "\"coordinates\":["; doEndl(output);
- doIndent();
- int number = std::stoi(_number.value());
- output << _indent;
- for (auto iter2 = _lines.begin(); iter2 != _lines.end(); ++iter2)
- {
- auto next2 = iter2 + 1;
- output << "["; doEndl(output);
- doIndent();
- auto iter = iter2->begin();
- while (iter != iter2->end())
- {
- output << _indent;
- int count = 0;
- while (iter != iter2->end() && count < number)
- {
- auto next = iter + 1;
- output << '[' << iter->_lon << ',' << iter->_lat << ']';
- if (next != iter2->end()) output << ',';
- ++iter; count++;
- }
- doEndl(output);
- }
- doOutdent();
- output << _indent << "]";
- if (next2 != _lines.end()) output << ",";
- }
- doEndl(output);
- doOutdent();
- output << _indent << "]"; doEndl(output);
- doOutdent();
- output << "}"; doEndl(output);
- }
- void doEndl(std::ostream &output)
- {
- if (_mode.value() == "normal") output << std::endl;
- }
- void doIndent()
- {
- if (_mode.value() == "normal") _indent += " ";
- }
- void doOutdent()
- {
- if (_mode.value() == "normal") _indent.erase(_indent.size()-2);
- }
- static double getDoubleAttribute(const Attributes &atts, const std::string &key)
- {
- auto iter = atts.find(key);
- try
- {
- return iter != atts.end() ? std::stod(iter->second) : std::numeric_limits<double>::min();
- }
- catch(...)
- {
- return std::numeric_limits<double>::min();
- }
- }
- public:
- // -- Callbacks -------------------------------------------------------------
- virtual void startElement(const std::string &path, const std::string &, const Attributes &attributes)
- {
- if ((_tracks.active() && (path == "/gpx/trk/trkseg")) ||
- (_routes.active() && (path == "/gpx/rte")))
- {
- _line.clear();
- }
- else if ((_tracks.active() && (path == "/gpx/trk/trkseg/trkpt")) ||
- (_routes.active() && (path == "/gpx/rte/rtept")))
- {
- double lat = getDoubleAttribute(attributes, "lat");
- double lon = getDoubleAttribute(attributes, "lon");
- _line.push_back(Point(lat, lon));
- }
- else if (_waypoints.active() && (path == "/gpx/wpt"))
- {
- double lat = getDoubleAttribute(attributes, "lat");
- double lon = getDoubleAttribute(attributes, "lon");
- _points.push_back(Point(lat, lon));
- }
- }
- virtual void text(const std::string &, const std::string &)
- {
- }
- virtual void endElement(const std::string &path, const std::string &)
- {
- if ((_tracks.active() && (path == "/gpx/trk/trkseg")) ||
- (_routes.active() && (path == "/gpx/rte")))
- {
- _lines.push_back(_line);
- }
- }
- private:
- arg::Arguments _arguments;
- arg::Argument _waypoints;
- arg::Argument _tracks;
- arg::Argument _routes;
- arg::Argument _mode;
- arg::Argument _number;
- arg::Argument _simplify;
- XMLParser _xmlParser;
- // Types
- struct Point
- {
- Point(double lat, double lon)
- {
- _lat = lat;
- _lon = lon;
- }
- double _lat;
- double _lon;
- };
- typedef std::vector<Point> Line;
- // Members
- Line _line;
- std::vector<Line> _lines;
- std::vector<Point> _points;
- std::string _indent;
- };
- }
- // -- Main program ------------------------------------------------------------
- int main(int argc, char *argv[])
- {
- gpxtools::GPXJson gpxJson;
- if (gpxJson.parseArguments(argc, argv))
- {
- gpxJson.simplify();
- gpxJson.outputJson(std::cout);
- return 0;
- }
- return 1;
- }
|