You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The initial grid size is 20° x 20° so that we can represent the full longitude range (-180 to 180, or 0 to 360).
47
70
48
71
Digits 11 to 15 differ from the above method in that each step adds a single digit, representing the position in a 4 x 5 grid.
49
72
(Latitude is divided by five, longitude divided by four.)
73
+
The code symbols are positioned in these grid cells as follows:
74
+
75
+
R V W X
76
+
J M P Q
77
+
C F G H
78
+
6 7 8 9
79
+
2 3 4 5
50
80
51
81
The following table gives the precision of the valid code lengths in degrees and in meters. Where the precisions differ between latitude and longitude both are shown (as latitude x longitude):
52
82
@@ -89,68 +119,72 @@ Implementations using high precision floating-point libraries should be well tes
89
119
The algorithms below should be considered as reference implementations.
90
120
Other implementations should be well tested to make sure they do not differ.
91
121
122
+
Whether you build the code from the first digit or the last digit may depend on personal preference, or the available string-building libraries.
123
+
92
124
### Prerequisites
93
125
94
-
The encoding algorithms below operate on integer values, requiring the latitude and longitude values to be converted to be positive integers.
95
-
Take care when implementing - merging the operations below can result in unexpected rounding operations and will result in test failures.
126
+
Both the encoding algorithms below operate on integer values, requiring the latitude and longitude values to be converted to be positive integers.
127
+
Take care when implementing this conversion - merging the operations below can result in unexpected rounding operations and will result in test failures.
96
128
97
129
1. Convert latitude to a clipped, positive integer:
98
-
1. Multiply it by the maximum latitude resolution, 2.5e7, and truncate (*not round*) it.
130
+
1. Multiply it by the maximum latitude resolution, 2.5e7, and truncate (_not round_) it.
99
131
2. Add `90 x 2.5e7` to put it in a positive range.
100
132
3. Clip it so that it is in the range `0 <= latitude < 180`. (If it is greater than or equal to 180, set it to `180 - 1/8.192e6`.)
101
133
2. Convert longitude to a normalised, positive integer:
102
-
1. Multiply it by the maximum longitude resolution, 8.192e6, and truncate (*not round*) it.
134
+
1. Multiply it by the maximum longitude resolution, 8.192e6, and truncate (_not round_) it.
103
135
2. Add `180 x 8.192e6` to put it in a positive range.
104
136
3. Normalise the longitude so that it is in the range `0 <= longitude < 360`.
105
137
106
-
### Reverse Encoding
107
-
108
-
This algorithm computes the code digits starting at the least significant digit, digit 15.
109
-
110
-
For each digit 15 to 11:
111
-
112
-
1. The code digits position in the symbol set is defined by `(latitude % 5) * 4 + (longitude % 4)`.
113
-
1. (`%` is the modulo or remainder operation, and this produces values in the range 0-19.)
114
-
2. For example, if the calculation returns the value `8`, the code digit is `C`.
115
-
2. Divide latitude by 5 and truncate it.
116
-
1. For example, `latitude = math.Floor(latitude / 5)`
117
-
3. Similarly, divide longitude by 4 and truncate it.
118
-
119
-
Digits 10 to 1 are computed in pairs as follows:
120
-
121
-
1. Digit _n_ (latitude) position in the symbol set is defined by `latitude % 20`
122
-
1. For example, if the calculation returns the value `8`, the code digit is `C`.
123
-
2. Similarly, digit _n + 1_ (longitude) position in the symbol set is defined by `longitude %20`
124
-
3. Divide each value by `20` (and truncate).
125
-
126
-
The separator character "+" will need to be inserted into the code, and codes with fewer digits than the separator position will need to be padded with "0".
127
-
128
138
### Forward Encoding
129
139
130
-
The code can also be computed starting from the first character.
140
+
This algorithm computes the code starting from the first character and working towards the end of the code.
131
141
132
142
1. Define a latitude divisor of `400 * 2.5e7`.
133
143
2. Define a longitude divisor of `400 * 8.192e6`
134
144
135
145
Digits 1 to 10 are computed in pairs as follows:
136
146
137
147
1. Divide the `latitude divisor` by `20`
138
-
2. Digit _n_ (latitude) position in the symbol set is defined by the quotient of `latitude / latitude divisor`
148
+
2. Digit _n_ (latitude) position in the symbol set is defined by the integer result ("quotient") of `latitude / latitude divisor`
139
149
3. Subtract `latitude divisor * quotient` from `latitude`
140
150
4. Divide the `longitude divisor` by `20`
141
-
5. Digit _n + 1_ (longitude) position in the symbol set is defined by the quotient of `longitude / longitude divisor`
151
+
5. Digit _n + 1_ (longitude) position in the symbol set is defined by the integer result ("quotient") of `longitude / longitude divisor`
142
152
6. Subtract `longitude divisor * quotient` from `longitude`
143
153
144
154
Digits 11 to 15 are computed as follows:
145
155
146
156
1. Divide `latitude divisor` by `5`
147
157
2. Divide `longitude divisor` by `4`
148
-
3. Latitude quotient is defined by the quotient of `latitude / latitude divisor`
149
-
4. Longitude quotient is defined by the quotient of `longitude / longitude divisor`
158
+
3. Latitude quotient is defined by the integer result of `latitude / latitude divisor`
159
+
4. Longitude quotient is defined by the integer result of `longitude / longitude divisor`
150
160
5. Digit _n_ position in the symbol set is defined by `latitude quotient * 4 + longitude quotient`
151
161
152
162
The separator character "+" will need to be inserted into the code, and codes with fewer digits than the separator position will need to be padded with "0".
153
163
164
+
### Reverse Encoding
165
+
166
+
This alternative algorithm computes the code digits starting at the least significant digit, digit 15.
167
+
It is also based on the integer converted values for latitude and longitude.
168
+
This may be more convenient to implement, depending on the string building capabilities in a specific language.
169
+
170
+
For each digit 15 to 11:
171
+
172
+
1. The code digits position in the symbol set is defined by `(latitude % 5) * 4 + (longitude % 4)`.
173
+
1. (`%` is the modulo or remainder operation, and this produces values in the range 0-19.)
174
+
2. For example, if the calculation returns the value `8`, the code digit is `C`.
175
+
2. Set latitude to the integer result of `latitude / 5`.
176
+
1. For example, `latitude = math.Floor(latitude / 5)` or `latitude = int64(latitude / 5)`
177
+
3. Similarly, set longitude to the integer result of `longitude / 4`.
178
+
179
+
Digits 10 to 1 are computed in pairs as follows:
180
+
181
+
1. Digit _n_ (latitude) position in the symbol set is defined by `latitude % 20`
182
+
1. For example, if the calculation returns the value `8`, the code digit is `C`.
183
+
2. Similarly, digit _n + 1_ (longitude) position in the symbol set is defined by `longitude % 20`
184
+
3. Divide each value by `20` (and truncate).
185
+
186
+
The separator character "+" will need to be inserted into the code, and codes with fewer digits than the separator position will need to be padded with "0".
187
+
154
188
## Decoding
155
189
156
190
Decoding is the reverse of the encoding algorithms, and can be done in either direction.
0 commit comments