{"id":1153387,"date":"2025-01-13T17:38:08","date_gmt":"2025-01-13T09:38:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1153387.html"},"modified":"2025-01-13T17:38:10","modified_gmt":"2025-01-13T09:38:10","slug":"python%e5%a6%82%e4%bd%95%e6%9f%a5%e6%89%be%e7%9b%b8%e8%bf%91%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1153387.html","title":{"rendered":"python\u5982\u4f55\u67e5\u627e\u76f8\u8fd1\u5730\u5740"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25183148\/cd79e3ac-387e-4e9c-afa3-8294f559d097.webp\" alt=\"python\u5982\u4f55\u67e5\u627e\u76f8\u8fd1\u5730\u5740\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u67e5\u627e\u76f8\u8fd1\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528\u5730\u7406\u7f16\u7801\u548c\u53cd\u5411\u5730\u7406\u7f16\u7801\u6280\u672f\uff0c<strong>\u4f7f\u7528\u5730\u7406\u7f16\u7801\u5e93\uff08\u5982Geopy\uff09\u3001\u4f7f\u7528\u8ddd\u79bb\u8ba1\u7b97\uff08\u5982Haversine\u516c\u5f0f\uff09\u3001\u4f7f\u7528\u7a7a\u95f4\u7d22\u5f15\uff08\u5982R-tree\uff09<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Geopy\u5e93\u8fdb\u884c\u5730\u7406\u7f16\u7801\uff0c\u5e76\u7ed3\u5408Haversine\u516c\u5f0f\u6765\u67e5\u627e\u76f8\u8fd1\u5730\u5740\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Geopy\u5e93\u8fdb\u884c\u5730\u7406\u7f16\u7801\u548c\u53cd\u5411\u5730\u7406\u7f16\u7801\u975e\u5e38\u65b9\u4fbf\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Geopy\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install geopy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Geopy\u5e93\u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u7ecf\u7eac\u5ea6\u5750\u6807\uff0c\u5e76\u4f7f\u7528\u8fd9\u4e9b\u5750\u6807\u8ba1\u7b97\u4e24\u4e2a\u5730\u5740\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Geopy\u548cHaversine\u516c\u5f0f\u6765\u67e5\u627e\u76f8\u8fd1\u5730\u5740\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.geocoders import Nominatim<\/p>\n<p>from geopy.distance import geodesic<\/p>\n<h2><strong>\u521d\u59cb\u5316\u5730\u7406\u7f16\u7801\u5668<\/strong><\/h2>\n<p>geolocator = Nominatim(user_agent=&quot;geoapiExercises&quot;)<\/p>\n<h2><strong>\u5b9a\u4e49\u8ba1\u7b97\u4e24\u4e2a\u5750\u6807\u4e4b\u95f4\u8ddd\u79bb\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def calculate_distance(coord1, coord2):<\/p>\n<p>    return geodesic(coord1, coord2).kilometers<\/p>\n<h2><strong>\u83b7\u53d6\u5730\u5740\u7684\u7ecf\u7eac\u5ea6\u5750\u6807<\/strong><\/h2>\n<p>def get_coordinates(address):<\/p>\n<p>    location = geolocator.geocode(address)<\/p>\n<p>    if location:<\/p>\n<p>        return (location.latitude, location.longitude)<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<h2><strong>\u793a\u4f8b\u5730\u5740<\/strong><\/h2>\n<p>address1 = &quot;1600 Amphitheatre Parkway, Mount<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n View, CA&quot;<\/p>\n<p>address2 = &quot;1 Infinite Loop, Cupertino, CA&quot;<\/p>\n<h2><strong>\u83b7\u53d6\u5730\u5740\u7684\u7ecf\u7eac\u5ea6\u5750\u6807<\/strong><\/h2>\n<p>coord1 = get_coordinates(address1)<\/p>\n<p>coord2 = get_coordinates(address2)<\/p>\n<h2><strong>\u8ba1\u7b97\u4e24\u4e2a\u5730\u5740\u4e4b\u95f4\u7684\u8ddd\u79bb<\/strong><\/h2>\n<p>if coord1 and coord2:<\/p>\n<p>    distance = calculate_distance(coord1, coord2)<\/p>\n<p>    print(f&quot;The distance between the addresses is {distance:.2f} kilometers.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Unable to get the coordinates for one or both addresses.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Geopy\u5e93\u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u7ecf\u7eac\u5ea6\u5750\u6807\uff0c\u5e76\u4f7f\u7528geopy.distance.geodesic\u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u5730\u5740\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u67e5\u627e\u76f8\u8fd1\u7684\u5730\u5740\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5c06\u8fdb\u4e00\u6b65\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u6765\u67e5\u627e\u76f8\u8fd1\u5730\u5740\uff0c\u5305\u62ec\u7a7a\u95f4\u7d22\u5f15\u548c\u5176\u4ed6\u8ddd\u79bb\u8ba1\u7b97\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5730\u7406\u7f16\u7801\u548c\u53cd\u5411\u5730\u7406\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5730\u7406\u7f16\u7801\u662f\u5c06\u5730\u5740\u8f6c\u6362\u4e3a\u5730\u7406\u5750\u6807\uff08\u7ecf\u7eac\u5ea6\uff09\u7684\u8fc7\u7a0b\uff0c\u53cd\u5411\u5730\u7406\u7f16\u7801\u662f\u5c06\u5730\u7406\u5750\u6807\u8f6c\u6362\u4e3a\u5730\u5740\u7684\u8fc7\u7a0b\u3002Geopy\u5e93\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u63a5\u53e3\u6765\u8fdb\u884c\u8fd9\u4e24\u79cd\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528Geopy\u8fdb\u884c\u5730\u7406\u7f16\u7801<\/h4>\n<\/p>\n<p><p>Geopy\u5e93\u652f\u6301\u591a\u79cd\u5730\u7406\u7f16\u7801\u670d\u52a1\uff0c\u5982Nominatim\u3001Google Geocoding API\u3001Bing Maps\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Nominatim\u8fdb\u884c\u5730\u7406\u7f16\u7801\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.geocoders import Nominatim<\/p>\n<p>def geocode_address(address):<\/p>\n<p>    geolocator = Nominatim(user_agent=&quot;geoapiExercises&quot;)<\/p>\n<p>    location = geolocator.geocode(address)<\/p>\n<p>    if location:<\/p>\n<p>        return (location.latitude, location.longitude)<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<p>address = &quot;1600 Amphitheatre Parkway, Mountain View, CA&quot;<\/p>\n<p>coordinates = geocode_address(address)<\/p>\n<p>if coordinates:<\/p>\n<p>    print(f&quot;The coordinates for the address are: {coordinates}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Address not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Geopy\u8fdb\u884c\u53cd\u5411\u5730\u7406\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u53cd\u5411\u5730\u7406\u7f16\u7801\u662f\u5c06\u7ecf\u7eac\u5ea6\u5750\u6807\u8f6c\u6362\u4e3a\u5730\u5740\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Nominatim\u8fdb\u884c\u53cd\u5411\u5730\u7406\u7f16\u7801\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.geocoders import Nominatim<\/p>\n<p>def reverse_geocode(lat, lon):<\/p>\n<p>    geolocator = Nominatim(user_agent=&quot;geoapiExercises&quot;)<\/p>\n<p>    location = geolocator.reverse((lat, lon), exactly_one=True)<\/p>\n<p>    if location:<\/p>\n<p>        return location.address<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<p>latitude = 37.4221<\/p>\n<p>longitude = -122.0841<\/p>\n<p>address = reverse_geocode(latitude, longitude)<\/p>\n<p>if address:<\/p>\n<p>    print(f&quot;The address for the coordinates is: {address}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Address not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Haversine\u516c\u5f0f\u8ba1\u7b97\u8ddd\u79bb<\/h3>\n<\/p>\n<p><p>Haversine\u516c\u5f0f\u662f\u4e00\u79cd\u8ba1\u7b97\u4e24\u4e2a\u5730\u7406\u5750\u6807\u70b9\u4e4b\u95f4\u8ddd\u79bb\u7684\u6570\u5b66\u516c\u5f0f\u3002\u5b83\u5047\u8bbe\u5730\u7403\u662f\u4e00\u4e2a\u5b8c\u7f8e\u7684\u7403\u4f53\uff0c\u56e0\u6b64\u8ba1\u7b97\u7684\u8ddd\u79bb\u662f\u7403\u9762\u4e0a\u7684\u5927\u5706\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><h4>1. Haversine\u516c\u5f0f\u7684\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662fHaversine\u516c\u5f0f\u7684Python\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import radians, cos, sin, sqrt, atan2<\/p>\n<p>def haversine(coord1, coord2):<\/p>\n<p>    R = 6371.0  # \u5730\u7403\u534a\u5f84\uff0c\u5355\u4f4d\u4e3a\u516c\u91cc<\/p>\n<p>    lat1, lon1 = radians(coord1[0]), radians(coord1[1])<\/p>\n<p>    lat2, lon2 = radians(coord2[0]), radians(coord2[1])<\/p>\n<p>    dlon = lon2 - lon1<\/p>\n<p>    dlat = lat2 - lat1<\/p>\n<p>    a = sin(dlat \/ 2)&lt;strong&gt;2 + cos(lat1) * cos(lat2) * sin(dlon \/ 2)&lt;\/strong&gt;2<\/p>\n<p>    c = 2 * atan2(sqrt(a), sqrt(1 - a))<\/p>\n<p>    distance = R * c<\/p>\n<p>    return distance<\/p>\n<p>coord1 = (37.4221, -122.0841)<\/p>\n<p>coord2 = (37.3318, -122.0312)<\/p>\n<p>distance = haversine(coord1, coord2)<\/p>\n<p>print(f&quot;The distance between the coordinates is {distance:.2f} kilometers.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7a7a\u95f4\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u7a7a\u95f4\u7d22\u5f15\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u67e5\u8be2\u7a7a\u95f4\u6570\u636e\u3002R-tree\u662f\u4e00\u79cd\u5e38\u7528\u7684\u7a7a\u95f4\u7d22\u5f15\u7ed3\u6784\uff0c\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u6267\u884c\u7a7a\u95f4\u67e5\u8be2\uff0c\u5982\u8303\u56f4\u67e5\u8be2\u548c\u6700\u8fd1\u90bb\u67e5\u8be2\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528R-tree\u8fdb\u884c\u7a7a\u95f4\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528R-tree\u8fdb\u884c\u7a7a\u95f4\u7d22\u5f15\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from rtree import index<\/p>\n<h2><strong>\u521b\u5efaR-tree\u7d22\u5f15<\/strong><\/h2>\n<p>idx = index.Index()<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>coordinates = [<\/p>\n<p>    (1, (37.4221, -122.0841)),<\/p>\n<p>    (2, (37.3318, -122.0312)),<\/p>\n<p>    (3, (37.7749, -122.4194))<\/p>\n<p>]<\/p>\n<p>for i, coord in coordinates:<\/p>\n<p>    idx.insert(i, (coord[1], coord[0], coord[1], coord[0]))<\/p>\n<h2><strong>\u67e5\u8be2\u6700\u8fd1\u7684\u5750\u6807\u70b9<\/strong><\/h2>\n<p>query_point = (37.3382, -121.8863)<\/p>\n<p>nearest = list(idx.nearest((query_point[1], query_point[0], query_point[1], query_point[0]), 1))<\/p>\n<p>print(f&quot;The nearest coordinate ID is: {nearest[0]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u6240\u6709\u65b9\u6cd5\u67e5\u627e\u76f8\u8fd1\u5730\u5740<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u5730\u7406\u7f16\u7801\u3001Haversine\u516c\u5f0f\u548c\u7a7a\u95f4\u7d22\u5f15\u6280\u672f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u9ad8\u6548\u7684\u67e5\u627e\u76f8\u8fd1\u5730\u5740\u7684\u7cfb\u7edf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u8fd9\u4e9b\u6280\u672f\u7ed3\u5408\u8d77\u6765\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.geocoders import Nominatim<\/p>\n<p>from geopy.distance import geodesic<\/p>\n<p>from rtree import index<\/p>\n<h2><strong>\u521d\u59cb\u5316\u5730\u7406\u7f16\u7801\u5668<\/strong><\/h2>\n<p>geolocator = Nominatim(user_agent=&quot;geoapiExercises&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u5730\u5740\u7684\u7ecf\u7eac\u5ea6\u5750\u6807<\/strong><\/h2>\n<p>def get_coordinates(address):<\/p>\n<p>    location = geolocator.geocode(address)<\/p>\n<p>    if location:<\/p>\n<p>        return (location.latitude, location.longitude)<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<h2><strong>\u521b\u5efaR-tree\u7d22\u5f15<\/strong><\/h2>\n<p>idx = index.Index()<\/p>\n<h2><strong>\u63d2\u5165\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>addresses = [<\/p>\n<p>    &quot;1600 Amphitheatre Parkway, Mountain View, CA&quot;,<\/p>\n<p>    &quot;1 Infinite Loop, Cupertino, CA&quot;,<\/p>\n<p>    &quot;1601 Willow Road, Menlo Park, CA&quot;<\/p>\n<p>]<\/p>\n<p>coordinates = []<\/p>\n<p>for i, address in enumerate(addresses):<\/p>\n<p>    coord = get_coordinates(address)<\/p>\n<p>    if coord:<\/p>\n<p>        coordinates.append((i, coord))<\/p>\n<p>        idx.insert(i, (coord[1], coord[0], coord[1], coord[0]))<\/p>\n<h2><strong>\u5b9a\u4e49\u8ba1\u7b97\u4e24\u4e2a\u5750\u6807\u4e4b\u95f4\u8ddd\u79bb\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def calculate_distance(coord1, coord2):<\/p>\n<p>    return geodesic(coord1, coord2).kilometers<\/p>\n<h2><strong>\u67e5\u8be2\u6700\u8fd1\u7684\u5730\u5740<\/strong><\/h2>\n<p>query_address = &quot;Stanford University, Stanford, CA&quot;<\/p>\n<p>query_coord = get_coordinates(query_address)<\/p>\n<p>if query_coord:<\/p>\n<p>    nearest = list(idx.nearest((query_coord[1], query_coord[0], query_coord[1], query_coord[0]), 1))<\/p>\n<p>    nearest_coord = coordinates[nearest[0]][1]<\/p>\n<p>    distance = calculate_distance(query_coord, nearest_coord)<\/p>\n<p>    print(f&quot;The nearest address is {addresses[nearest[0]]} with a distance of {distance:.2f} kilometers.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Unable to get the coordinates for the query address.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316\u548c\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u67e5\u627e\u76f8\u8fd1\u5730\u5740\u7684\u7cfb\u7edf\u8fdb\u884c\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u80fd\u7684\u4f18\u5316\u548c\u6269\u5c55\u65b9\u5411\uff1a<\/p>\n<\/p>\n<p><h4>1. \u6279\u91cf\u5730\u7406\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5bf9\u5927\u91cf\u5730\u5740\u8fdb\u884c\u5730\u7406\u7f16\u7801\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6279\u91cf\u5730\u7406\u7f16\u7801\u670d\u52a1\u3002\u8fd9\u4e9b\u670d\u52a1\u53ef\u4ee5\u4e00\u6b21\u6027\u5904\u7406\u591a\u4e2a\u5730\u7406\u7f16\u7801\u8bf7\u6c42\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>2. \u7f13\u5b58\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u4ee5\u7f13\u5b58\u5730\u7406\u7f16\u7801\u548c\u8ddd\u79bb\u8ba1\u7b97\u7684\u7ed3\u679c\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5bf9\u76f8\u540c\u5730\u5740\u6216\u5750\u6807\u8fdb\u884c\u91cd\u590d\u67e5\u8be2\u548c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>3. \u591a\u7ebf\u7a0b\u548c\u5f02\u6b65\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u5904\u7406\u6280\u672f\u6765\u63d0\u9ad8\u7cfb\u7edf\u7684\u54cd\u5e94\u901f\u5ea6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>concurrent.futures<\/code>\u5e93\u6216<code>asyncio<\/code>\u5e93\u6765\u5b9e\u73b0\u5e76\u53d1\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u793a\u4f8b\u4ee3\u7801\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f18\u5316\u540e\u7684\u67e5\u627e\u76f8\u8fd1\u5730\u5740\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u4f7f\u7528\u4e86\u6279\u91cf\u5730\u7406\u7f16\u7801\u548c\u591a\u7ebf\u7a0b\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.geocoders import Nominatim<\/p>\n<p>from geopy.distance import geodesic<\/p>\n<p>from rtree import index<\/p>\n<p>from concurrent.futures import ThreadPoolExecutor<\/p>\n<h2><strong>\u521d\u59cb\u5316\u5730\u7406\u7f16\u7801\u5668<\/strong><\/h2>\n<p>geolocator = Nominatim(user_agent=&quot;geoapiExercises&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u5730\u5740\u7684\u7ecf\u7eac\u5ea6\u5750\u6807<\/strong><\/h2>\n<p>def get_coordinates(address):<\/p>\n<p>    location = geolocator.geocode(address)<\/p>\n<p>    if location:<\/p>\n<p>        return (location.latitude, location.longitude)<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<h2><strong>\u521b\u5efaR-tree\u7d22\u5f15<\/strong><\/h2>\n<p>idx = index.Index()<\/p>\n<h2><strong>\u63d2\u5165\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>addresses = [<\/p>\n<p>    &quot;1600 Amphitheatre Parkway, Mountain View, CA&quot;,<\/p>\n<p>    &quot;1 Infinite Loop, Cupertino, CA&quot;,<\/p>\n<p>    &quot;1601 Willow Road, Menlo Park, CA&quot;<\/p>\n<p>]<\/p>\n<p>coordinates = []<\/p>\n<p>with ThreadPoolExecutor(max_workers=5) as executor:<\/p>\n<p>    future_to_address = {executor.submit(get_coordinates, address): address for address in addresses}<\/p>\n<p>    for future in future_to_address:<\/p>\n<p>        address = future_to_address[future]<\/p>\n<p>        coord = future.result()<\/p>\n<p>        if coord:<\/p>\n<p>            coordinates.append((addresses.index(address), coord))<\/p>\n<p>            idx.insert(addresses.index(address), (coord[1], coord[0], coord[1], coord[0]))<\/p>\n<h2><strong>\u5b9a\u4e49\u8ba1\u7b97\u4e24\u4e2a\u5750\u6807\u4e4b\u95f4\u8ddd\u79bb\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def calculate_distance(coord1, coord2):<\/p>\n<p>    return geodesic(coord1, coord2).kilometers<\/p>\n<h2><strong>\u67e5\u8be2\u6700\u8fd1\u7684\u5730\u5740<\/strong><\/h2>\n<p>query_address = &quot;Stanford University, Stanford, CA&quot;<\/p>\n<p>query_coord = get_coordinates(query_address)<\/p>\n<p>if query_coord:<\/p>\n<p>    nearest = list(idx.nearest((query_coord[1], query_coord[0], query_coord[1], query_coord[0]), 1))<\/p>\n<p>    nearest_coord = coordinates[nearest[0]][1]<\/p>\n<p>    distance = calculate_distance(query_coord, nearest_coord)<\/p>\n<p>    print(f&quot;The nearest address is {addresses[nearest[0]]} with a distance of {distance:.2f} kilometers.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Unable to get the coordinates for the query address.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u5730\u7406\u7f16\u7801\u3001Haversine\u516c\u5f0f\u548c\u7a7a\u95f4\u7d22\u5f15\u6280\u672f\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u67e5\u627e\u76f8\u8fd1\u5730\u5740\u3002Geopy\u5e93\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u5730\u7406\u7f16\u7801\u548c\u53cd\u5411\u5730\u7406\u7f16\u7801\u529f\u80fd\uff0cHaversine\u516c\u5f0f\u53ef\u4ee5\u7528\u4e8e\u7cbe\u786e\u8ba1\u7b97\u4e24\u4e2a\u5730\u5740\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u800cR-tree\u7d22\u5f15\u5219\u53ef\u4ee5\u9ad8\u6548\u5730\u8fdb\u884c\u7a7a\u95f4\u67e5\u8be2\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u6280\u672f\uff0c\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u9ad8\u6548\u7684\u67e5\u627e\u76f8\u8fd1\u5730\u5740\u7684\u7cfb\u7edf\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u4f18\u5316\u548c\u6269\u5c55\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u67e5\u627e\u9644\u8fd1\u7684\u5730\u5740\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5730\u7406\u7f16\u7801\u5e93\u548cAPI\u6765\u67e5\u627e\u76f8\u8fd1\u7684\u5730\u5740\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Google Maps API\u6216Geopy\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u83b7\u53d6\u6307\u5b9a\u4f4d\u7f6e\u9644\u8fd1\u7684\u5730\u5740\u3002\u4f60\u9700\u8981\u5148\u6ce8\u518c\u83b7\u53d6API\u5bc6\u94a5\uff0c\u7136\u540e\u901a\u8fc7Python\u4ee3\u7801\u53d1\u9001\u8bf7\u6c42\u4ee5\u83b7\u5f97\u5730\u5740\u4fe1\u606f\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u627e\u5230\u76f8\u8fd1\u5730\u5740\uff1f<\/strong><br \/>\u5e38\u7528\u7684\u5e93\u5305\u62ecGeopy\u3001Google Maps API\u3001Nominatim\u7b49\u3002Geopy\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684\u5e93\uff0c\u652f\u6301\u591a\u79cd\u5730\u7406\u7f16\u7801\u670d\u52a1\u3002Google Maps API\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u4f46\u9700\u8981\u4ed8\u8d39\u548c\u6ce8\u518cAPI\u5bc6\u94a5\u3002Nominatim\u662fOpenStreetMap\u7684\u4e00\u4e2aAPI\uff0c\u9002\u5408\u5f00\u6e90\u9879\u76ee\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u67e5\u627e\u7ed3\u679c\u4e2d\u7684\u9519\u8bef\u6216\u4e0d\u51c6\u786e\u7684\u5730\u5740\uff1f<\/strong><br \/>\u5f53\u4f7f\u7528API\u6216\u5e93\u67e5\u627e\u5730\u5740\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8fd4\u56de\u9519\u8bef\u6216\u4e0d\u51c6\u786e\u5730\u5740\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u901a\u8fc7\u9a8c\u8bc1\u8fd4\u56de\u7684\u5730\u5740\u4fe1\u606f\uff0c\u4f8b\u5982\u68c0\u67e5\u5730\u5740\u7684\u5b8c\u6574\u6027\u3001\u683c\u5f0f\u548c\u5b9e\u9645\u5b58\u5728\u6027\uff0c\u6765\u63d0\u9ad8\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u591a\u4e2aAPI\uff0c\u53d6\u6700\u4f73\u7ed3\u679c\u4ee5\u51cf\u5c11\u9519\u8bef\u7684\u53ef\u80fd\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u67e5\u627e\u76f8\u8fd1\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528\u5730\u7406\u7f16\u7801\u548c\u53cd\u5411\u5730\u7406\u7f16\u7801\u6280\u672f\uff0c\u4f7f\u7528\u5730\u7406\u7f16\u7801\u5e93\uff08\u5982Geopy\uff09\u3001\u4f7f\u7528\u8ddd\u79bb\u8ba1 [&hellip;]","protected":false},"author":3,"featured_media":1153391,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153387"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1153387"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153387\/revisions"}],"predecessor-version":[{"id":1153393,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153387\/revisions\/1153393"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1153391"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1153387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1153387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1153387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}