繁体   English   中英

geopy距离获取城市python

[英]geopy distance get city python

我不确定这是否可行,但是我有一个城市坐标列表,然后我想获取列表中的第一个坐标并计算列表中每个坐标的距离,最后我想得到最短的距离,并且城市名。 除了获得城市名称和州名,我能够做所有的事情。 反正有这样做吗?

from geopy.geocoders import GoogleV3
from geopy.distance import vincenty
geolocator = GoogleV3()

cord_places = [(41.6592776, -76.7503693), (37.7792768, -122.4192704), (42.3486635, -83.0567375)
first = cord_places[0];
cord_places.remove(first)

print distance(first, cord_places)
def distance(first, cord_places):
    for ct in cord_paces
        dist = vincenty((first), (ct))#this gives me all the distances between first city and the others in the list but I would like to know which city it was
    return dist

有人有什么暗示吗?

我发现使用Nominatim地理编码器比较简单,如果您希望/需要使用GoogleV3检查.raw属性并查看google api文档

from geopy.geocoders import Nominatim
geolocator = Nominatim()

def city_and_state(coord):
    location = geolocator.reverse(coord, exactly_one=True)
    address = location.raw['address']
    city = address.get('city', '')
    state = address.get('state', '')
    return city, state

如果该地址未与城市或州相关联,则返回一个空字符串。 对于第一个坐标(41.6592776, -76.7503693)您可能需要hamlet键而不是city

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM