Skip to content

Commit 61f6092

Browse files
committed
Added support for high-precision mapcodes in test sets. Fixed for lat/lot margin check for negative to positive crossovers.
1 parent ad4a87e commit 61f6092

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

examples/test geocoder.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from __future__ import print_function
3232
import sys
3333
import time
34+
import re
3435
import mapcode
3536

3637

@@ -39,7 +40,7 @@
3940

4041

4142
def outside_margin(coordinate1, coordinate2):
42-
if abs(coordinate1 - coordinate2) > allowed_margin:
43+
if abs(abs(coordinate1) - abs(coordinate2)) > allowed_margin:
4344
return True
4445
else:
4546
return False
@@ -52,23 +53,36 @@ def decode(latitude_in_file, longitude_in_file, mapcodes_in_file):
5253
decoded_latitude, decoded_longitude = mapcode.decode(m_code, m_territory)
5354

5455
if outside_margin(decoded_latitude, latitude_in_file) or \
55-
outside_margin(decoded_longitude, longitude_in_file):
56-
print('decode: mapcode outside margin! (file: %s, %f, %f) != %f, %f' %
57-
(line, latitude_in_file, longitude_in_file, decoded_latitude, decoded_longitude))
56+
outside_margin(decoded_longitude, longitude_in_file):
57+
print('decode: mapcode outside margin! (file: %s, %f, %f) != %f, %f' %
58+
(line, latitude_in_file, longitude_in_file, decoded_latitude, decoded_longitude))
5859

59-
# return how many decodes we have done
60+
# Return how many decodes we have done
6061
return len(mapcodes_in_file)
6162

6263

64+
def is_high_precision(mapcode):
65+
# Check if third before last character is a hyphen (e.g. GLP 9N0.WN6-W3)
66+
if re.match(r'.*-..$', mapcode):
67+
return True
68+
else:
69+
return False
70+
71+
6372
def encode(latitude_in_file, longitude_in_file, mapcodes_in_file):
64-
# Do encode ourself, change format to match fileformat and compare
65-
mapcodes = mapcode.encode(latitude_in_file, longitude_in_file)
73+
# Do encode ourself, use extra precision incase input file entry has it
74+
if not is_high_precision(list(mapcodes_in_file)[0]):
75+
mapcodes = mapcode.encode(latitude_in_file, longitude_in_file)
76+
else:
77+
mapcodes = mapcode.encode(latitude_in_file, longitude_in_file, None, 2)
78+
79+
# Change format to match fileformat and compare
6680
mapcodes_geocoded = set(m_territory + ' ' + m_code for m_code, m_territory in mapcodes)
6781
if mapcodes_in_file != mapcodes_geocoded:
68-
print('encode: mapcodes do no match: (file: %s) != %s' %
69-
(mapcodes_in_file, mapcodes_geocoded))
82+
print('encode: mapcodes do no match: (file: %s) != %s' %
83+
(mapcodes_in_file, mapcodes_geocoded))
7084

71-
# return how many encodes we have done
85+
# Return how many encodes we have done
7286
return 1
7387

7488

@@ -91,14 +105,14 @@ def parse_boundary_file(filename, mapcode_function):
91105
mapcodes_in_file = set(f.readline().strip() for x in range(int(mapcode_count)))
92106

93107
# do encode or decode
94-
counter += mapcode_function(latitude, longitude, mapcodes_in_file)
108+
counter += mapcode_function(float(latitude), float(longitude), mapcodes_in_file)
95109

96110
# eat whitespace between entries in input file
97111
f.readline()
98112

99113
duration = time.time() - start_time
100114
print('Did %d %ss in %.3f seconds (%d per second).' % (counter,
101-
mapcode_function.__name__, duration, counter / duration))
115+
mapcode_function.__name__, duration, counter / duration))
102116

103117

104118
if __name__ == "__main__":

0 commit comments

Comments
 (0)