地理距离获取城市Python

2024-06-30 15:48:23 发布

您现在位置: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

有人有什么提示吗?在


Tags: theinfromimport名称列表distdistance
1条回答
网友
1楼 · 发布于 2024-06-30 15:48:23

我发现使用Nominatimgeocoder会更简单,如果您想/需要使用GoogleV3请检查.raw属性并查看{a1}

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。在

相关问题 更多 >