Utility functions

This commit is contained in:
Alex Zolotarev 2014-06-24 23:48:53 -10:00 committed by Alex Zolotarev
parent ad25b95f90
commit b2f49c86c5
3 changed files with 63 additions and 0 deletions

View file

@ -64,3 +64,18 @@ UNIT_TEST(LatLonToDMS_NoRounding)
TEST_EQUAL(FormatLatLonAsDMS(21.981112, -159.371112, 2), "21°5852″N 159°2216″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", ());
}

View file

@ -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

View file

@ -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);