@@ -293,25 +293,26 @@ def recoverNearest(shortcode, referenceLatitude, referenceLongitude):
293
293
# The resolution (height and width) of the padded area in degrees.
294
294
resolution = pow (20 , 2 - (paddingLength / 2 ))
295
295
# Distance from the center to an edge (in degrees).
296
- areaToEdge = resolution / 2.0
296
+ halfResolution = resolution / 2.0
297
297
# Use the reference location to pad the supplied short code and decode it.
298
298
codeArea = decode (encode (referenceLatitude , referenceLongitude )[0 :paddingLength ] + shortcode )
299
299
# 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.
305
306
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.
309
311
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 :
313
314
codeArea .longitudeCenter -= resolution
314
- elif degreesDifference < - areaToEdge :
315
+ elif referenceLongitude - halfResolution > codeArea . longitudeCenter :
315
316
codeArea .longitudeCenter += resolution
316
317
return encode (codeArea .latitudeCenter , codeArea .longitudeCenter , codeArea .codeLength )
317
318
0 commit comments