Skip to content

Commit e5ba2ad

Browse files
committed
Added decode() comparison of all mapcodes read from file.
1 parent 2d24468 commit e5ba2ad

File tree

1 file changed

+64
-18
lines changed

1 file changed

+64
-18
lines changed

examples/test geocoder.py

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
# as test data set.
77
#
88
# It reads each coordinate in the test dataset, does a geocode using the mapcode module and
9-
# compare the results with the entries in the test data set.
9+
# compare the results with the entries in the test data set. In addition it decodes all
10+
# mapcodes and compares it to the lat/lon in the file.
11+
#
12+
# Input format:
13+
#
14+
# <number of mapcodes> <latitude> <longitude>
15+
# <one or more mapcodes lines>
1016
#
1117
# Input format example:
1218
#
@@ -22,42 +28,82 @@
2228
# AAA TJKM1.D2Z6
2329

2430

31+
from __future__ import print_function
2532
import sys
33+
import time
2634
import mapcode
2735

2836

29-
def read_boundary_file(filename, debug=False):
37+
# The allowed margin in latitude, longitude
38+
allowed_margin = 0.00022
39+
40+
41+
def outside_margin(coordinate1, coordinate2):
42+
if abs(coordinate1 - coordinate2) > allowed_margin:
43+
return True
44+
else:
45+
return False
46+
47+
48+
def decode(latitude_in_file, longitude_in_file, mapcodes_in_file):
49+
# Decode all mapcodes from file back to latitude/longitude and compare
50+
for line in mapcodes_in_file:
51+
m_territory, m_code = line.split(' ')
52+
decoded_latitude, decoded_longitude = mapcode.decode(m_code, m_territory)
53+
54+
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))
58+
59+
# return how many decodes we have done
60+
return len(mapcodes_in_file)
61+
62+
63+
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)
66+
mapcodes_geocoded = set(m_territory + ' ' + m_code for m_code, m_territory in mapcodes)
67+
if mapcodes_in_file != mapcodes_geocoded:
68+
print('encode: mapcodes do no match: (file: %s) != %s' %
69+
(mapcodes_in_file, mapcodes_geocoded))
70+
71+
# return how many encodes we have done
72+
return 1
73+
74+
75+
def parse_boundary_file(filename, mapcode_function):
3076
with open(filename, 'r') as f:
31-
geocode_count = 0
77+
counter = 0
3278

79+
start_time = time.time()
3380
while True:
3481
header_line = f.readline()
3582
if not header_line:
36-
print 'EOF'
3783
break
3884

3985
# Get header line find out how many mapcodes will follow
40-
mapcode_count, lat, lon = header_line.strip().split(' ')
86+
mapcode_count, latitude, longitude = header_line.strip().split(' ')
87+
latitude = float(latitude)
88+
longitude = float(longitude)
89+
4190
# Put all mapcodes from file in a set
4291
mapcodes_in_file = set(f.readline().strip() for x in range(int(mapcode_count)))
43-
# if debug:
44-
# print 'Input file: ', mapcode_count, lat, lon, ' = ', mapcodes_in_file if debug
45-
# print header_line.strip()
46-
47-
# Do geocode ourself, change format to match fileformat, store in set
48-
mapcodes = mapcode.encode(float(lat), float(lon))
49-
mapcodes_geocoded = set(m_territory + ' ' + m_code for m_code, m_territory in mapcodes)
5092

51-
if mapcodes_in_file != mapcodes_geocoded:
52-
print "Geocodes did not match!", header_line, mapcodes_in_file, mapcodes_geocoded
53-
else:
54-
geocode_count += 1
93+
# do encode or decode
94+
counter += mapcode_function(latitude, longitude, mapcodes_in_file)
5595

56-
# eat whitespace between entries in source file
96+
# eat whitespace between entries in input file
5797
f.readline()
5898

99+
duration = time.time() - start_time
100+
print('Did %d %ss in %.3f seconds (%d per second).' % (counter,
101+
mapcode_function.__name__, duration, counter / duration))
102+
103+
59104
if __name__ == "__main__":
60105
if len(sys.argv) != 2:
61106
print('Usage: {} <input file>'.format(sys.argv[0]))
62107
else:
63-
read_boundary_file(sys.argv[1], False)
108+
parse_boundary_file(sys.argv[1], decode)
109+
parse_boundary_file(sys.argv[1], encode)

0 commit comments

Comments
 (0)