]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/olc/CoordPair.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / function / olc / CoordPair.java
1 package tim.prune.function.olc;
2
3 class ParseException extends Exception {}
4
5 /**
6  * Pair of coordinates
7  */
8 class CoordPair
9 {
10         /** Alphabet of allowed characters */
11         private static final String ALPHABET = "23456789CFGHJMPQRVWX";
12
13         public double lat = 0.0;
14         public double lon = 0.0;
15
16         /** Constructor */
17         public CoordPair(double inLat, double inLon)
18         {
19                 lat = inLat;
20                 lon = inLon;
21         }
22
23         /** Constant pair to represent padding */
24         public static CoordPair PADDING = new CoordPair(-1.0, -1.0);
25
26         /**
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
32          */
33         public static CoordPair decode(char inFirst, char inSecond) throws ParseException
34         {
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);
43         }
44
45         /**
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
50          */
51         public static CoordPair decode(char inChar) throws ParseException
52         {
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);
58         }
59
60         /**
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
65          */
66         private static int decodeChar(char inChar) throws ParseException
67         {
68                 final int index = ALPHABET.indexOf(inChar);
69                 if (index < 0)
70                 {
71                         throw new ParseException();
72                 }
73                 return index;
74         }
75 }