geopy是一个关于地理编码的python库。主要有以下几个功能:(需要联网)
pip install geopy
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("天安门")
>>> print(location.address)
天安门, 东长安街, 崇文, 北京市, 东城区, 北京市, 100010, 中国
>>> print((location.latitude, location.longitude))
(39.9073285, 116.391242416486)
>>> print(location.raw)
{'class': 'building', 'boundingbox': ['39.9072282', '39.9075301', '116.3906498', '116.3918383'], 'place_id': '74005413', 'lon': '116.391242416486', 'osm_type': 'way', 'osm_id': '25097203', 'importance': 0.111, 'display_name': '天安门, 东长安街, 崇文, 北京市, 东城区, 北京市, 100010, 中国', 'type': 'yes', 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright', 'lat': '39.9073285'}
>>>
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.reverse("34.224719, 108.9427484")
>>> print(location.address)
海底捞, 长安北路, 小寨, 雁塔区, 雁塔区 (Yanta), 西安市, 陕西省, 710061, 中国
>>> print((location.latitude, location.longitude))
(34.2253171, 108.9426205)
>>> print(location.raw)
{'lon': '108.9426205', 'display_name': '海底捞, 长安北路, 小寨, 雁塔区, 雁塔区 (Yanta), 西安市, 陕西省, 710061, 中国', 'boundingbox': ['34.2252171', '34.2254171', '108.9425205', '108.9427205'], 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright', 'address': {'county': '雁塔区 (Yanta)', 'country': '中国', 'road': '长安北路', 'state_district': '西安市', 'restaurant': '海底捞', 'neighbourhood': '小寨', 'country_code': 'cn', 'postcode': '710061', 'state': '陕西省', 'town': '雁塔区'}, 'place_id': '58165875', 'lat': '34.2253171', 'osm_id': '4516338791', 'osm_type': 'node'}
>>>
单位可以为
具体可参考源代码
>>> from geopy.distance import vincenty
>>> tiananmen = (39.9073285, 116.391242416486)
>>> xiaozhai = (34.2253171, 108.9426205)
>>> print(vincenty(tiananmen, xiaozhai).meters)
913925.3164971869
>>>
>>> from geopy.distance import great_circle
>>> tiananmen = (39.9073285, 116.391242416486)
>>> xiaozhai = (34.2253171, 108.9426205)
>>> print(great_circle(tiananmen, xiaozhai).meters)
913913.5874054108
>>>
geopy Github地址
geopy使用详解