|
103 | 103 | #Minimum length of a code that can be shortened.
|
104 | 104 | MIN_TRIMMABLE_CODE_LEN_ = 6
|
105 | 105 |
|
| 106 | +#The maximum significant digits in a plus code. |
| 107 | +MAX_CODE_LENGTH = 15 |
| 108 | + |
106 | 109 | SP = '+0'
|
107 | 110 |
|
108 | 111 |
|
@@ -213,6 +216,7 @@ def isFull(code):
|
213 | 216 | def encode(latitude, longitude, codeLength=PAIR_CODE_LENGTH_):
|
214 | 217 | if codeLength < 2 or (codeLength < PAIR_CODE_LENGTH_ and codeLength % 2 == 1):
|
215 | 218 | raise ValueError('Invalid Open Location Code length - ' + str(codeLength))
|
| 219 | + codeLength = min(codeLength, MAX_CODE_LENGTH) |
216 | 220 | # Ensure that latitude and longitude are valid.
|
217 | 221 | latitude = clipLatitude(latitude)
|
218 | 222 | longitude = normalizeLongitude(longitude)
|
@@ -240,10 +244,12 @@ def decode(code):
|
240 | 244 | if not isFull(code):
|
241 | 245 | raise ValueError('Passed Open Location Code is not a valid full code - ' + str(code))
|
242 | 246 | # Strip out separator character (we've already established the code is
|
243 |
| - # valid so the maximum is one), padding characters and convert to upper |
244 |
| - # case. |
| 247 | + # valid so the maximum is one), and padding characters. Convert to upper |
| 248 | + # case and constrain to the maximum number of digits. |
245 | 249 | code = re.sub('[+0]','',code)
|
246 | 250 | code = code.upper()
|
| 251 | + code = code[:MAX_CODE_LENGTH] |
| 252 | + |
247 | 253 | # Decode the lat/lng pair component.
|
248 | 254 | codeArea = decodePairs(code[0:PAIR_CODE_LENGTH_])
|
249 | 255 | if len(code) <= PAIR_CODE_LENGTH_:
|
|
0 commit comments