]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/olc/OlcDecoder.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / function / olc / OlcDecoder.java
1 package tim.prune.function.olc;
2
3
4 /**
5  * Decoder of OLC (Open Location Code) strings
6  */
7 public class OlcDecoder
8 {
9         /**
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
13          */
14         public static OlcArea decode(String inCode)
15         {
16                 if (inCode == null || inCode.length() < 8) {
17                         return null;
18                 }
19                 String code = inCode.trim().toUpperCase();
20                 if (code.length() < 8 || code.length() > 12) {
21                         return null;
22                 }
23                 double lat = 0.0, lon = 0.0;
24                 double resolution = 400.0;
25                 int charPos = 0;
26                 int numSteps = 0;
27                 boolean amPadding = false;
28                 try
29                 {
30                         while (charPos < inCode.length())
31                         {
32                                 if (charPos == 0 || charPos == 2 || charPos == 4 || charPos == 6 || charPos == 9)
33                                 {
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) {
37                                                 amPadding = true;
38                                         }
39                                         else if (amPadding)
40                                         {
41                                                 return null;
42                                         }
43                                         else
44                                         {
45                                                 // Add to current lat, lon
46                                                 lat += (pair.lat * resolution);
47                                                 lon += (pair.lon * resolution);
48                                                 numSteps++;
49                                                 resolution /= 20.0;
50                                         }
51                                         charPos += 2;
52                                 }
53                                 else if (charPos == 8)
54                                 {
55                                         if (code.charAt(charPos) != '+')
56                                         {
57                                                 return null;
58                                         }
59                                         charPos += 1;
60                                 }
61                                 else if (charPos == 11)
62                                 {
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);
68                                         charPos += 1;
69                                         numSteps++;
70                                         resolution /= 20.0;
71                                 }
72                                 else
73                                 {
74                                         return null;
75                                 }
76                         }
77
78                         // Make OlcArea object and return it
79                         if (numSteps < 1)
80                         {
81                                 return null;
82                         }
83                         else if (numSteps < 6)
84                         {
85                                 // make four points
86                                 lat -= 90.0;
87                                 lon -= 180.0;
88                                 return new OlcArea(lat, lon, lat+resolution, lon+resolution);
89                         }
90                         else
91                         {
92                                 // make single point:
93                                 lat -= 90.0;
94                                 lon -= 180.0;
95                                 return new OlcArea(lat, lon, lat+resolution*2.5, lon+resolution*2.0);
96                         }
97                 }
98                 catch (ParseException e) {}
99                 return null;
100         }
101 }