Skip to content

Commit 175e747

Browse files
authored
Merge pull request google#129 from bocops/patch-1
Fix issue google#128 (recoverNearest for full OLC)
2 parents 675e822 + e5a9216 commit 175e747

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

python/openlocationcode.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,32 +270,36 @@ def decode(code):
270270
Note that short codes with an odd number of characters will have their
271271
last character decoded using the grid refinement algorithm.
272272
Args:
273-
shortCode: A valid short OLC character sequence.
273+
code: A valid OLC character sequence.
274274
referenceLatitude: The latitude (in signed decimal degrees) to use to
275275
find the nearest matching full code.
276276
referenceLongitude: The longitude (in signed decimal degrees) to use
277277
to find the nearest matching full code.
278278
Returns:
279279
The nearest full Open Location Code to the reference location that matches
280280
the short code. If the passed code was not a valid short code, but was a
281-
valid full code, it is returned unchanged.
282-
"""
283-
def recoverNearest(shortcode, referenceLatitude, referenceLongitude):
284-
if not isShort(shortcode):
285-
raise ValueError('Passed short code is not valid - ' + str(shortcode))
281+
valid full code, it is returned with proper capitalization but otherwise
282+
unchanged.
283+
"""
284+
def recoverNearest(code, referenceLatitude, referenceLongitude):
285+
# if code is a valid full code, return it properly capitalized
286+
if isFull(code):
287+
return code.upper()
288+
if not isShort(code):
289+
raise ValueError('Passed short code is not valid - ' + str(code))
286290
# Ensure that latitude and longitude are valid.
287291
referenceLatitude = clipLatitude(referenceLatitude)
288292
referenceLongitude = normalizeLongitude(referenceLongitude)
289293
# Clean up the passed code.
290-
shortcode = shortcode.upper()
294+
code = code.upper()
291295
# Compute the number of digits we need to recover.
292-
paddingLength = SEPARATOR_POSITION_ - shortcode.find(SEPARATOR_)
296+
paddingLength = SEPARATOR_POSITION_ - code.find(SEPARATOR_)
293297
# The resolution (height and width) of the padded area in degrees.
294298
resolution = pow(20, 2 - (paddingLength / 2))
295299
# Distance from the center to an edge (in degrees).
296300
halfResolution = resolution / 2.0
297301
# Use the reference location to pad the supplied short code and decode it.
298-
codeArea = decode(encode(referenceLatitude, referenceLongitude)[0:paddingLength] + shortcode)
302+
codeArea = decode(encode(referenceLatitude, referenceLongitude)[0:paddingLength] + code)
299303
# How many degrees latitude is the code from the reference? If it is more
300304
# than half the resolution, we need to move it north or south but keep it
301305
# within -90 to 90 degrees.

0 commit comments

Comments
 (0)