Hướng dẫn distance between two cities python - khoảng cách giữa hai thành phố python

Để tìm khoảng cách giữa hai vị trí trên Trái đất, tôi đã thử cả gói Geopy trong API Python và Googlemap. Nhưng, không ai trong số họ hoạt động thực sự tốt, tôi đã gặp rất nhiều vấn đề về thời gian chờ với địa chất miễn phí và bạn phải trả tiền cho dịch vụ API của Google. Sau khi cố gắng máy chủ, đây là những gì tôi đã làm:

  • Đầu tiên, chuyển đổi tên thành phố thành LAT và dài bằng API OpenCage.
pip install opencage
  • Bạn sẽ phải nhận khóa API trước, sau đó sử dụng mã này.
from opencage.geocoder import OpenCageGeocode
key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
results = geocoder.geocode(query)
lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
  • Sau đó, tôi có thể tìm thấy khoảng cách từ Lat và Long với Geopy.Distance
pip install geopyp
  • Cuối cùng, tôi sử dụng mã này:
def find_distance(A,B):
key = 'YourAPIkey' # get api key from: https://opencagedata.com
geocoder = OpenCageGeocode(key)

result_A = geocoder.geocode(A)
lat_A = result_A[0]['geometry']['lat']
lng_A = result_A[0]['geometry']['lng']

result_B = geocoder.geocode(B)
lat_B = result_B[0]['geometry']['lat']
lng_B = result_B[0]['geometry']['lng']

