diff options
-rw-r--r-- | main/src/cgeo/geocaching/location/GeopointParser.java | 8 | ||||
-rw-r--r-- | tests/src/cgeo/geocaching/location/GeoPointParserTest.java | 8 |
2 files changed, 10 insertions, 6 deletions
diff --git a/main/src/cgeo/geocaching/location/GeopointParser.java b/main/src/cgeo/geocaching/location/GeopointParser.java index a6b8e45..273c5b8 100644 --- a/main/src/cgeo/geocaching/location/GeopointParser.java +++ b/main/src/cgeo/geocaching/location/GeopointParser.java @@ -24,9 +24,9 @@ class GeopointParser { } } - // ( 1 ) ( 2 ) ( 3 ) ( 4 ) ( 5 ) - private static final Pattern PATTERN_LAT = Pattern.compile("\\b([NS]|)\\s*(\\d+)°?(?:\\s*(\\d+)(?:[.,](\\d+)|'?\\s*(\\d+(?:[.,]\\d+)?)(?:''|\")?)?)?", Pattern.CASE_INSENSITIVE); - private static final Pattern PATTERN_LON = Pattern.compile("\\b([WE]|)\\s*(\\d+)°?(?:\\s*(\\d+)(?:[.,](\\d+)|'?\\s*(\\d+(?:[.,]\\d+)?)(?:''|\")?)?)?", Pattern.CASE_INSENSITIVE); + // ( 1 ) ( 2 ) ( 3 ) ( 4 ) ( 5 ) + private static final Pattern PATTERN_LAT = Pattern.compile("\\b([NS]|)\\s*(\\d+°?|°)(?:\\s*(\\d+)(?:[.,](\\d+)|'?\\s*(\\d+(?:[.,]\\d+)?)(?:''|\")?)?)?", Pattern.CASE_INSENSITIVE); + private static final Pattern PATTERN_LON = Pattern.compile("\\b([WE]|)\\s*(\\d+°?|°)(?:\\s*(\\d+)(?:[.,](\\d+)|'?\\s*(\\d+(?:[.,]\\d+)?)(?:''|\")?)?)?", Pattern.CASE_INSENSITIVE); private static final Pattern PATTERN_BAD_BLANK = Pattern.compile("(\\d)[,.] (\\d{2,})"); @@ -99,7 +99,7 @@ class GeopointParser { try { if (matcher.find()) { final double sign = matcher.group(1).equalsIgnoreCase("S") || matcher.group(1).equalsIgnoreCase("W") ? -1.0 : 1.0; - final double degree = Integer.valueOf(matcher.group(2)).doubleValue(); + final double degree = Integer.valueOf(StringUtils.defaultIfEmpty(StringUtils.stripEnd(matcher.group(2), "°"), "0")).doubleValue(); double minutes = 0.0; double seconds = 0.0; diff --git a/tests/src/cgeo/geocaching/location/GeoPointParserTest.java b/tests/src/cgeo/geocaching/location/GeoPointParserTest.java index e9e002d..a2b916d 100644 --- a/tests/src/cgeo/geocaching/location/GeoPointParserTest.java +++ b/tests/src/cgeo/geocaching/location/GeoPointParserTest.java @@ -2,8 +2,6 @@ package cgeo.geocaching.location; import static org.assertj.core.api.Assertions.assertThat; -import cgeo.geocaching.location.Geopoint; -import cgeo.geocaching.location.GeopointParser; import cgeo.geocaching.utils.Formatter; import android.test.AndroidTestCase; @@ -43,6 +41,12 @@ public class GeoPointParserTest extends AndroidTestCase { assertThat(point).isNull(); } + public static void testCoordinateMissingDegree() { + // Some home coordinates on geocaching.com lack the degree part. + final Geopoint point = GeopointParser.parse("N 51° 23.123' W ° 17.123"); + assertThat(point).isEqualTo(new Geopoint("N", "51", "23", "123", "W", "0", "17", "123")); + } + public static void testSouth() { assertEquals(-refLatitude, GeopointParser.parseLatitude("S 49° 56.031"), 1e-8); } |