|
6 | 6 | # as test data set. |
7 | 7 | # |
8 | 8 | # 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> |
10 | 16 | # |
11 | 17 | # Input format example: |
12 | 18 | # |
|
22 | 28 | # AAA TJKM1.D2Z6 |
23 | 29 |
|
24 | 30 |
|
| 31 | +from __future__ import print_function |
25 | 32 | import sys |
| 33 | +import time |
26 | 34 | import mapcode |
27 | 35 |
|
28 | 36 |
|
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): |
30 | 76 | with open(filename, 'r') as f: |
31 | | - geocode_count = 0 |
| 77 | + counter = 0 |
32 | 78 |
|
| 79 | + start_time = time.time() |
33 | 80 | while True: |
34 | 81 | header_line = f.readline() |
35 | 82 | if not header_line: |
36 | | - print 'EOF' |
37 | 83 | break |
38 | 84 |
|
39 | 85 | # 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 | + |
41 | 90 | # Put all mapcodes from file in a set |
42 | 91 | 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) |
50 | 92 |
|
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) |
55 | 95 |
|
56 | | - # eat whitespace between entries in source file |
| 96 | + # eat whitespace between entries in input file |
57 | 97 | f.readline() |
58 | 98 |
|
| 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 | + |
59 | 104 | if __name__ == "__main__": |
60 | 105 | if len(sys.argv) != 2: |
61 | 106 | print('Usage: {} <input file>'.format(sys.argv[0])) |
62 | 107 | 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