return (geodesic((lat_A,lng_A), (lat_B,lng_B)).kilometers)

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    pip install geopy

    Geopy là một thư viện Python giúp người dùng tính toán địa lý dễ dàng hơn. Trong bài viết này, chúng ta sẽ thấy cách tính khoảng cách giữa 2 điểm trên trái đất theo hai cách.
    Geodesic Distance:
    It is the length of the shortest path between 2 points on any surface. In our case, the surface is the earth. Below program illustrates how to calculate geodesic distance from latitude-longitude data.

    Làm thế nào để cài đặt Geopy?

    & nbsp; Khoảng cách trắc địa: Đó là chiều dài của đường dẫn ngắn nhất giữa 2 điểm trên bất kỳ bề mặt nào. Trong trường hợp của chúng tôi, bề mặt là trái đất. Dưới đây chương trình minh họa cách tính khoảng cách trắc địa từ dữ liệu dài vĩ độ.

    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    0
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    1
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    2
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    3

    pip install geopyp
    8
    pip install geopyp
    9

    Output:

    1318.13891581683

    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    4
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    5
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    6
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    7
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    8
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    9
    pip install geopyp
    0
    Great Circle Distance:
    It is the length of the shortest path between 2 points on a sphere. In this case, the earth is assumed to be a perfect sphere. Below program illustrates how to calculate great-circle distance from latitude-longitude data.

    pip install geopyp
    1
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    5
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    6
    pip install geopyp
    4
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    8
    pip install geopyp
    6
    pip install geopyp
    0

    & nbsp; Khoảng cách trắc địa: Đó là chiều dài của đường dẫn ngắn nhất giữa 2 điểm trên bất kỳ bề mặt nào. Trong trường hợp của chúng tôi, bề mặt là trái đất. Dưới đây chương trình minh họa cách tính khoảng cách trắc địa từ dữ liệu dài vĩ độ.

    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    0
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    1
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    2
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    3

    pip install geopyp
    8
    pip install geopy
    9

    Output:

    1317.7554645657162

    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    4
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    5
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    6
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    7
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    8
    from opencage.geocoder import OpenCageGeocode
    key = ‘YourAPIkey’ # get api key from: https://opencagedata.com
    geocoder = OpenCageGeocode(key)query = ‘Los Angeles CA’
    results = geocoder.geocode(query)
    lat = results[0][‘geometry’][‘lat’]lng = results[0][‘geometry’][‘lng’]print (lat, lng)
    9
    pip install geopyp
    0


    Giả sử có n số thành phố và các thành phố được kết nối với hai loại đường; Đường cao tốc và phím tắt. Bây giờ, có một bản đồ và chỉ có các đường cao tốc có mặt trên bản đồ và tất cả các phím tắt không có. Bộ phận Giao thông vận tải của các thành phố muốn khởi động một phương tiện giao thông kết nối các thành phố sử dụng đường cao tốc và các phím tắt. Chúng tôi biết có một lối tắt giữa hai thành phố khi không có đường cao tốc giữa chúng. Nhiệm vụ của chúng tôi ở đây là tìm khoảng cách tối thiểu về các phím tắt từ một thành phố bắt đầu đến tất cả các thành phố khác.

    Vì vậy, nếu đầu vào giống như

    Hướng dẫn distance between two cities python - khoảng cách giữa hai thành phố python

    và bắt đầu đỉnh (s) là 1, sau đó đầu ra sẽ là 3 1 2.

    Nếu chúng ta chỉ đi các phím tắt, đường dẫn giữa các thành phố 1 và 2 sẽ là 1-> 3-> 4-> 2 và chi phí sẽ là 3.

    Similarly,

    1 và 3: 1-> 3, chi phí 1.

    1 và 4: 1-> 3-> 4, chi phí 2.

    Để giải quyết vấn đề này, chúng tôi sẽ làm theo các bước này -

    • đồ thị: = một danh sách mới chứa n bộ
    • Đối với mỗi cặp (x, y) trong các cạnh, làm
      • x: = x - 1
      • y: = y - 1
      • Chèn y vào đồ thị [x]
      • Chèn x vào đồ thị [y]
    • TEMP_ARR: = một mảng mới có kích thước n chứa giá trị -1
    • b_set: = một bản đồ mới chứa khóa s - 1
    • F: = một bộ mới chứa đặt chênh lệch giữa các số 0 đến n và b_set
    • Chỉ mục: = 0
    • Trong khi kích thước của b_set> 0, hãy làm
      • Đối với mỗi phần tử a trong b_set, hãy làm
        • TEMP_ARR [A]: = Index
      • NXT: = một bản đồ mới chứa các giá trị của biểu đồ không phải là tập hợp của b_set
      • F: = Đặt sự khác biệt của F và NXT
      • b_set: = nxt
      • INDEX: = INDEX + 1
    • trả về các giá trị khác không của temp_arr

    Thí dụ

    Hãy cho chúng tôi xem việc thực hiện sau đây để hiểu rõ hơn -

    def solve(n, edges, s):
        graph = [set() for i in range(n)]
        for (x, y) in edges:
            x -= 1
            y -= 1
            graph[x].add(y)
            graph[y].add(x)
        temp_arr = [-1] * n
        b_set = {s - 1}
        f = set(range(n)).difference(b_set)
        index = 0
        while len(b_set) > 0:
            for a in b_set:
                temp_arr[a] = index
            nxt = {f for f in f if not b_set.issubset(graph[f])}
            f = f.difference(nxt)
            b_set = nxt
            index += 1
        return (' '.join(str(t) for t in temp_arr if t > 0))    
    
    print(solve(4, [(1, 2), (2, 3), (1, 4)], 1))
    

    Đầu vào

    4, [(1, 2), (2, 3), (1, 4)], 1

    Đầu ra

    3 1 2

    Hướng dẫn distance between two cities python - khoảng cách giữa hai thành phố python

    Cập nhật vào ngày 06 tháng 10 năm 2021 12:59:26

    • Câu hỏi và câu trả lời liên quan
    • Chương trình tìm ra khoảng cách giữa hai nút trong một cây nhị phân ở Python
    • Chương trình để tìm ra Subarray lớn nhất của một độ dài nhất định trong Python
    • Chương trình để tìm ra độ dài của sau khi tiếp tục bằng python dài nhất bằng cách sử dụng Python
    • Chương trình để tìm ra tỷ lệ chuyển đổi của hai loại tiền tệ trong Python
    • Chương trình tìm dân số tối đa có thể của tất cả các thành phố ở Python
    • Chương trình để tìm ra đường dẫn giữa hai đỉnh trong biểu đồ có hình phạt tối thiểu (Python)
    • Chương trình tìm ra sản phẩm DOT của hai vectơ thưa thớt trong Python
    • Chương trình tìm chi phí tối thiểu để gửi cùng một số người đến hai thành phố khác nhau ở Python
    • Chương trình để tìm ra số tọa độ tích phân trên một đường thẳng giữa hai điểm trong Python
    • Chương trình để tìm ra các tiểu mục tối thiểu trong Python
    • Chương trình Python để tìm sự khác biệt giữa hai dấu thời gian
    • Chương trình tìm ra khu vực thẳng đứng giữa hai điểm mà không có điểm nào nằm và là rộng nhất trong Python
    • Chương trình để tìm ra sự tương đồng giữa một chuỗi và hậu tố của nó trong Python
    • Chương trình tìm độ dài của chuỗi con dài nhất có hai yếu tố riêng biệt trong Python
    • Chương trình để tìm ra chênh lệch tiền tệ trong Python

    Khoảng cách địa chất là bao nhiêu?

    PIP Cài đặt Geopy. Khoảng cách trắc địa: Đó là chiều dài của đường dẫn ngắn nhất giữa 2 điểm trên bất kỳ bề mặt nào. Trong trường hợp của chúng tôi, bề mặt là trái đất. Dưới đây chương trình minh họa cách tính khoảng cách trắc địa từ dữ liệu dài vĩ độ.the length of the shortest path between 2 points on any surface. In our case, the surface is the earth. Below program illustrates how to calculate geodesic distance from latitude-longitude data.

    Công thức Haversine có chính xác không?

    Do đó, công thức Haversine có thể dẫn đến lỗi lên tới 0,5%.Để giải quyết vấn đề này, Thaddeus Vincenty đã phát triển một công thức rất phức tạp, chính xác lên tới 0,5mm, làm cho nó trở thành công thức trắc địa cuối cùng cho tất cả các mục đích khoa học nghiêm trọng.accurate up to 0.5mm, making it the ultimate geodesic formula for all serious scientific purposes.

    Ma trận khoảng cách python là gì?

    Một ma trận khoảng cách chứa khoảng cách được tính theo cặp giữa các vectơ của ma trận/ ma trận.scipy.Gói không gian cung cấp cho chúng tôi Phương thức Disce_Matrix () để tính toán ma trận khoảng cách.Nói chung, các ma trận ở dạng mảng 2 chiều và các vectơ của ma trận là các hàng ma trận (mảng 1-D).Cú pháp: Scipy.contains the distances computed pairwise between the vectors of matrix/ matrices. scipy. spatial package provides us distance_matrix() method to compute the distance matrix. Generally matrices are in the form of 2-D array and the vectors of the matrix are matrix rows ( 1-D array). Syntax: scipy.

    Công thức Haversine hoạt động như thế nào?

    Công thức Haversine xác định khoảng cách vòng tròn lớn giữa hai điểm trên một hình cầu với kinh độ và vĩ độ của chúng.Quan trọng trong điều hướng, nó là một trường hợp đặc biệt của một công thức tổng quát hơn trong lượng giác hình cầu, định luật của các haversines, liên quan đến các cạnh và góc của các hình tam giác hình cầu.determines the great-circle distance between two points on a sphere given their longitudes and latitudes. Important in navigation, it is a special case of a more general formula in spherical trigonometry, the law of haversines, that relates the sides and angles of spherical triangles.