From b2f49c86c5c4815ef77fe2659bf9b719078b2b1b Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Tue, 24 Jun 2014 23:48:53 -1000 Subject: [PATCH] Utility functions --- map/map_tests/measurement_tests.cpp | 15 ++++++++++ map/measurement_utils.cpp | 43 +++++++++++++++++++++++++++++ map/measurement_utils.hpp | 5 ++++ 3 files changed, 63 insertions(+) diff --git a/map/map_tests/measurement_tests.cpp b/map/map_tests/measurement_tests.cpp index 429e87650b..ed39d65c52 100644 --- a/map/map_tests/measurement_tests.cpp +++ b/map/map_tests/measurement_tests.cpp @@ -64,3 +64,18 @@ UNIT_TEST(LatLonToDMS_NoRounding) TEST_EQUAL(FormatLatLonAsDMS(21.981112, -159.371112, 2), "21°58′52″N 159°22′16″W", ()); } + +UNIT_TEST(FormatAltitude) +{ + Settings::Set("Units", Settings::Foot); + TEST_EQUAL(FormatAltitude(10000), "32808ft", ()); + Settings::Set("Units", Settings::Metric); + TEST_EQUAL(FormatAltitude(5), "5m", ()); +} + +UNIT_TEST(FormatSpeed) +{ + Settings::Set("Units", Settings::Metric); + TEST_EQUAL(FormatSpeed(10), "36km/h", ()); + TEST_EQUAL(FormatSpeed(1), "3.6km/h", ()); +} diff --git a/map/measurement_utils.cpp b/map/measurement_utils.cpp index 45ff88ac09..a182078d27 100644 --- a/map/measurement_utils.cpp +++ b/map/measurement_utils.cpp @@ -124,4 +124,47 @@ string FormatMercator(m2::PointD const & mercator, int dac) return FormatLatLon(MercatorBounds::YToLat(mercator.y), MercatorBounds::XToLon(mercator.x), dac); } +string FormatAltitude(double altitudeInMeters) +{ + using namespace Settings; + Units u = Metric; + (void)Settings::Get("Units", u); + + ostringstream sstream; + sstream << std::fixed << setprecision(0); + /// @todo Put string units resources. + switch (u) + { + case Yard: sstream << MetersToYards(altitudeInMeters) << "yd"; break; + case Foot: sstream << MetersToFeet(altitudeInMeters) << "ft"; break; + default: sstream << altitudeInMeters << "m"; break; + } + return sstream.str(); +} + +string FormatSpeed(double metersPerSecond) +{ + using namespace Settings; + Units u = Metric; + (void)Settings::Get("Units", u); + + double perHour; + string res; + + /// @todo Put string units resources. + switch (u) + { + case Yard: + case Foot: + perHour = metersPerSecond * 3600. / 1609.344; + res = ToStringPrecision(perHour, perHour >= 10.0 ? 0 : 1) + "mph"; + break; + default: + perHour = metersPerSecond * 3600. / 1000.; + res = ToStringPrecision(perHour, perHour >= 10.0 ? 0 : 1) + "km/h"; + break; + } + return res; +} + } // namespace MeasurementUtils diff --git a/map/measurement_utils.hpp b/map/measurement_utils.hpp index 28d41a9ac3..b2dddcef70 100644 --- a/map/measurement_utils.hpp +++ b/map/measurement_utils.hpp @@ -22,6 +22,11 @@ inline double YardToMiles(double yd) { return yd * 1760; } /// @return should be direction arrow drawed? false if distance is to small (< 1.0) bool FormatDistance(double m, string & res); +/// We always use meters and feet/yards for altitude +string FormatAltitude(double altitudeInMeters); +/// km/h or mph +string FormatSpeed(double metersPerSecond); + /// @param[in] dac Digits after comma in seconds. /// Use dac == 3 for our common conversions. string FormatLatLonAsDMS(double lat, double lon, int dac = 3);