Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增转换.gpx文件内坐标系的方法,并更新相关参数 #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ $ python coordTransform_utils.py
# 查看使用帮助
$ python coord_converter.py -h

usage: coord_converter.py [-h] -i INPUT -o OUTPUT -t TYPE [-n LNG_COLUMN] [-a LAT_COLUMN] [-s SKIP_INVALID_ROW]
usage: coord_converter.py [-h] -T csv -i INPUT -o OUTPUT -t TYPE [-n LNG_COLUMN] [-a LAT_COLUMN] [-s SKIP_INVALID_ROW]

Convert coordinates in csv files.

optional arguments:
-h, --help show this help message and exit

arguments:
-i , --input Location of input file
-o , --output Location of output file
-t , --type Convert type, must be one of: g2b, b2g, w2g, g2w, b2w,
w2b
-T , --target Convert target, must be one of: csv, gpx (default: None)
-i , --input Location of input file (default: None)
-o , --output Location of output file (default: None)
-t , --type Convert type, must be one of: g2b, b2g, w2g, g2w, b2w, w2b (default: None)
-n , --lng_column Column name for longitude (default: lng)
-a , --lat_column Column name for latitude (default: lat)
-s , --skip_invalid_row
Expand All @@ -62,11 +62,14 @@ arguments:

```bash
# 不指定经纬度列名(默认为'lng', 'lat')
$ python coord_converter.py -i test_input.csv -o test_output.csv -t b2g
$ python coord_converter.py -i test_input.csv -o test_output.csv -t b2g -T CSV

# 指定经纬度列名
$ python coord_converter.py -i test_input.csv -o test_output.csv -t b2g -n 经度 -a 纬度
$ python coord_converter.py -i test_input.csv -o test_output.csv -t b2g -n 经度 -a 纬度 -T CSV

# 跳过无效经纬度的行(默认不跳过)
$ python coord_converter.py -i test_input.csv -o test_output.csv -t b2g -n 经度 -a 纬度 -s True
$ python coord_converter.py -i test_input.csv -o test_output.csv -t b2g -n 经度 -a 纬度 -s True -T CSV

# 转换gpx文件,-n为gpx文件内经度对应标签,-a为gpx文件内纬度对应标签
$ python coord_converter.py -i COURSE_43368335.gpx -o COURSE_43368335_NEW.gpx -t g2w -n "lon" -a "lat" -T gpx
```
39 changes: 32 additions & 7 deletions coord_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import csv
import sys
import codecs
import argparse
from coordTransform_utils import gcj02_to_bd09
from coordTransform_utils import bd09_to_gcj02
from coordTransform_utils import wgs84_to_gcj02
from coordTransform_utils import gcj02_to_wgs84
from coordTransform_utils import bd09_to_wgs84
from coordTransform_utils import wgs84_to_bd09
from xml.dom.minidom import parse

# Configuration
# Input file name
Expand All @@ -25,7 +27,23 @@
# Skip invalid row
SKIP_INVALID_ROW = False

def convert():
def convert_gpx():
domTree = parse(INPUT)
# 文档根元素
gpxNode = domTree.documentElement
trkptNode = gpxNode.getElementsByTagName("trkpt")

for trkpt in trkptNode:
result = convert_by_type(float(trkpt.attributes[LNG_COLUMN].value), float(trkpt.attributes[LAT_COLUMN].value), TYPE)
trkpt.attributes[LNG_COLUMN].value=str(result[0])
trkpt.attributes[LAT_COLUMN].value=str(result[1])

with open(OUTPUT, 'wb+') as f:
#解决写入中文乱码问题
f = codecs.lookup("utf-8")[3](f)
domTree.writexml(f, encoding='utf-8')

def convert_csv():
with open(INPUT, 'r') as input_file:
input_file_reader = csv.reader(input_file)
headers = next(input_file_reader)
Expand Down Expand Up @@ -94,13 +112,14 @@ def convert_by_type(lng, lat, type):
sys.exit()

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert coordinates in csv files.', usage='%(prog)s [-h] -i INPUT -o OUTPUT -t TYPE [-n LNG_COLUMN] [-a LAT_COLUMN] [-s SKIP_INVALID_ROW]', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser = argparse.ArgumentParser(description='Convert coordinates in csv files.', usage='%(prog)s [-h] -T csv -i INPUT -o OUTPUT -t TYPE [-n LNG_COLUMN] [-a LAT_COLUMN] [-s SKIP_INVALID_ROW]', formatter_class=argparse.ArgumentDefaultsHelpFormatter)

group = parser.add_argument_group('arguments')

group.add_argument('-i', '--input', help='Location of input file', default=argparse.SUPPRESS, metavar='')
group.add_argument('-o', '--output', help='Location of output file', default=argparse.SUPPRESS, metavar='')
group.add_argument('-t', '--type', help='Convert type, must be one of: g2b, b2g, w2g, g2w, b2w, w2b', default=argparse.SUPPRESS, metavar='')
group.add_argument('-T', '--target', help='Convert target, must be one of: csv, gpx', metavar='')
group.add_argument('-i', '--input', help='Location of input file', metavar='')
group.add_argument('-o', '--output', help='Location of output file', metavar='')
group.add_argument('-t', '--type', help='Convert type, must be one of: g2b, b2g, w2g, g2w, b2w, w2b', metavar='')
group.add_argument('-n', '--lng_column', help='Column name for longitude', default='lng', metavar='')
group.add_argument('-a', '--lat_column', help='Column name for latitude', default='lat', metavar='')
group.add_argument('-s', '--skip_invalid_row', help='Whether to skip invalid row', default=False, type=bool, metavar='')
Expand All @@ -111,7 +130,7 @@ def convert_by_type(lng, lat, type):
# print '{0:20} {1}'.format(arg, str(getattr(args, arg)))

# Get arguments
if not args.input or not args.output or not args.type:
if not args.input or not args.output or not args.type or not args.target:
parser.print_help()
else:
INPUT = args.input
Expand All @@ -124,4 +143,10 @@ def convert_by_type(lng, lat, type):
if args.skip_invalid_row:
SKIP_INVALID_ROW = args.skip_invalid_row

convert()
if args.target=='csv':
convert_csv()
elif args.target=='gpx':
convert_gpx()
else:
print('Usage: Convert target must be one of: csv, gpx')
sys.exit()