1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
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 final double lat;
public final double lon;
/** Constructor */
public CoordPair(double inLat, double inLon)
{
lat = inLat;
lon = inLon;
}
/** Constant pair to represent padding */
public static final 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;
}
/**
* Get the character for the given index
* @param inValue from 0 to 19
*/
public static char encode(int inValue)
{
return ALPHABET.charAt((inValue + 20) % 20);
}
}
|