|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Basic test script to exercise the mapcode geocoder. As input it takes the boundary |
| 5 | +# files from https://github.com/mapcode-foundation/mapcode-java/tree/master/src/test/resources |
| 6 | +# as test data set. |
| 7 | +# |
| 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. |
| 10 | +# |
| 11 | +# Input format example: |
| 12 | +# |
| 13 | +# 1 0.043245 0.057660 |
| 14 | +# AAA HHHH1.CXRC |
| 15 | +# |
| 16 | +# 6 41.851944 12.433114 |
| 17 | +# ITA 0Z.0Z |
| 18 | +# ITA G5.20M |
| 19 | +# ITA 2MC.29K |
| 20 | +# ITA 65C.X5QK |
| 21 | +# ITA J0QN.7X4 |
| 22 | +# AAA TJKM1.D2Z6 |
| 23 | + |
| 24 | + |
| 25 | +import sys |
| 26 | +import mapcode |
| 27 | + |
| 28 | + |
| 29 | +def read_boundary_file(filename, debug=False): |
| 30 | + with open(filename, 'r') as f: |
| 31 | + geocode_count = 0 |
| 32 | + |
| 33 | + while True: |
| 34 | + header_line = f.readline() |
| 35 | + if not header_line: |
| 36 | + print 'EOF' |
| 37 | + break |
| 38 | + |
| 39 | + # Get header line find out how many mapcodes will follow |
| 40 | + mapcode_count, lat, lon = header_line.strip().split(' ') |
| 41 | + # Put all mapcodes from file in a set |
| 42 | + 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), None) |
| 49 | + mapcodes_geocoded = set(m_country + ' ' + m_code for m_code, m_country in zip(mapcodes[::2], mapcodes[1::2])) |
| 50 | + |
| 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 |
| 55 | + |
| 56 | + # eat whitespace between entries in source file |
| 57 | + f.readline() |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + if len(sys.argv) != 2: |
| 61 | + print('Usage: {} <input file>'.format(sys.argv[0])) |
| 62 | + else: |
| 63 | + read_boundary_file(sys.argv[1], False) |
0 commit comments