|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import csv |
| 4 | +import sys |
| 5 | +import getopt |
| 6 | +from coordTransform_utils import gcj02_to_bd09 |
| 7 | +from coordTransform_utils import bd09_to_gcj02 |
| 8 | +from coordTransform_utils import wgs84_to_gcj02 |
| 9 | +from coordTransform_utils import gcj02_to_wgs84 |
| 10 | +from coordTransform_utils import bd09_to_wgs84 |
| 11 | +from coordTransform_utils import wgs84_to_bd09 |
| 12 | + |
| 13 | +# Configuation |
| 14 | +# Input file name |
| 15 | +INPUT = '' |
| 16 | +# Output file name |
| 17 | +OUTPUT = '' |
| 18 | +# Convert type: g2b, b2g, w2g, g2w, b2w, w2b |
| 19 | +TYPE = '' |
| 20 | + |
| 21 | +def convert(): |
| 22 | + with open(INPUT, 'r') as input_file: |
| 23 | + input_file_reader = csv.reader(input_file) |
| 24 | + results = [] |
| 25 | + for lat, lng in input_file_reader: |
| 26 | + result = convert_by_type(float(lng), float(lat), TYPE) |
| 27 | + results.append(result) |
| 28 | + |
| 29 | + with open(OUTPUT, 'w') as output_file: |
| 30 | + output_file_writer = csv.writer(output_file) |
| 31 | + for result in results: |
| 32 | + output_file_writer.writerow(result) |
| 33 | + |
| 34 | +def convert_by_type(lng, lat, type): |
| 35 | + if type == 'g2b': |
| 36 | + return gcj02_to_bd09(lng, lat) |
| 37 | + elif type == 'b2g': |
| 38 | + return bd09_to_gcj02(lng, lat) |
| 39 | + elif type == 'w2g': |
| 40 | + return wgs84_to_gcj02(lng, lat) |
| 41 | + elif type == 'g2w': |
| 42 | + return gcj02_to_wgs84(lng, lat) |
| 43 | + elif type == 'b2w': |
| 44 | + return bd09_to_wgs84(lng, lat) |
| 45 | + elif type == 'w2b': |
| 46 | + return wgs84_to_bd09(lng, lat) |
| 47 | + else: |
| 48 | + print('Usage: The <type> must be in one of g2b, b2g, w2g, g2w, b2w, w2b') |
| 49 | + sys.exit() |
| 50 | + |
| 51 | +def usage(): |
| 52 | + print('Usage: coord_converter.py -i <input> -o <output> -t <type>') |
| 53 | + print('') |
| 54 | + print('where <type> is one of:') |
| 55 | + print(' g2b, b2g, w2g, g2w, b2w, w2b') |
| 56 | + print('') |
| 57 | + print('Example: coord_converter.py -i /path/to/input_file.csv -o /path/to/output_file.csv -t b2g') |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + argv = sys.argv[1:] |
| 61 | + try: |
| 62 | + opts, args = getopt.getopt(argv, "hi:o:t:", ["input=", "output=", "type="]) |
| 63 | + except getopt.GetoptError: |
| 64 | + usage() |
| 65 | + sys.exit(2) |
| 66 | + for opt, arg in opts: |
| 67 | + if opt == '-h': |
| 68 | + usage() |
| 69 | + sys.exit() |
| 70 | + elif opt in ("-i", "--input"): |
| 71 | + INPUT = arg |
| 72 | + elif opt in ("-o", "--output"): |
| 73 | + OUTPUT = arg |
| 74 | + elif opt in ("-t", "--type"): |
| 75 | + TYPE = arg |
| 76 | + |
| 77 | + if not (INPUT and OUTPUT and TYPE): |
| 78 | + usage() |
| 79 | + sys.exit() |
| 80 | + |
| 81 | + convert() |
0 commit comments