[core] Conversion to DMS format. Tests.

This commit is contained in:
Dmitry Kunin 2013-06-21 15:59:38 +03:00 committed by Alex Zolotarev
parent f014062acb
commit a453aa087e
3 changed files with 79 additions and 0 deletions

View file

@ -33,3 +33,28 @@ UNIT_TEST(Measurement_Smoke)
TEST_EQUAL(s, arr[i].second, (arr[i]));
}
}
UNIT_TEST(LatLonToDMS_Origin)
{
TEST_EQUAL(FormatLatLonAsDMS(0, 0, true), "00°0000″ 00°0000″", ());
TEST_EQUAL(FormatLatLonAsDMS(0, 0, false), "00°0000″ 00°0000″", ());
}
UNIT_TEST(LatLonToDMS_Rounding)
{
// Here and after data is from Wiki: http://bit.ly/datafotformatingtest
// Boston
TEST_EQUAL(FormatLatLonAsDMS(42.358056, -71.063611, true), "42°2129″N 71°0349″W", ());
// Minsk
TEST_EQUAL(FormatLatLonAsDMS(53.916667, 27.55, true), "53°5500″N 27°3300″E", ());
// Rio
TEST_EQUAL(FormatLatLonAsDMS(-22.908333, -43.196389, true), "22°5430″S 43°1147″W", ());
}
UNIT_TEST(LatLonToDMS_NoRounding)
{
// Paris
TEST_EQUAL(FormatLatLonAsDMS(48.8567, 2.3508, false), "48°5124.12″N 02°2102.88″E", ());
// Vatican
TEST_EQUAL(FormatLatLonAsDMS(41.904, 12.453, false), "41°5414.4″N 12°2710.8″E", ());
}

View file

@ -53,4 +53,53 @@ bool FormatDistance(double m, string & res)
}
}
string FormatLatLonAsDMSImpl(string const & posPost, string const & negPost,
double value,bool roundSec)
{
double i = 0.0;
double d = 0.0;
string postfix;
ostringstream sstream;
sstream << setfill('0');
// Degreess
d = modf(fabs(value), &i);
sstream << setw(2) << i << "°";
// Minutes
d = modf(d * 60, &i);
sstream << setw(2) << i << "";
// Seconds
if (roundSec)
{
d = modf(round(d * 60), &i);
sstream << setw(2) << i;
}
else
{
d = modf(d * 60, &i);
sstream << setw(2) << setprecision(2) << i;
if (d > 1e-5)
{
ostringstream tstream;
tstream << setprecision(4) << d;
string dStr = tstream.str().substr(1, 5);
sstream << dStr;
}
}
sstream << "";
if (value > 0)
postfix = posPost;
else if (value < 0)
postfix = negPost;
sstream << postfix;
return sstream.str();
}
string FormatLatLonAsDMS(double lat, double lon, bool roundSecToInt)
{
return FormatLatLonAsDMSImpl("N", "S", lat, roundSecToInt) + " " + FormatLatLonAsDMSImpl("E", "W", lon, roundSecToInt);
}
} // namespace MeasurementUtils

View file

@ -1,6 +1,9 @@
#pragma once
#include "../std/string.hpp"
#include "../std/cmath.hpp"
#include "../std/iomanip.hpp"
#include "../std/sstream.hpp"
namespace MeasurementUtils
{
@ -20,4 +23,6 @@ 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);
string FormatLatLonAsDMS(double lat, double lon, bool roundSecToInt);
}