aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main/src/cgeo/geocaching/geopoint/Geopoint.java10
-rw-r--r--main/src/cgeo/geocaching/geopoint/GeopointFormatter.java45
-rw-r--r--tests/src/cgeo/geocaching/geopoint/GeoPointFormatterTest.java11
3 files changed, 51 insertions, 15 deletions
diff --git a/main/src/cgeo/geocaching/geopoint/Geopoint.java b/main/src/cgeo/geocaching/geopoint/Geopoint.java
index c03a2bc..dbe0aa4 100644
--- a/main/src/cgeo/geocaching/geopoint/Geopoint.java
+++ b/main/src/cgeo/geocaching/geopoint/Geopoint.java
@@ -173,6 +173,16 @@ public final class Geopoint implements ICoordinates, Parcelable {
return longitude;
}
+ /*
+ * Return a waypoint which is the copy of this one rounded to the given limit.
+ * For example, to get a waypoint adapter to a display with 3 digits after the
+ * seconds decimal point, a rounding factor of 3600*1000 would be appropriate.
+ */
+ Geopoint roundedAt(final long factor) {
+ return new Geopoint(((double) Math.round(latitude * factor)) / factor,
+ ((double) Math.round(longitude * factor)) / factor);
+ }
+
/**
* Get longitude in microdegree.
*
diff --git a/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java b/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java
index ba0a4d5..67f5ebb 100644
--- a/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java
+++ b/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java
@@ -64,36 +64,51 @@ public class GeopointFormatter {
case LAT_LON_DECDEGREE_COMMA:
return String.format((Locale) null, "%.6f,%.6f", latSigned, lonSigned);
- case LAT_LON_DECMINUTE:
+ case LAT_LON_DECMINUTE: {
+ final Geopoint rgp = gp.roundedAt(60 * 1000);
return String.format(Locale.getDefault(), "%c %02d° %06.3f · %c %03d° %06.3f",
- gp.getLatDir(), gp.getLatDeg(), gp.getLatMinRaw(), gp.getLonDir(), gp.getLonDeg(), gp.getLonMinRaw());
+ rgp.getLatDir(), rgp.getLatDeg(), rgp.getLatMinRaw(), rgp.getLonDir(), rgp.getLonDeg(), rgp.getLonMinRaw());
+ }
- case LAT_LON_DECMINUTE_RAW:
+ case LAT_LON_DECMINUTE_RAW: {
+ final Geopoint rgp = gp.roundedAt(60 * 1000);
return String.format((Locale) null, "%c %02d° %06.3f %c %03d° %06.3f",
- gp.getLatDir(), gp.getLatDeg(), gp.getLatMinRaw(), gp.getLonDir(), gp.getLonDeg(), gp.getLonMinRaw());
+ rgp.getLatDir(), rgp.getLatDeg(), rgp.getLatMinRaw(), rgp.getLonDir(), rgp.getLonDeg(), rgp.getLonMinRaw());
+ }
- case LAT_LON_DECSECOND:
+ case LAT_LON_DECSECOND: {
+ final Geopoint rgp = gp.roundedAt(3600 * 1000);
return String.format(Locale.getDefault(), "%c %02d° %02d' %06.3f\" · %c %03d° %02d' %06.3f\"",
- gp.getLatDir(), gp.getLatDeg(), gp.getLatMin(), gp.getLatSecRaw(),
- gp.getLonDir(), gp.getLonDeg(), gp.getLonMin(), gp.getLonSecRaw());
+ rgp.getLatDir(), rgp.getLatDeg(), rgp.getLatMin(), rgp.getLatSecRaw(),
+ rgp.getLonDir(), rgp.getLonDeg(), rgp.getLonMin(), rgp.getLonSecRaw());
+ }
case LAT_DECDEGREE_RAW:
return String.format((Locale) null, "%.6f", latSigned);
- case LAT_DECMINUTE:
- return String.format(Locale.getDefault(), "%c %02d° %06.3f", gp.getLatDir(), gp.getLatDeg(), gp.getLatMinRaw());
+ case LAT_DECMINUTE: {
+ final Geopoint rgp = gp.roundedAt(60 * 1000);
+ return String.format(Locale.getDefault(), "%c %02d° %06.3f", rgp.getLatDir(), rgp.getLatDeg(), rgp.getLatMinRaw());
+ }
- case LAT_DECMINUTE_RAW:
- return String.format(Locale.getDefault(), "%c %02d %06.3f", gp.getLatDir(), gp.getLatDeg(), gp.getLatMinRaw());
+ case LAT_DECMINUTE_RAW: {
+ final Geopoint rgp = gp.roundedAt(60 * 1000);
+ return String.format(Locale.getDefault(), "%c %02d %06.3f", rgp.getLatDir(), rgp.getLatDeg(), rgp.getLatMinRaw());
+ }
case LON_DECDEGREE_RAW:
return String.format((Locale) null, "%.6f", lonSigned);
- case LON_DECMINUTE:
- return String.format(Locale.getDefault(), "%c %03d° %06.3f", gp.getLonDir(), gp.getLonDeg(), gp.getLonMinRaw());
+ case LON_DECMINUTE: {
+ final Geopoint rgp = gp.roundedAt(60 * 1000);
+ return String.format(Locale.getDefault(), "%c %03d° %06.3f", rgp.getLonDir(), rgp.getLonDeg(), rgp.getLonMinRaw());
+ }
+
+ case LON_DECMINUTE_RAW: {
+ final Geopoint rgp = gp.roundedAt(60 * 1000);
+ return String.format(Locale.getDefault(), "%c %03d %06.3f", rgp.getLonDir(), rgp.getLonDeg(), rgp.getLonMinRaw());
+ }
- case LON_DECMINUTE_RAW:
- return String.format(Locale.getDefault(), "%c %03d %06.3f", gp.getLonDir(), gp.getLonDeg(), gp.getLonMinRaw());
default:
throw new IllegalArgumentException();
}
diff --git a/tests/src/cgeo/geocaching/geopoint/GeoPointFormatterTest.java b/tests/src/cgeo/geocaching/geopoint/GeoPointFormatterTest.java
index c0cea01..a1be25d 100644
--- a/tests/src/cgeo/geocaching/geopoint/GeoPointFormatterTest.java
+++ b/tests/src/cgeo/geocaching/geopoint/GeoPointFormatterTest.java
@@ -6,6 +6,17 @@ import android.test.AndroidTestCase;
public class GeoPointFormatterTest extends AndroidTestCase {
+ public static void testConfluence() {
+ // From issue #2624: coordinate is wrong near to a confluence point
+ final Geopoint point = new Geopoint(49.9999999999999, 5.0);
+ final String format = GeopointFormatter.format(GeopointFormatter.Format.LAT_LON_DECDEGREE_COMMA, point);
+ assertEquals("50.000000,5.000000", format);
+ final String formatMinute = GeopointFormatter.format(GeopointFormatter.Format.LAT_LON_DECMINUTE_RAW, point);
+ assertEquals("N 50° 00.000 E 005° 00.000", formatMinute);
+ final String formatSecond = GeopointFormatter.format(GeopointFormatter.Format.LAT_LON_DECSECOND, point).replaceAll(",", ".");
+ assertEquals(formatSecond, "N 50° 00' 00.000\"" + Formatter.SEPARATOR + "E 005° 00' 00.000\"", formatSecond);
+ }
+
public static void testFormat() {
// taken from GC30R6G
final Geopoint point = new Geopoint("N 51° 21.104 E 010° 15.369");