diff options
Diffstat (limited to 'src/cgeo/geocaching/geopoint/GeopointParser.java')
| -rw-r--r-- | src/cgeo/geocaching/geopoint/GeopointParser.java | 81 |
1 files changed, 44 insertions, 37 deletions
diff --git a/src/cgeo/geocaching/geopoint/GeopointParser.java b/src/cgeo/geocaching/geopoint/GeopointParser.java index e250459..e351701 100644 --- a/src/cgeo/geocaching/geopoint/GeopointParser.java +++ b/src/cgeo/geocaching/geopoint/GeopointParser.java @@ -1,10 +1,10 @@ package cgeo.geocaching.geopoint; +import cgeo.geocaching.geopoint.Geopoint.GeopointException; + import java.util.regex.Matcher; import java.util.regex.Pattern; -import cgeo.geocaching.geopoint.Geopoint.GeopointException; - /** * Parse coordinates. */ @@ -19,35 +19,38 @@ public class GeopointParser LAT, LON } - + /** * Parses a pair of coordinates (latitude and longitude) out of a String. * Accepts following formats and combinations of it: - * X DD - * X DD° - * X DD° MM - * X DD° MM.MMM - * X DD° MM SS - * + * X DD + * X DD° + * X DD° MM + * X DD° MM.MMM + * X DD° MM SS + * * as well as: - * DD.DDDDDDD - * + * DD.DDDDDDD + * * Both . and , are accepted, also variable count of spaces (also 0) - * - * @param text the string to parse + * + * @param text + * the string to parse * @return an Geopoint with parsed latitude and longitude - * @throws ParseException if lat or lon could not be parsed + * @throws ParseException + * if lat or lon could not be parsed */ public static Geopoint parse(final String text) { - + double lat = parseLatitude(text); double lon = parseLongitude(text); - + return new Geopoint(lat, lon); } - - /* (non JavaDoc) + + /* + * (non JavaDoc) * Helper for coordinates-parsing. */ private static double parseHelper(final String text, final LatLon latlon) @@ -66,22 +69,22 @@ public class GeopointParser if (matcher.find()) { - int sign = 1; - int degree = 0; - int minutes = 0; - int seconds = 0; - + int sign = 1; + int degree = 0; + int minutes = 0; + int seconds = 0; + if (matcher.group(1).equalsIgnoreCase("S") || matcher.group(1).equalsIgnoreCase("W")) { sign = -1; } - + degree = Integer.parseInt(matcher.group(2)); - + if (null != matcher.group(4)) { minutes = Integer.parseInt(matcher.group(4)); - + if (null != matcher.group(6)) { seconds = Math.round(Float.parseFloat("0." + matcher.group(6)) * 60); @@ -91,7 +94,7 @@ public class GeopointParser seconds = Integer.parseInt(matcher.group(7)); } } - + return (double) sign * ((double) degree + (double) minutes / 60 + (double) seconds / 3600); } else // Nothing found with "N 52...", try to match string as decimaldegree @@ -110,17 +113,19 @@ public class GeopointParser } } } - + throw new ParseException("Could not parse coordinates as " + latlon + ": \"" + text + "\""); } /** * Parses latitude out of a given string. - * + * * @see parse() - * @param text the string to be parsed + * @param text + * the string to be parsed * @return the latitude as decimaldegree - * @throws ParseException if latitude could not be parsed + * @throws ParseException + * if latitude could not be parsed */ public static double parseLatitude(final String text) { @@ -129,22 +134,24 @@ public class GeopointParser /** * Parses longitude out of a given string. - * + * * @see parse() - * @param text the string to be parsed + * @param text + * the string to be parsed * @return the longitude as decimaldegree - * @throws ParseException if longitude could not be parsed + * @throws ParseException + * if longitude could not be parsed */ public static double parseLongitude(final String text) { return parseHelper(text, LatLon.LON); } - + public static class ParseException - extends GeopointException + extends GeopointException { private static final long serialVersionUID = 1L; - + public ParseException(String msg) { super(msg); |
