Skip to content

Commit c001f45

Browse files
Implement max code length behavior for Python
1 parent df32153 commit c001f45

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

python/openlocationcode.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
#Minimum length of a code that can be shortened.
104104
MIN_TRIMMABLE_CODE_LEN_ = 6
105105

106+
#The maximum significant digits in a plus code.
107+
MAX_CODE_LENGTH = 15
108+
106109
SP = '+0'
107110

108111

@@ -213,6 +216,7 @@ def isFull(code):
213216
def encode(latitude, longitude, codeLength=PAIR_CODE_LENGTH_):
214217
if codeLength < 2 or (codeLength < PAIR_CODE_LENGTH_ and codeLength % 2 == 1):
215218
raise ValueError('Invalid Open Location Code length - ' + str(codeLength))
219+
codeLength = min(codeLength, MAX_CODE_LENGTH)
216220
# Ensure that latitude and longitude are valid.
217221
latitude = clipLatitude(latitude)
218222
longitude = normalizeLongitude(longitude)
@@ -240,10 +244,12 @@ def decode(code):
240244
if not isFull(code):
241245
raise ValueError('Passed Open Location Code is not a valid full code - ' + str(code))
242246
# 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.
245249
code = re.sub('[+0]','',code)
246250
code = code.upper()
251+
code = code[:MAX_CODE_LENGTH]
252+
247253
# Decode the lat/lng pair component.
248254
codeArea = decodePairs(code[0:PAIR_CODE_LENGTH_])
249255
if len(code) <= PAIR_CODE_LENGTH_:

0 commit comments

Comments
 (0)