]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - src/tim/prune/function/olc/CoordPair.java
Version 20, March 2020
[GpsPrune.git] / src / tim / prune / function / olc / CoordPair.java
diff --git a/src/tim/prune/function/olc/CoordPair.java b/src/tim/prune/function/olc/CoordPair.java
new file mode 100644 (file)
index 0000000..778b46d
--- /dev/null
@@ -0,0 +1,75 @@
+package tim.prune.function.olc;
+
+class ParseException extends Exception {}
+
+/**
+ * Pair of coordinates
+ */
+class CoordPair
+{
+       /** Alphabet of allowed characters */
+       private static final String ALPHABET = "23456789CFGHJMPQRVWX";
+
+       public double lat = 0.0;
+       public double lon = 0.0;
+
+       /** Constructor */
+       public CoordPair(double inLat, double inLon)
+       {
+               lat = inLat;
+               lon = inLon;
+       }
+
+       /** Constant pair to represent padding */
+       public static CoordPair PADDING = new CoordPair(-1.0, -1.0);
+
+       /**
+        * Try to parse the given pair of characters into a CoordPair
+        * @param inFirst first character of pair
+        * @param inSecond second character of pair
+        * @return CoordPair from (0, 0) to (19/20, 19/20)
+        * @throws ParseException
+        */
+       public static CoordPair decode(char inFirst, char inSecond) throws ParseException
+       {
+               final boolean isFirstPadding = (inFirst == '0');
+               final boolean isSecondPadding = (inSecond == '0');
+               if (isFirstPadding && isSecondPadding) {return CoordPair.PADDING;}
+               if (isFirstPadding || isSecondPadding) {throw new ParseException();}
+               // Try to turn these characters into numbers
+               final double lat = decodeChar(inFirst);
+               final double lon = decodeChar(inSecond);
+               return new CoordPair(lat / 20.0, lon / 20.0);
+       }
+
+       /**
+        * Try to parse the given single character into a CoordPair
+        * @param inChar single character from level 11
+        * @return CoordPair from (0, 0) to (19/20, 19/20)
+        * @throws ParseException
+        */
+       public static CoordPair decode(char inChar) throws ParseException
+       {
+               // Try to turn this character into a number
+               final int charIndex = decodeChar(inChar);
+               final int lat = charIndex / 4;
+               final int lon = charIndex % 4;
+               return new CoordPair(lat / 5.0, lon / 4.0);
+       }
+
+       /**
+        * Get the index from the given character
+        * @param inChar character from OLC
+        * @return index from 0 to 19
+        * @throws ParseException if character not found
+        */
+       private static int decodeChar(char inChar) throws ParseException
+       {
+               final int index = ALPHABET.indexOf(inChar);
+               if (index < 0)
+               {
+                       throw new ParseException();
+               }
+               return index;
+       }
+}