diff --git a/.gitignore b/.gitignore index c22aaeb..3991c1d 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,6 @@ Makefile* # documentation /doc + +# clang-format +/.clang-format diff --git a/angle.cpp b/angle.cpp index 133b7f6..e003816 100644 --- a/angle.cpp +++ b/angle.cpp @@ -3,10 +3,10 @@ #include #include -#include -#include -#include #include +#include +#include +#include #ifndef M_PI #define M_PI 3.14159265359 @@ -16,16 +16,15 @@ using namespace std; using namespace ApplicationUtilities; using namespace ConversionUtilities; -Angle::Angle() : - m_val(0) +Angle::Angle() + : m_val(0) { } -Angle::Angle(double value, AngularMeasure measure) : - m_val(0) +Angle::Angle(double value, AngularMeasure measure) + : m_val(0) { - switch(measure) - { + switch (measure) { case AngularMeasure::Radian: m_val = value; break; @@ -34,31 +33,30 @@ Angle::Angle(double value, AngularMeasure measure) : } } -Angle::Angle(const string &value, AngularMeasure measure) : - m_val(0) +Angle::Angle(const string &value, AngularMeasure measure) + : m_val(0) { - switch(measure) { + switch (measure) { case AngularMeasure::Radian: m_val += stringToNumber(value); break; - case AngularMeasure::Degree: - { + case AngularMeasure::Degree: { string::size_type mpos, spos = string::npos; mpos = value.find(':'); - if(mpos == string::npos) + if (mpos == string::npos) m_val += stringToNumber(value); - else if(mpos >= (value.length() - 1)) + else if (mpos >= (value.length() - 1)) throw Failure("excepted minutes after ':' in " + value); else { m_val += stringToNumber(value.substr(0, mpos)); spos = value.find(':', mpos + 1); - if(spos == string::npos) + if (spos == string::npos) m_val += stringToNumber(value.substr(mpos + 1)) / 60.0; - else if(spos >= (value.length() - 1)) + else if (spos >= (value.length() - 1)) throw Failure("excepted seconds after second ':'' in " + value); else m_val += (stringToNumber(value.substr(mpos + 1, spos - mpos - 1)) / 60.0) - + (stringToNumber(value.substr(spos + 1)) / 3600.0); + + (stringToNumber(value.substr(spos + 1)) / 3600.0); } m_val = m_val * M_PI / 180.0; break; @@ -83,14 +81,18 @@ bool Angle::isNull() const void Angle::adjust0To360() { - while(m_val < 0) m_val += 2.0 * M_PI; - while(m_val > 2.0 * M_PI) m_val -= 2.0 * M_PI; + while (m_val < 0) + m_val += 2.0 * M_PI; + while (m_val > 2.0 * M_PI) + m_val -= 2.0 * M_PI; } void Angle::adjust180To180() { - while(m_val > M_PI) m_val -= 2.0 * M_PI; - while(m_val < -M_PI) m_val += 2.0 * M_PI; + while (m_val > M_PI) + m_val -= 2.0 * M_PI; + while (m_val < -M_PI) + m_val += 2.0 * M_PI; } void Angle::reverse() @@ -104,19 +106,19 @@ string Angle::toString(OutputForm format) const stringstream sstream(stringstream::in | stringstream::out); sstream << setprecision(9); double intpart, fractpart; - switch(format) { + switch (format) { case OutputForm::Degrees: sstream << degreeValue(); break; case OutputForm::Minutes: - if(degreeValue() < 0) + if (degreeValue() < 0) sstream << "-"; fractpart = modf(fabs(degreeValue()), &intpart); fractpart *= 60; sstream << intpart << ":" << fractpart; break; case OutputForm::Seconds: - if(degreeValue() < 0) + if (degreeValue() < 0) sstream << "-"; fractpart = modf(fabs(degreeValue()), &intpart); sstream << intpart << ":"; @@ -131,33 +133,33 @@ string Angle::toString(OutputForm format) const return sstream.str(); } -bool Angle::operator ==(const Angle &other) const +bool Angle::operator==(const Angle &other) const { return m_val == other.m_val; } -bool Angle::operator !=(const Angle &other) const +bool Angle::operator!=(const Angle &other) const { return m_val != other.m_val; } -Angle Angle::operator +(const Angle &other) const +Angle Angle::operator+(const Angle &other) const { return Angle(m_val + other.m_val); } -Angle Angle::operator -(const Angle &other) const +Angle Angle::operator-(const Angle &other) const { return Angle(m_val - other.m_val); } -Angle &Angle::operator +=(const Angle &other) +Angle &Angle::operator+=(const Angle &other) { m_val += other.m_val; return *this; } -Angle &Angle::operator -=(const Angle &other) +Angle &Angle::operator-=(const Angle &other) { m_val -= other.m_val; return *this; diff --git a/angle.h b/angle.h index f9e4ec8..bce4033 100644 --- a/angle.h +++ b/angle.h @@ -3,22 +3,11 @@ #include -class Angle -{ +class Angle { public: - enum class AngularMeasure - { - Radian, - Degree - }; + enum class AngularMeasure { Radian, Degree }; - enum class OutputForm - { - Degrees, - Minutes, - Seconds, - Radians - }; + enum class OutputForm { Degrees, Minutes, Seconds, Radians }; Angle(); Angle(double value, AngularMeasure measure = AngularMeasure::Radian); @@ -32,12 +21,13 @@ public: std::string toString() const; std::string toString(OutputForm format) const; - bool operator ==(const Angle &other) const; - bool operator !=(const Angle &other) const; - Angle operator +(const Angle &other) const; - Angle operator -(const Angle &other) const; - Angle &operator +=(const Angle &other); - Angle &operator -=(const Angle &other); + bool operator==(const Angle &other) const; + bool operator!=(const Angle &other) const; + Angle operator+(const Angle &other) const; + Angle operator-(const Angle &other) const; + Angle &operator+=(const Angle &other); + Angle &operator-=(const Angle &other); + private: double m_val; }; diff --git a/location.cpp b/location.cpp index ddc19a2..85cc5d9 100644 --- a/location.cpp +++ b/location.cpp @@ -3,65 +3,69 @@ #include #include -#include -#include #include +#include +#include using namespace std; using namespace ApplicationUtilities; using namespace ConversionUtilities; // WGS84 Parameters -#define WGS84_A 6378137.0 // major axis -#define WGS84_B 6356752.31424518 // minor axis -#define WGS84_F 0.0033528107 // ellipsoid flattening -#define WGS84_E 0.0818191908 // first eccentricity -#define WGS84_EP 0.0820944379 // second eccentricity +#define WGS84_A 6378137.0 // major axis +#define WGS84_B 6356752.31424518 // minor axis +#define WGS84_F 0.0033528107 // ellipsoid flattening +#define WGS84_E 0.0818191908 // first eccentricity +#define WGS84_EP 0.0820944379 // second eccentricity // UTM Parameters -#define UTM_K0 0.9996 // scale factor -#define UTM_FE 500000.0 // false easting -#define UTM_FN_N 0.0 // false northing, northern hemisphere -#define UTM_FN_S 10000000.0 // false northing, southern hemisphere -#define UTM_E2 (WGS84_E * WGS84_E) // e^2 -#define UTM_E4 (UTM_E2 * UTM_E2) // e^4 -#define UTM_E6 (UTM_E4 * UTM_E2) // e^6 -#define UTM_EP2 (UTM_E2 / (1 - UTM_E2)) // e'^2 +#define UTM_K0 0.9996 // scale factor +#define UTM_FE 500000.0 // false easting +#define UTM_FN_N 0.0 // false northing, northern hemisphere +#define UTM_FN_S 10000000.0 // false northing, southern hemisphere +#define UTM_E2 (WGS84_E * WGS84_E) // e^2 +#define UTM_E4 (UTM_E2 * UTM_E2) // e^4 +#define UTM_E6 (UTM_E4 * UTM_E2) // e^6 +#define UTM_EP2 (UTM_E2 / (1 - UTM_E2)) // e'^2 -Location::Location() : - m_lat(0.0), - m_lon(0.0), - m_ele(0.0) -{} +Location::Location() + : m_lat(0.0) + , m_lon(0.0) + , m_ele(0.0) +{ +} -Location::Location(const Angle &latitude, const Angle &lon) : - m_lat(latitude), - m_lon(lon), - m_ele(0.0) -{} +Location::Location(const Angle &latitude, const Angle &lon) + : m_lat(latitude) + , m_lon(lon) + , m_ele(0.0) +{ +} -Location::Location(const string &lat, const string &lon, Angle::AngularMeasure measure) : - m_lat(Angle(lat, measure)), - m_lon(Angle(lon, measure)), - m_ele(0.0) -{} +Location::Location(const string &lat, const string &lon, Angle::AngularMeasure measure) + : m_lat(Angle(lat, measure)) + , m_lon(Angle(lon, measure)) + , m_ele(0.0) +{ +} -Location::Location(const string &latitudeAndLongitude, Angle::AngularMeasure measure) : - m_ele(0.0) +Location::Location(const string &latitudeAndLongitude, Angle::AngularMeasure measure) + : m_ele(0.0) { string::size_type dpos = latitudeAndLongitude.find(','); - if(dpos == string::npos) + if (dpos == string::npos) throw Failure("Pair of coordinates (latitude and longitude) required."); - else if(dpos >= (latitudeAndLongitude.length() - 1)) + else if (dpos >= (latitudeAndLongitude.length() - 1)) throw Failure("No second longitude following after comma."); - else if(latitudeAndLongitude.find(',', dpos + 1) != string::npos) + else if (latitudeAndLongitude.find(',', dpos + 1) != string::npos) throw Failure("More then 2 coordinates given."); m_lat = Angle(latitudeAndLongitude.substr(0, dpos), measure); m_lon = Angle(latitudeAndLongitude.substr(dpos + 1), measure); } Location::~Location() -{} +{ +} string Location::toString(Angle::OutputForm form) const { @@ -138,17 +142,20 @@ void Location::computeUtmWgs4Coordinates(int &zone, char &zoneDesignator, double zone = int((lond + 180) / 6) + 1; - if(latd >= 56.0 && latd < 64.0 && lond >= 3.0 && lond < 12.0) + if (latd >= 56.0 && latd < 64.0 && lond >= 3.0 && lond < 12.0) zone = 32; // Special zones for Svalbard - if(latd >= 72.0 && latd < 84.0) - { - if(lond >= 0.0 && lond < 9.0) zone = 31; - else if(lond >= 9.0 && lond < 21.0) zone = 33; - else if(lond >= 21.0 && lond < 33.0) zone = 35; - else if(lond >= 33.0 && lond < 42.0) zone = 37; - } + if (latd >= 72.0 && latd < 84.0) { + if (lond >= 0.0 && lond < 9.0) + zone = 31; + else if (lond >= 9.0 && lond < 21.0) + zone = 33; + else if (lond >= 21.0 && lond < 33.0) + zone = 35; + else if (lond >= 33.0 && lond < 42.0) + zone = 37; + } zoneDesignator = computeUtmZoneDesignator(); @@ -159,26 +166,21 @@ void Location::computeUtmWgs4Coordinates(int &zone, char &zoneDesignator, double double N = a / sqrt(1 - eccSquared * sin(latr) * sin(latr)); double T = tan(latr) * tan(latr); double C = eccPrimeSquared * cos(latr) * cos(latr); - double A = cos(latr) * (lonr-lonOriginr); - double M = a * ((1 - eccSquared / 4 - 3 * eccSquared * eccSquared / 64 - - 5 * eccSquared * eccSquared * eccSquared / 256) * latr - - (3 * eccSquared / 8 + 3 * eccSquared*eccSquared / 32 - + 45 * eccSquared * eccSquared * eccSquared / 1024)*sin(2 * latr) - + (15 * eccSquared * eccSquared / 256 - + 45 * eccSquared * eccSquared*eccSquared / 1024) * sin(4 * latr) - - (35 * eccSquared * eccSquared * eccSquared/3072) * sin(6 * latr)); + double A = cos(latr) * (lonr - lonOriginr); + double M = a * ((1 - eccSquared / 4 - 3 * eccSquared * eccSquared / 64 - 5 * eccSquared * eccSquared * eccSquared / 256) * latr + - (3 * eccSquared / 8 + 3 * eccSquared * eccSquared / 32 + 45 * eccSquared * eccSquared * eccSquared / 1024) * sin(2 * latr) + + (15 * eccSquared * eccSquared / 256 + 45 * eccSquared * eccSquared * eccSquared / 1024) * sin(4 * latr) + - (35 * eccSquared * eccSquared * eccSquared / 3072) * sin(6 * latr)); - east = static_cast - (k0 * N * (A + (1 - T + C) * A * A * A / 6 - + (5 - 18 * T + T * T + 72 * C - 58 * eccPrimeSquared) * A * A * A * A * A / 120) - + 500000.0); + east = static_cast( + k0 * N * (A + (1 - T + C) * A * A * A / 6 + (5 - 18 * T + T * T + 72 * C - 58 * eccPrimeSquared) * A * A * A * A * A / 120) + 500000.0); - north = static_cast - (k0 * (M + N * tan(latr) - * (A * A / 2 + (5 - T + 9 * C + 4 * C * C) * A * A * A *A / 24 - + (61 - 58 * T + T * T + 600 * C - 330 * eccPrimeSquared) * A * A * A * A * A * A / 720))); + north = static_cast( + k0 * (M + + N * tan(latr) * (A * A / 2 + (5 - T + 9 * C + 4 * C * C) * A * A * A * A / 24 + + (61 - 58 * T + T * T + 600 * C - 330 * eccPrimeSquared) * A * A * A * A * A * A / 720))); - if(latd < 0) + if (latd < 0) north += 10000000.0; } @@ -191,27 +193,25 @@ Location Location::midpoint(const Location &location1, const Location &location2 double lond = lon2 - lon1; double x = cos(lat2) * cos(lond); double y = cos(lat2) * sin(lond); - return Location( - Angle(atan2(sin(lat1) + sin(lat2), sqrt((cos(lat1) + x) * (cos(lat1) + x) + y * y))), - Angle(lon1 + atan2(y, cos(lat1) + x))); + return Location(Angle(atan2(sin(lat1) + sin(lat2), sqrt((cos(lat1) + x) * (cos(lat1) + x) + y * y))), Angle(lon1 + atan2(y, cos(lat1) + x))); } double Location::trackLength(const std::vector &track, bool circle) { - if(track.size() < 2) + if (track.size() < 2) throw Failure("At least two locations are required to calculate a distance."); const Location *location1 = &track.at(0); const Location *location2 = &track.at(1); double distance = location1->distanceTo(*location2); - for(std::vector::const_iterator i = track.cbegin() + 2, end = track.cend(); i != end; ++i) { + for (std::vector::const_iterator i = track.cbegin() + 2, end = track.cend(); i != end; ++i) { location1 = location2; location2 = &(*i); distance += location1->distanceTo(*location2); } - if(circle) + if (circle) distance += track.front().distanceTo(track.back()); return distance; @@ -230,35 +230,56 @@ Angle Location::angularDistance(double distance) char Location::computeUtmZoneDesignator() const { double l = m_lat.degreeValue(); - if ((84 >= l) && (l >= 72)) return 'X'; - else if ((72 > l) && (l >= 64)) return 'W'; - else if ((64 > l) && (l >= 56)) return 'V'; - else if ((56 > l) && (l >= 48)) return 'U'; - else if ((48 > l) && (l >= 40)) return 'T'; - else if ((40 > l) && (l >= 32)) return 'S'; - else if ((32 > l) && (l >= 24)) return 'R'; - else if ((24 > l) && (l >= 16)) return 'Q'; - else if ((16 > l) && (l >= 8)) return 'P'; - else if (( 8 > l) && (l >= 0)) return 'N'; - else if (( 0 > l) && (l >= -8)) return 'M'; - else if ((-8 > l) && (l >= -16)) return 'L'; - else if((-16 > l) && (l >= -24)) return 'K'; - else if((-24 > l) && (l >= -32)) return 'J'; - else if((-32 > l) && (l >= -40)) return 'H'; - else if((-40 > l) && (l >= -48)) return 'G'; - else if((-48 > l) && (l >= -56)) return 'F'; - else if((-56 > l) && (l >= -64)) return 'E'; - else if((-64 > l) && (l >= -72)) return 'D'; - else if((-72 > l) && (l >= -80)) return 'C'; - else return '\0'; + if ((84 >= l) && (l >= 72)) + return 'X'; + else if ((72 > l) && (l >= 64)) + return 'W'; + else if ((64 > l) && (l >= 56)) + return 'V'; + else if ((56 > l) && (l >= 48)) + return 'U'; + else if ((48 > l) && (l >= 40)) + return 'T'; + else if ((40 > l) && (l >= 32)) + return 'S'; + else if ((32 > l) && (l >= 24)) + return 'R'; + else if ((24 > l) && (l >= 16)) + return 'Q'; + else if ((16 > l) && (l >= 8)) + return 'P'; + else if ((8 > l) && (l >= 0)) + return 'N'; + else if ((0 > l) && (l >= -8)) + return 'M'; + else if ((-8 > l) && (l >= -16)) + return 'L'; + else if ((-16 > l) && (l >= -24)) + return 'K'; + else if ((-24 > l) && (l >= -32)) + return 'J'; + else if ((-32 > l) && (l >= -40)) + return 'H'; + else if ((-40 > l) && (l >= -48)) + return 'G'; + else if ((-48 > l) && (l >= -56)) + return 'F'; + else if ((-56 > l) && (l >= -64)) + return 'E'; + else if ((-64 > l) && (l >= -72)) + return 'D'; + else if ((-72 > l) && (l >= -80)) + return 'C'; + else + return '\0'; } void Location::setValueByProvidedUtmWgs4Coordinates(const string &utmWgs4Coordinates) { string::size_type epos = utmWgs4Coordinates.find('E'); - if(epos != 0 && epos != string::npos) { + if (epos != 0 && epos != string::npos) { string::size_type npos = utmWgs4Coordinates.find('N', epos); - if(npos < (utmWgs4Coordinates.length() - 1) && npos != string::npos) { + if (npos < (utmWgs4Coordinates.length() - 1) && npos != string::npos) { int zone = stringToNumber(utmWgs4Coordinates.substr(0, epos - 1)); char zoneDesignator = utmWgs4Coordinates.at(epos - 1); double east = stringToNumber(utmWgs4Coordinates.substr(epos + 1, npos - epos - 1)); @@ -281,21 +302,19 @@ void Location::setValueByProvidedUtmWgs4Coordinates(int zone, char zoneDesignato double x = easting - 500000.0; //remove 500,000 meter offset for longitude double y = northing; - if((zoneDesignator - 'N') < 0) + if ((zoneDesignator - 'N') < 0) //remove 10,000,000 meter offset used for southern hemisphere y -= 10000000.0; //+3 puts origin in middle of zone double longOriginr = Angle((zone - 1) * 6 - 180 + 3, Angle::AngularMeasure::Degree).radianValue(); - eccPrimeSquared = (eccSquared)/(1-eccSquared); + eccPrimeSquared = (eccSquared) / (1 - eccSquared); double M = y / k0; - double mu = M / (a * (1 - eccSquared / 4 - 3 * eccSquared * eccSquared / 64 - -5 * eccSquared * eccSquared * eccSquared / 256)); + double mu = M / (a * (1 - eccSquared / 4 - 3 * eccSquared * eccSquared / 64 - 5 * eccSquared * eccSquared * eccSquared / 256)); - double phi1Rad = mu + ((3 * e1 / 2 - 27 * e1 * e1 * e1 / 32) * sin(2 * mu) - + (21 * e1 * e1 / 16 - 55 * e1 * e1 * e1 * e1 / 32) * sin(4 * mu) - + (151 * e1 * e1 * e1 / 96) * sin(6 * mu)); + double phi1Rad = mu + ((3 * e1 / 2 - 27 * e1 * e1 * e1 / 32) * sin(2 * mu) + (21 * e1 * e1 / 16 - 55 * e1 * e1 * e1 * e1 / 32) * sin(4 * mu) + + (151 * e1 * e1 * e1 / 96) * sin(6 * mu)); double N1 = a / sqrt(1 - eccSquared * sin(phi1Rad) * sin(phi1Rad)); double T1 = tan(phi1Rad) * tan(phi1Rad); @@ -303,16 +322,14 @@ void Location::setValueByProvidedUtmWgs4Coordinates(int zone, char zoneDesignato double R1 = a * (1 - eccSquared) / pow(1 - eccSquared * sin(phi1Rad) * sin(phi1Rad), 1.5); double D = x / (N1 * k0); - m_lat = Angle(phi1Rad - ((N1 * tan(phi1Rad) / R1) - * (D * D / 2 - -(5 + 3 * T1 + 10 * C1 - 4 * C1 * C1 - 9 * eccPrimeSquared) * D * D * D * D / 24 - +(61 + 90 * T1 + 298 * C1 + 45 * T1 * T1 - 252 * eccPrimeSquared - -3 * C1 * C1) * D * D * D * D * D * D / 720))); + m_lat = Angle(phi1Rad - ((N1 * tan(phi1Rad) / R1) * (D * D / 2 - (5 + 3 * T1 + 10 * C1 - 4 * C1 * C1 - 9 * eccPrimeSquared) * D * D * D * D / 24 + + (61 + 90 * T1 + 298 * C1 + 45 * T1 * T1 - 252 * eccPrimeSquared - 3 * C1 * C1) * D * D + * D * D * D * D / 720))); m_lat.adjust180To180(); - m_lon = Angle(((D - (1 + 2 * T1 + C1) * D * D * D / 6 - +(5 - 2 * C1 + 28 * T1 - 3 * C1 * C1 + 8 * eccPrimeSquared + 24 * T1 * T1) - * D * D * D * D * D / 120) - / cos(phi1Rad)) + longOriginr); + m_lon = Angle( + ((D - (1 + 2 * T1 + C1) * D * D * D / 6 + (5 - 2 * C1 + 28 * T1 - 3 * C1 * C1 + 8 * eccPrimeSquared + 24 * T1 * T1) * D * D * D * D * D / 120) + / cos(phi1Rad)) + + longOriginr); m_lon.adjust180To180(); } diff --git a/location.h b/location.h index d19e2ab..df07eff 100644 --- a/location.h +++ b/location.h @@ -3,17 +3,12 @@ #include "./angle.h" -#include #include +#include -class Location -{ +class Location { public: - enum class GeographicCoordinateSystem - { - LatitudeAndLongitude, - UTMWGS84 - }; + enum class GeographicCoordinateSystem { LatitudeAndLongitude, UTMWGS84 }; Location(); Location(const Angle &lat, const Angle &lon); @@ -42,6 +37,7 @@ public: static double trackLength(const std::vector &track, bool circle = false); static double earthRadius(); static Angle angularDistance(double distance); + protected: private: Angle m_lat; diff --git a/main.cpp b/main.cpp index 380e18d..6b9bd51 100644 --- a/main.cpp +++ b/main.cpp @@ -8,9 +8,9 @@ #include #include -#include -#include #include +#include +#include using namespace std; using namespace ConversionUtilities; @@ -22,7 +22,7 @@ SystemForLocations inputSystemForLocations = SystemForLocations::LatitudeLongitu SystemForLocations outputSystemForLocations = SystemForLocations::LatitudeLongitude; int main(int argc, char *argv[]) -{ +{ try { SET_APPLICATION_INFO; @@ -37,20 +37,24 @@ int main(int argc, char *argv[]) distance.appendValueName("location 1"); distance.appendValueName("location 2"); - Argument trackLength("track-length", 't', "Computes the approximate length in meters of a track given by a file containing trackpoints separated by new lines."); + Argument trackLength("track-length", 't', + "Computes the approximate length in meters of a track given by a file containing trackpoints separated by new lines."); Argument fileArg("file", 'f', "Specifies the file containing the track points"); fileArg.setRequiredValueCount(1); fileArg.appendValueName("path"); fileArg.setRequired(true); - Argument circle("circle", '\0', "If present the distance between the first and the last trackpoints will be added to the total track length."); - trackLength.setSubArguments({&fileArg, &circle}); + Argument circle( + "circle", '\0', "If present the distance between the first and the last trackpoints will be added to the total track length."); + trackLength.setSubArguments({ &fileArg, &circle }); - Argument bearing("bearing", 'b', "Computes the approximate initial bearing East of true North when traveling along the shortest path between the given locations."); + Argument bearing("bearing", 'b', + "Computes the approximate initial bearing East of true North when traveling along the shortest path between the given locations."); bearing.setRequiredValueCount(2); bearing.appendValueName("location 1"); bearing.appendValueName("location 2"); - Argument fbearing("final-bearing", '\0', "Computes the approximate final bearing East of true North when traveling along the shortest path between the given locations."); + Argument fbearing("final-bearing", '\0', + "Computes the approximate final bearing East of true North when traveling along the shortest path between the given locations."); fbearing.setRequiredValueCount(2); fbearing.appendValueName("location 1"); fbearing.appendValueName("location 2"); @@ -66,26 +70,31 @@ int main(int argc, char *argv[]) destination.appendValueName("distance"); destination.appendValueName("bearing"); - Argument gmapsLink("gmaps-link", '\0', "Generates a Google Maps link for all locations given by a file containing locations separated by new lines."); + Argument gmapsLink( + "gmaps-link", '\0', "Generates a Google Maps link for all locations given by a file containing locations separated by new lines."); gmapsLink.setRequiredValueCount(1); gmapsLink.appendValueName("path"); - Argument inputAngularMeasureArg("input-angular-measure", 'i', "Use this option to specify the angular measure you use to provide angles (degree or radian; default is degree)."); + Argument inputAngularMeasureArg("input-angular-measure", 'i', + "Use this option to specify the angular measure you use to provide angles (degree or radian; default is degree)."); inputAngularMeasureArg.setRequiredValueCount(1); inputAngularMeasureArg.appendValueName("angular measure"); inputAngularMeasureArg.setCombinable(true); - Argument outputFormForAnglesArg("output-angle-form", 'o', "Use this option to specify the output form for angles (degrees, minutes, seconds or radians; default is degrees)."); + Argument outputFormForAnglesArg("output-angle-form", 'o', + "Use this option to specify the output form for angles (degrees, minutes, seconds or radians; default is degrees)."); outputFormForAnglesArg.setRequiredValueCount(1); outputFormForAnglesArg.appendValueName("form"); outputFormForAnglesArg.setCombinable(true); - Argument inputSystemForLocationsArg("input-location-system", '\0', "Use this option to specify the geographic system you use to provide locations (latitude&longitue or UTM-WGS84)."); + Argument inputSystemForLocationsArg("input-location-system", '\0', + "Use this option to specify the geographic system you use to provide locations (latitude&longitue or UTM-WGS84)."); inputSystemForLocationsArg.setRequiredValueCount(1); inputSystemForLocationsArg.appendValueName("system"); inputSystemForLocationsArg.setCombinable(true); - Argument outputSystemForLocationsArg("output-location-system", '\0', "Use this option to specify which geographic system is used to display locations (latitude&longitue or UTM-WGS84)."); + Argument outputSystemForLocationsArg("output-location-system", '\0', + "Use this option to specify which geographic system is used to display locations (latitude&longitue or UTM-WGS84)."); outputSystemForLocationsArg.setRequiredValueCount(1); outputSystemForLocationsArg.appendValueName("system"); outputSystemForLocationsArg.setCombinable(true); @@ -93,14 +102,15 @@ int main(int argc, char *argv[]) HelpArgument help(argparser); Argument version("version", 'v', "Shows the version of this application."); - argparser.setMainArguments({&help, &convert, &distance, &trackLength, &bearing, &fbearing, &midpoint, &destination, &gmapsLink, &inputAngularMeasureArg, &outputFormForAnglesArg, &inputSystemForLocationsArg, &outputSystemForLocationsArg, &version}); + argparser.setMainArguments({ &help, &convert, &distance, &trackLength, &bearing, &fbearing, &midpoint, &destination, &gmapsLink, + &inputAngularMeasureArg, &outputFormForAnglesArg, &inputSystemForLocationsArg, &outputSystemForLocationsArg, &version }); argparser.parseArgs(argc, argv); - if(inputAngularMeasureArg.isPresent()) { + if (inputAngularMeasureArg.isPresent()) { const char *inputFormat = inputAngularMeasureArg.values().front(); - if(!strcmp(inputFormat, "radian")) { + if (!strcmp(inputFormat, "radian")) { inputAngularMeasure = Angle::AngularMeasure::Radian; - } else if(!strcmp(inputFormat, "degree")) { + } else if (!strcmp(inputFormat, "degree")) { inputAngularMeasure = Angle::AngularMeasure::Degree; } else { cerr << "Invalid angular measure given, see --help." << endl; @@ -108,15 +118,15 @@ int main(int argc, char *argv[]) } } - if(outputFormForAnglesArg.isPresent()) { + if (outputFormForAnglesArg.isPresent()) { const char *outputFormat = outputFormForAnglesArg.values().front(); - if(!strcmp(outputFormat, "degrees")) { + if (!strcmp(outputFormat, "degrees")) { outputFormForAngles = Angle::OutputForm::Degrees; - } else if(!strcmp(outputFormat, "minutes")) { + } else if (!strcmp(outputFormat, "minutes")) { outputFormForAngles = Angle::OutputForm::Minutes; - } else if(!strcmp(outputFormat, "seconds")) { + } else if (!strcmp(outputFormat, "seconds")) { outputFormForAngles = Angle::OutputForm::Seconds; - } else if(!strcmp(outputFormat, "radians")) { + } else if (!strcmp(outputFormat, "radians")) { outputFormForAngles = Angle::OutputForm::Radians; } else { cerr << "Invalid output form for angles given, see --help." << endl; @@ -124,11 +134,11 @@ int main(int argc, char *argv[]) } } - if(inputSystemForLocationsArg.isPresent()) { + if (inputSystemForLocationsArg.isPresent()) { const char *inputFormat = inputSystemForLocationsArg.values().front(); - if(!strcmp(inputFormat, "latitude&longitue")) { + if (!strcmp(inputFormat, "latitude&longitue")) { inputSystemForLocations = SystemForLocations::LatitudeLongitude; - } else if(!strcmp(inputFormat, "UTM-WGS84")) { + } else if (!strcmp(inputFormat, "UTM-WGS84")) { inputSystemForLocations = SystemForLocations::UTMWGS84; } else { cerr << "Invalid geographic coordinate system given, see --help." << endl; @@ -136,11 +146,11 @@ int main(int argc, char *argv[]) } } - if(outputSystemForLocationsArg.isPresent()) { + if (outputSystemForLocationsArg.isPresent()) { const char *outputSystem = outputSystemForLocationsArg.values().front(); - if(!strcmp(outputSystem, "latitude&longitue")) { + if (!strcmp(outputSystem, "latitude&longitue")) { outputSystemForLocations = SystemForLocations::LatitudeLongitude; - } else if(!strcmp(outputSystem, "UTM-WGS84")) { + } else if (!strcmp(outputSystem, "UTM-WGS84")) { outputSystemForLocations = SystemForLocations::UTMWGS84; } else { cerr << "Invalid geographic coordinate system given, see --help." << endl; @@ -149,40 +159,40 @@ int main(int argc, char *argv[]) } try { - if(help.isPresent()) { + if (help.isPresent()) { cout << endl; printAngleFormatInfo(cout); - } else if(version.isPresent()) { + } else if (version.isPresent()) { cout << APP_VERSION; - } else if(convert.isPresent()) { + } else if (convert.isPresent()) { printConversion(convert.values().front()); - } else if(distance.isPresent()) { + } else if (distance.isPresent()) { printDistance(distance.values()[0], distance.values()[1]); - } else if(trackLength.isPresent()) { + } else if (trackLength.isPresent()) { printTrackLength(fileArg.values().front(), circle.isPresent()); - } else if(bearing.isPresent()) { + } else if (bearing.isPresent()) { printBearing(bearing.values()[0], bearing.values()[1]); - } else if(fbearing.isPresent()) { + } else if (fbearing.isPresent()) { printFinalBearing(fbearing.values()[0], fbearing.values()[1]); - } else if(midpoint.isPresent()) { + } else if (midpoint.isPresent()) { printMidpoint(midpoint.values()[0], midpoint.values()[1]); - } else if(destination.isPresent()) { + } else if (destination.isPresent()) { printDestination(destination.values()[0], destination.values()[1], destination.values()[2]); - } else if(gmapsLink.isPresent()) { + } else if (gmapsLink.isPresent()) { printMapsLink(gmapsLink.values().front()); } else { cerr << "No arguments given. See --help for available commands."; } - } catch(const ConversionException &) { + } catch (const ConversionException &) { cerr << "The provided numbers couldn't be parsed correctly." << endl; cerr << endl; printAngleFormatInfo(cerr); - } catch(const Failure &ex) { + } catch (const Failure &ex) { cerr << "The provided locations/coordinates couldn't be parsed correctly: " << ex.what() << endl; cerr << endl; printAngleFormatInfo(cerr); } - } catch(const Failure &ex) { + } catch (const Failure &ex) { cerr << "Unable to parse arguments. " << ex.what() << endl << "See --help for available commands."; } @@ -190,15 +200,15 @@ int main(int argc, char *argv[]) return 0; } - Location locationFromString(const string &userInput) { - switch(inputSystemForLocations) { + switch (inputSystemForLocations) { case SystemForLocations::UTMWGS84: { Location l; l.setValueByProvidedUtmWgs4Coordinates(userInput); return l; - } default: + } + default: return Location(userInput, inputAngularMeasure); } } @@ -208,13 +218,13 @@ vector locationsFromFile(const string &path) // prepare reading fstream file; file.open(path, ios_base::in); - if(!file) + if (!file) IoUtilities::throwIoFailure(("Unable to open the file \"" + path + "\".").data()); file.exceptions(ios_base::badbit); string line; vector locations; - while(getline(file, line)) { - if(line.empty() || line.at(0) == '#') + while (getline(file, line)) { + if (line.empty() || line.at(0) == '#') continue; // skip empty lines and comments locations.push_back(locationFromString(line)); } @@ -230,7 +240,8 @@ void printAngleFormatInfo(ostream &os) os << "[+-]DDD.DDDDD\n"; os << "[+-]DDD:MM.MMMMM\n"; os << "[+-]DDD:MM:SS.SSSSS\n"; - os << "D indicates degrees, M indicates minutes of arc, and S indicates seconds of arc (1 minute = 1/60th of a degree, 1 second = 1/3600th of a degree).\n"; + os << "D indicates degrees, M indicates minutes of arc, and S indicates seconds of arc (1 minute = 1/60th of a degree, 1 second = 1/3600th of a " + "degree).\n"; os << "You can use the following form where R indicates radians, if you use --input-angle-measure radian:\n"; os << "[+-]RRR.RRRRR"; @@ -238,7 +249,7 @@ void printAngleFormatInfo(ostream &os) void printConversion(const string &coordinates) { - if(coordinates.find(',') == string::npos && coordinates.find('N') == string::npos && coordinates.find('E') == string::npos) + if (coordinates.find(',') == string::npos && coordinates.find('N') == string::npos && coordinates.find('E') == string::npos) cout << Angle(coordinates, inputAngularMeasure).toString(outputFormForAngles); else printLocation(locationFromString(coordinates)); @@ -246,13 +257,12 @@ void printConversion(const string &coordinates) void printDistance(const std::string &locationstr1, const std::string &locationstr2) { - printDistance(locationFromString(locationstr1) - .distanceTo(locationFromString(locationstr2))); + printDistance(locationFromString(locationstr1).distanceTo(locationFromString(locationstr2))); } void printDistance(double distance) { - if(distance > 1000) + if (distance > 1000) cout << (distance / 1000.0) << " km"; else cout << distance << " m"; @@ -264,7 +274,7 @@ void printTrackLength(const string &filePath, bool circle) vector locations(locationsFromFile(filePath)); printDistance(Location::trackLength(locations, circle)); cout << " (" << locations.size() << " trackpoints)"; - } catch(...) { + } catch (...) { const char *what = ::IoUtilities::catchIoFailure(); cout << "An IO failure occured when reading file from provided path: " << what << endl; } @@ -272,23 +282,17 @@ void printTrackLength(const string &filePath, bool circle) void printBearing(const string &locationstr1, const string &locationstr2) { - cout << locationFromString(locationstr1).initialBearingTo( - locationFromString(locationstr2)) - .toString(outputFormForAngles) << endl; + cout << locationFromString(locationstr1).initialBearingTo(locationFromString(locationstr2)).toString(outputFormForAngles) << endl; } void printFinalBearing(const string &locationstr1, const string &locationstr2) { - cout << locationFromString(locationstr1).finalBearingTo( - locationFromString(locationstr2)) - .toString(outputFormForAngles) << endl; + cout << locationFromString(locationstr1).finalBearingTo(locationFromString(locationstr2)).toString(outputFormForAngles) << endl; } void printMidpoint(const string &locationstr1, const string &locationstr2) { - printLocation(Location::midpoint( - locationFromString(locationstr1), - locationFromString(locationstr2))); + printLocation(Location::midpoint(locationFromString(locationstr1), locationFromString(locationstr2))); } void printDestination(const string &locationstr, const string &distancestr, const string &bearingstr) @@ -301,7 +305,7 @@ void printDestination(const string &locationstr, const string &distancestr, cons void printLocation(const Location &location) { - switch(outputSystemForLocations) { + switch (outputSystemForLocations) { case SystemForLocations::LatitudeLongitude: cout << location.toString(outputFormForAngles); break; @@ -311,21 +315,20 @@ void printLocation(const Location &location) } } - void printMapsLink(const string &filePath) { try { vector locations(locationsFromFile(filePath)); - if(locations.size() > 0) { + if (locations.size() > 0) { cout << "https://maps.google.de/maps?saddr="; const Angle::OutputForm outputForm = Angle::OutputForm::Degrees; cout << locations.front().toString(outputForm); - if(locations.size() > 1) { + if (locations.size() > 1) { cout << "&daddr="; cout << locations.at(1).toString(outputForm); } - if(locations.size() > 2) { - for(vector::const_iterator i = locations.cbegin() + 2, end = locations.cend(); i != end; ++i) { + if (locations.size() > 2) { + for (vector::const_iterator i = locations.cbegin() + 2, end = locations.cend(); i != end; ++i) { cout << "+to:"; cout << i->toString(outputForm); } @@ -334,7 +337,7 @@ void printMapsLink(const string &filePath) } else { throw Failure("At least one location is required to generate a link."); } - } catch(...) { + } catch (...) { const char *what = ::IoUtilities::catchIoFailure(); cout << "An IO failure occured when reading file from provided path: " << what << endl; } diff --git a/main.h b/main.h index b957b47..5f0d08c 100644 --- a/main.h +++ b/main.h @@ -8,11 +8,7 @@ #include #include -enum class SystemForLocations -{ - LatitudeLongitude, - UTMWGS84 -}; +enum class SystemForLocations { LatitudeLongitude, UTMWGS84 }; extern Angle::AngularMeasure inputAngularMeasure; extern Angle::OutputForm outputFormForAngles; @@ -35,5 +31,4 @@ void printDestination(const std::string &locationstr, const std::string &distanc void printLocation(const Location &location); void printMapsLink(const std::string &filePath); - #endif // MAIN_H_INCLUDED