1 package tim.prune.function.olc;
3 class ParseException extends Exception {}
10 /** Alphabet of allowed characters */
11 private static final String ALPHABET = "23456789CFGHJMPQRVWX";
13 public double lat = 0.0;
14 public double lon = 0.0;
17 public CoordPair(double inLat, double inLon)
23 /** Constant pair to represent padding */
24 public static CoordPair PADDING = new CoordPair(-1.0, -1.0);
27 * Try to parse the given pair of characters into a CoordPair
28 * @param inFirst first character of pair
29 * @param inSecond second character of pair
30 * @return CoordPair from (0, 0) to (19/20, 19/20)
31 * @throws ParseException
33 public static CoordPair decode(char inFirst, char inSecond) throws ParseException
35 final boolean isFirstPadding = (inFirst == '0');
36 final boolean isSecondPadding = (inSecond == '0');
37 if (isFirstPadding && isSecondPadding) {return CoordPair.PADDING;}
38 if (isFirstPadding || isSecondPadding) {throw new ParseException();}
39 // Try to turn these characters into numbers
40 final double lat = decodeChar(inFirst);
41 final double lon = decodeChar(inSecond);
42 return new CoordPair(lat / 20.0, lon / 20.0);
46 * Try to parse the given single character into a CoordPair
47 * @param inChar single character from level 11
48 * @return CoordPair from (0, 0) to (19/20, 19/20)
49 * @throws ParseException
51 public static CoordPair decode(char inChar) throws ParseException
53 // Try to turn this character into a number
54 final int charIndex = decodeChar(inChar);
55 final int lat = charIndex / 4;
56 final int lon = charIndex % 4;
57 return new CoordPair(lat / 5.0, lon / 4.0);
61 * Get the index from the given character
62 * @param inChar character from OLC
63 * @return index from 0 to 19
64 * @throws ParseException if character not found
66 private static int decodeChar(char inChar) throws ParseException
68 final int index = ALPHABET.indexOf(inChar);
71 throw new ParseException();