Skip to content

Commit 34c0928

Browse files
authored
Update openlocationcode.py
1 parent 99ae839 commit 34c0928

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

python/openlocationcode.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -293,25 +293,26 @@ def recoverNearest(shortcode, referenceLatitude, referenceLongitude):
293293
# The resolution (height and width) of the padded area in degrees.
294294
resolution = pow(20, 2 - (paddingLength / 2))
295295
# Distance from the center to an edge (in degrees).
296-
areaToEdge = resolution / 2.0
296+
halfResolution = resolution / 2.0
297297
# Use the reference location to pad the supplied short code and decode it.
298298
codeArea = decode(encode(referenceLatitude, referenceLongitude)[0:paddingLength] + shortcode)
299299
# How many degrees latitude is the code from the reference? If it is more
300-
# than half the resolution, we need to move it east or west.
301-
degreesDifference = codeArea.latitudeCenter - referenceLatitude
302-
if degreesDifference > areaToEdge:
303-
# If the center of the short code is more than half a cell east,
304-
# then the best match will be one position west.
300+
# than half the resolution, we need to move it north or south but keep it
301+
# within -90 to 90 degrees.
302+
if (referenceLatitude + halfResolution < codeArea.latitudeCenter and
303+
codeArea.latitudeCenter - resolution >= -LATITUDE_MAX_):
304+
# If the proposed code is more than half a cell north of the reference location,
305+
# it's too far, and the best match will be one cell south.
305306
codeArea.latitudeCenter -= resolution
306-
elif degreesDifference < -areaToEdge:
307-
# If the center of the short code is more than half a cell west,
308-
# then the best match will be one position east.
307+
elif (referenceLatitude - halfResolution > codeArea.latitudeCenter &&
308+
codeArea.latitudeCenter + resolution <= LATITUDE_MAX_):
309+
# If the proposed code is more than half a cell south of the reference location,
310+
# it's too far, and the best match will be one cell north.
309311
codeArea.latitudeCenter += resolution
310-
# How many degrees longitude is the code from the reference?
311-
degreesDifference = codeArea.longitudeCenter - referenceLongitude
312-
if degreesDifference > areaToEdge:
312+
# Adjust longitude if necessary.
313+
if referenceLongitude + halfResolution < codeArea.longitudeCenter:
313314
codeArea.longitudeCenter -= resolution
314-
elif degreesDifference < -areaToEdge:
315+
elif referenceLongitude - halfResolution > codeArea.longitudeCenter:
315316
codeArea.longitudeCenter += resolution
316317
return encode(codeArea.latitudeCenter, codeArea.longitudeCenter, codeArea.codeLength)
317318

0 commit comments

Comments
 (0)