forked from organicmaps/organicmaps
[core] Conversion to DMS format. Tests.
This commit is contained in:
parent
f014062acb
commit
a453aa087e
3 changed files with 79 additions and 0 deletions
|
@ -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°00′00″ 00°00′00″", ());
|
||||
TEST_EQUAL(FormatLatLonAsDMS(0, 0, false), "00°00′00″ 00°00′00″", ());
|
||||
}
|
||||
|
||||
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°21′29″N 71°03′49″W", ());
|
||||
// Minsk
|
||||
TEST_EQUAL(FormatLatLonAsDMS(53.916667, 27.55, true), "53°55′00″N 27°33′00″E", ());
|
||||
// Rio
|
||||
TEST_EQUAL(FormatLatLonAsDMS(-22.908333, -43.196389, true), "22°54′30″S 43°11′47″W", ());
|
||||
}
|
||||
|
||||
UNIT_TEST(LatLonToDMS_NoRounding)
|
||||
{
|
||||
// Paris
|
||||
TEST_EQUAL(FormatLatLonAsDMS(48.8567, 2.3508, false), "48°51′24.12″N 02°21′02.88″E", ());
|
||||
// Vatican
|
||||
TEST_EQUAL(FormatLatLonAsDMS(41.904, 12.453, false), "41°54′14.4″N 12°27′10.8″E", ());
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue