summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@digia.com>2014-10-09 15:50:16 +0200
committerAlex Blasche <alexander.blasche@digia.com>2014-10-13 09:32:40 +0200
commiteebf55a6919dff9eec4dd8529223aae6f6d0f569 (patch)
tree91e6f31e2bf18941e07d183bb0efa603061a4272
parentbbb2aa81c4cb58cb29ab3b5146b9b5cb909cd1c9 (diff)
downloadqtlocation-eebf55a6919dff9eec4dd8529223aae6f6d0f569.tar.gz
Fix QGeoCoordinate::toString() when rounding long/lat corner cases
Change-Id: I14da28acbd124e07da42fbf5efc3a501267f86f3 Task-number: QTBUG-41739 Reviewed-by: Lars Knoll <lars.knoll@digia.com>
-rw-r--r--src/positioning/qgeocoordinate.cpp41
-rw-r--r--tests/auto/qgeocoordinate/tst_qgeocoordinate.cpp22
2 files changed, 63 insertions, 0 deletions
diff --git a/src/positioning/qgeocoordinate.cpp b/src/positioning/qgeocoordinate.cpp
index ceb23cd3..e2a77bfd 100644
--- a/src/positioning/qgeocoordinate.cpp
+++ b/src/positioning/qgeocoordinate.cpp
@@ -502,6 +502,22 @@ QString QGeoCoordinate::toString(CoordinateFormat format) const
case DegreesMinutesWithHemisphere: {
double latMin = (absLat - int(absLat)) * 60;
double lngMin = (absLng - int(absLng)) * 60;
+
+ if (qRound(latMin) >= 60) {
+ absLat++;
+ latMin = qAbs(latMin - 60.0f);
+ //avoid invalid latitude due to latMin rounding below
+ if (qRound(absLat) >= 90)
+ latMin = 0.0f;
+ }
+ if (qRound(lngMin) >= 60) {
+ absLng++;
+ lngMin = qAbs(lngMin - 60.0f);
+ // avoid invalid longitude due to lngMin rounding below
+ if (qRound(absLng) >= 180)
+ lngMin = 0.0f;
+ }
+
latStr = QString::fromLatin1("%1%2 %3'")
.arg(QString::number(int(absLat)))
.arg(symbol)
@@ -519,6 +535,31 @@ QString QGeoCoordinate::toString(CoordinateFormat format) const
double latSec = (latMin - int(latMin)) * 60;
double lngSec = (lngMin - int(lngMin)) * 60;
+ // overflow to full minutes
+ if (qRound(latSec) >= 60) {
+ latMin++;
+ latSec = qAbs(latSec - 60.0f);
+ // overflow to full degrees
+ if (qRound(latMin) >= 60) {
+ absLat++;
+ latMin = qAbs(latMin - 60.0f);
+ // avoid invalid latitude due to latSec rounding below
+ if (qRound(absLat) >= 90)
+ latSec = 0.0f;
+ }
+ }
+ if (qRound(lngSec) >= 60) {
+ lngMin++;
+ lngSec = qAbs(lngSec - 60.0f);
+ if (qRound(lngMin) >= 60) {
+ absLng++;
+ lngMin = qAbs(lngMin - 60.0f);
+ // avoid invalid longitude due to lngSec rounding below
+ if (qRound(absLng) >= 180)
+ lngSec = 0.0f;
+ }
+ }
+
latStr = QString::fromLatin1("%1%2 %3' %4\"")
.arg(QString::number(int(absLat)))
.arg(symbol)
diff --git a/tests/auto/qgeocoordinate/tst_qgeocoordinate.cpp b/tests/auto/qgeocoordinate/tst_qgeocoordinate.cpp
index 6979588c..142873dd 100644
--- a/tests/auto/qgeocoordinate/tst_qgeocoordinate.cpp
+++ b/tests/auto/qgeocoordinate/tst_qgeocoordinate.cpp
@@ -819,6 +819,28 @@ private slots:
QTest::newRow("SW with alt, dms, hemisphere, 28.2341m")
<< southWestWithAlt << QGeoCoordinate::DegreesMinutesSecondsWithHemisphere
<< QString("27%1 28' 3.3\" S, 153%1 1' 40.4\" W, 28.2341m").arg(DEGREES_SYMB);
+
+ QTest::newRow("Wrap seconds to Minutes DMSH")
+ << QGeoCoordinate(1.1333333, 1.1333333) << QGeoCoordinate::DegreesMinutesSecondsWithHemisphere
+ << QString( "1%1 8' 0.0\" N, 1%1 8' 0.0\" E").arg(DEGREES_SYMB);
+ QTest::newRow("Wrap seconds to Minutes DMS")
+ << QGeoCoordinate(1.1333333, 1.1333333) << QGeoCoordinate::DegreesMinutesSeconds
+ << QString( "1%1 8' 0.0\", 1%1 8' 0.0\"").arg(DEGREES_SYMB);
+ QTest::newRow("Wrap minutes to Degrees DMH")
+ << QGeoCoordinate(1.999999, 1.999999) << QGeoCoordinate::DegreesMinutesWithHemisphere
+ << QString( "2%1 0.000' N, 2%1 0.000' E").arg(DEGREES_SYMB);
+ QTest::newRow("Wrap minutes to Degrees DM")
+ << QGeoCoordinate(1.999999, 1.999999) << QGeoCoordinate::DegreesMinutes
+ << QString( "2%1 0.000', 2%1 0.000'").arg(DEGREES_SYMB);
+
+ QTest::newRow("Wrap seconds to minutes to Degrees DM -> above valid long/lat values")
+ << QGeoCoordinate(89.9999, 179.9999) << QGeoCoordinate::DegreesMinutesSeconds
+ << QString( "90%1 0' 0.0\", 180%1 0' 0.0\"").arg(DEGREES_SYMB);
+
+ QTest::newRow("Wrap minutes to Degrees DM ->above valid long/lat values")
+ << QGeoCoordinate(89.9999, 179.9999) << QGeoCoordinate::DegreesMinutes
+ << QString( "90%1 0.000', 180%1 0.000'").arg(DEGREES_SYMB);
+
}
void datastream()