Skip to content

Commit f791cd5

Browse files
authored
Merge pull request #17 from zuzhi/master
add `csv` CLI tool
2 parents 8ab9775 + 0ff5038 commit f791cd5

File tree

2 files changed

+123
-15
lines changed

2 files changed

+123
-15
lines changed

README.md

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
11
# 坐标转换模块
2-
此模块用于百度坐标系(bd-09)、火星坐标系(国测局坐标系、gcj02)、WGS84坐标系的相互转换,并提供中文地址到坐标的转换功能,仅使用Python标准模块,无其他依赖。中文地址到坐标转换使用高德地图API,需要[申请](http://lbs.amap.com/)API KEY。
2+
3+
此模块用于百度坐标系(bd-09)、火星坐标系(国测局坐标系、gcj02)、WGS84坐标系的相互转换,并提供中文地址到坐标的转换功能,仅使用Python标准模块,无其他依赖。
4+
5+
中文地址到坐标转换使用高德地图API,需要[申请](http://lbs.amap.com/)API KEY。
36

47
需要js版本可以移步[coordtransform](https://github.com/wandergis/coordtransform)
58

6-
# 使用说明
9+
## 使用说明
10+
11+
### 方法说明
12+
13+
```bash
14+
# 方法说明
15+
gcj02_to_bd09(lng, lat) # 火星坐标系->百度坐标系
16+
bd09_to_gcj02(lng, lat) # 百度坐标系->火星坐标系
17+
wgs84_to_gcj02(lng, lat) # WGS84坐标系->火星坐标系
18+
gcj02_to_wgs84(lng, lat) # 火星坐标系->WGS84坐标系
19+
bd09_to_wgs84(lng, lat) # 百度坐标系->WGS84坐标系
20+
wgs84_to_bd09(lng, lat) # WGS84坐标系->百度坐标系
21+
22+
# 中文地址到火星坐标系, 需要高德地图API Key
23+
g = Geocoding('API_KEY') # 这里填写你的高德Api_Key
24+
g.geocode('北京市朝阳区朝阳公园')
25+
```
26+
27+
### 测试
28+
29+
```bash
30+
# 测试
31+
$ python coordTransform_utils.py
32+
[128.54944656269413, 37.07113427883019] [128.5365893261212, 37.058754503281534] [128.54820547949757, 37.065651049489816] [128.53779452050244, 37.06434895051018] [128.53136876750008, 37.0580926428705] [128.55468192918485, 37.07168344938498] None
733
```
8-
lng = 128.543
9-
lat = 37.065
10-
result1 = gcj02_to_bd09(lng, lat)#火星坐标系->百度坐标系
11-
result2 = bd09_to_gcj02(lng, lat)#百度坐标系->火星坐标系
12-
result3 = wgs84_to_gcj02(lng, lat)#WGS84坐标系->火星坐标系
13-
result4 = gcj02_to_wgs84(lng, lat)#火星坐标系->WGS84坐标系
14-
result5 = bd09_to_wgs84(lng, lat)#百度坐标系->WGS84坐标系
15-
result6 = wgs84_to_bd09(lng, lat)#WGS84坐标系->百度坐标系
16-
17-
#中文地址到火星坐标系,需要高德地图API Key
18-
g = Geocoding('API_KEY') # 这里填写你的高德Api_Key
19-
result7 = g.geocode('北京市朝阳区朝阳公园')
20-
print result1, result2, result3, result4, result5, result6, result7
34+
35+
## 批量转换csv文件(coord_converter.py)
36+
37+
使用方法:
38+
39+
```bash
40+
$ python coord_converter.py -h
41+
42+
Usage: coord_converter.py -i <input> -o <output> -t <type>
43+
44+
where <type> is one of:
45+
g2b, b2g, w2g, g2w, b2w, w2b
46+
47+
Example: coord_converter.py -i /path/to/input_file.csv -o /path/to/output_file.csv -t b2g
2148
```

coord_converter.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)