2 * We place these static definitions on a separate header file so that we can
3 * include the file in both the library and the tests.
11 #define OLC_kEncodingBase 20
12 #define OLC_kGridCols 4
13 #define OLC_kLatMaxDegrees 90
14 #define OLC_kLonMaxDegrees 180
16 // Separates the first eight digits from the rest of the code.
17 static const char kSeparator
= '+';
18 // Used to indicate null values before the separator.
19 static const char kPaddingCharacter
= '0';
20 // Digits used in the codes.
21 static const char kAlphabet
[] = "23456789CFGHJMPQRVWX";
22 // Number of digits in the alphabet.
23 static const size_t kEncodingBase
= OLC_kEncodingBase
;
24 // The max number of digits returned in a plus code. Roughly 1 x 0.5 cm.
25 static const size_t kMaximumDigitCount
= 15;
26 // The number of code characters that are lat/lng pairs.
27 static const size_t kPairCodeLength
= 10;
28 // The number of characters that combine lat and lng into a grid.
29 // kMaximumDigitCount - kPairCodeLength
30 static const size_t kGridCodeLength
= 5;
31 // The number of columns in each grid step.
32 static const size_t kGridCols
= OLC_kGridCols
;
33 // The number of rows in each grid step.
34 static const size_t kGridRows
= OLC_kEncodingBase
/ OLC_kGridCols
;
35 // The number of digits before the separator.
36 static const size_t kSeparatorPosition
= 8;
37 // Inverse of the precision of the last pair digits (in degrees).
38 static const size_t kPairPrecisionInverse
= 8000;
39 // Inverse (1/) of the precision of the final grid digits in degrees.
40 // Latitude is kEncodingBase^3 * kGridRows^kGridCodeLength
41 static const size_t kGridLatPrecisionInverse
= 2.5e7
;
42 // Longitude is kEncodingBase^3 * kGridColumns^kGridCodeLength
43 static const size_t kGridLonPrecisionInverse
= 8.192e6
;
44 // Latitude bounds are -kLatMaxDegrees degrees and +kLatMaxDegrees degrees
45 // which we transpose to 0 and 180 degrees.
46 static const double kLatMaxDegrees
= OLC_kLatMaxDegrees
;
47 static const double kLatMaxDegreesT2
= 2 * OLC_kLatMaxDegrees
;
49 // Longitude bounds are -kLonMaxDegrees degrees and +kLonMaxDegrees degrees
50 // which we transpose to 0 and 360 degrees.
51 static const double kLonMaxDegrees
= OLC_kLonMaxDegrees
;
52 static const double kLonMaxDegreesT2
= 2 * OLC_kLonMaxDegrees
;
54 // Lookup table of the alphabet positions of characters 'C' through 'X',
55 // inclusive. A value of -1 means the character isn't part of the alphabet.
56 static const int kPositionLUT
['X' - 'C' + 1] = {
57 8, -1, -1, 9, 10, 11, -1, 12, -1, -1, 13,
58 -1, -1, 14, 15, 16, -1, -1, -1, 17, 18, 19,
61 // Returns the position of a char in the encoding alphabet, or -1 if invalid.
62 static int get_alphabet_position(char c
) {
64 // We use a lookup table for performance reasons.
65 if (uc
>= 'C' && uc
<= 'X') return kPositionLUT
[uc
- 'C'];
66 if (uc
>= 'c' && uc
<= 'x') return kPositionLUT
[uc
- 'c'];
67 if (uc
>= '2' && uc
<= '9') return uc
- '2';