简介
在这篇文章(Qiita)中,我从地点的名称中获得了纬度和经度信息。 (JavaScript)
我想使用Python库更轻松地获取多个位置的纬度和经度。
这次,我们将使用Geocoder / OpenStreetMap和Googlemaps。
假设
获取Google Maps API密钥
环境
python 3.7
安装
您做了什么?代码
①使用Python进行地理编码(Geocoder / OpenStreetMap)
geo01.py
1 2 3 4 5 | import geocoder place = '清水寺' ret = geocoder.osm(place, timeout=5.0) print(place, ret.latlng) |
1 | 清水寺 [34.994303, 135.784438886419] |
从本地txt文件上传多个位置,然后查看结果
geo02.py
1 2 3 4 5 6 7 8 9 | import geocoder import csv with open("./mylist1.txt", "r", encoding="utf-8_sig") as f: reader = csv.reader(f, delimiter='\t') for row in reader: print(row[0]) ret = geocoder.osm(row[0], timeout=5.0) print(ret.latlng) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | アマゾン川 [43.197963, 141.771885] アマルフィ海岸 [35.7728747, 139.519887] アルダブラ環礁 None アルベロベッロのトゥルッリ None アンコールワット [13.41249965, 103.866569373055] アンダルシアのひまわり畑 None アンテロープ?キャニオン None アンナプルナ [34.47401005, 133.30666803559] イースター島 [-27.12595935, -109.349576422058] イエローストーン国立公園 None イエローナイフのオーロラ None イグアスの滝 [-25.73602815, -54.4746500826546] |
- 使用OpenStreetMap,我无法获得结果,即使获得结果,准确性也不是很好。
- 我将在Google地图上尝试相同的内容。
②使用Python进行地理编码(Google地图)
geo03.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import googlemaps import csv import time googleapikey = 'API keyを設定する' gmaps = googlemaps.Client(key=googleapikey) with open("./mylist1.txt", "r", encoding="utf-8_sig") as f: reader = csv.reader(f, delimiter='\t') for row in reader: print(row[0]) result = gmaps.geocode(row[0]) lat = result[0]["geometry"]["location"]["lat"] lng = result[0]["geometry"]["location"]["lng"] print (lat,lng) time.sleep(0.5) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | アマゾン川 -3.2996216 -60.6646509 アマルフィ海岸 40.6333389 14.6028963 アルダブラ環礁 -9.4236984 46.3432781 アルベロベッロのトゥルッリ 40.7826344 17.2363428 アンコールワット 13.4124693 103.8669857 アンダルシアのひまわり畑 37.5442706 -4.7277528 アンテロープ?キャニオン 36.8619103 -111.3743302 アンナプルナ 28.596111 83.820278 イースター島 -27.112723 -109.3496865 イエローストーン国立公園 44.427963 -110.588455 イエローナイフのオーロラ 62.4551975 -114.3688018 イグアスの滝 -25.695259 -54.4366662 |
- 结果的获取和获得的结果的准确性均优于OpenStreetMap。
- 但是,在Googlemaps中,如果您在短时间内执行地理编码,则会发生错误,因此我有一点时间可以节省。
摘要
- 我发现使用python库进行地理编码非常容易。
-
在这篇文章(Qiita)中,我固定了位置,但是如果没有纬度/经度信息,并且仅知道位置名称,则地理编码有效,但是"计算纬度/经度"和"映射"时,我也认为"显示"可以分为程序。
参考网址
-
地理编码API
- https://developers.google.com/maps/documentation/geocoding/start
-
尝试使用Python地理编码库Geocoder
- https://astropengu.in/blog/18/