X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Ffunction%2Folc%2FCoordPair.java;fp=src%2Ftim%2Fprune%2Ffunction%2Folc%2FCoordPair.java;h=778b46d98c4705ec36f59960246be9077134d4ef;hp=0000000000000000000000000000000000000000;hb=8b20e3e027058cdf6ff52993ee5576193d08667a;hpb=2302358503c38817e19f6e529f6c9e530aac0e86 diff --git a/src/tim/prune/function/olc/CoordPair.java b/src/tim/prune/function/olc/CoordPair.java new file mode 100644 index 0000000..778b46d --- /dev/null +++ b/src/tim/prune/function/olc/CoordPair.java @@ -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; + } +}