1 package tim.prune.function.olc;
5 * Decoder of OLC (Open Location Code) strings
7 public class OlcDecoder
10 * Decode the given String into an OlcArea object
11 * @param inCode code representing an OLC
12 * @return an OlcArea object, or null if parsing failed
14 public static OlcArea decode(String inCode)
16 if (inCode == null || inCode.length() < 8) {
19 String code = inCode.trim().toUpperCase();
20 if (code.length() < 8 || code.length() > 12) {
23 double lat = 0.0, lon = 0.0;
24 double resolution = 400.0;
27 boolean amPadding = false;
30 while (charPos < inCode.length())
32 if (charPos == 0 || charPos == 2 || charPos == 4 || charPos == 6 || charPos == 9)
34 // take next two characters, make pair, position += 2
35 CoordPair pair = CoordPair.decode(code.charAt(charPos), code.charAt(charPos+1));
36 if (pair == CoordPair.PADDING) {
45 // Add to current lat, lon
46 lat += (pair.lat * resolution);
47 lon += (pair.lon * resolution);
53 else if (charPos == 8)
55 if (code.charAt(charPos) != '+')
61 else if (charPos == 11)
63 // take next character, make pair
64 CoordPair pair = CoordPair.decode(code.charAt(charPos));
65 // Add to current lat, lon
66 lat += (pair.lat * resolution);
67 lon += (pair.lon * resolution);
78 // Make OlcArea object and return it
83 else if (numSteps < 6)
88 return new OlcArea(lat, lon, lat+resolution, lon+resolution);
95 return new OlcArea(lat, lon, lat+resolution*2.5, lon+resolution*2.0);
98 catch (ParseException e) {}