我拥有包含多个来源和投放地点以及它们各自的纬度和经度值的数据,我需要计算每个来源和目的地之间的距离(以公里/英里为单位),以下是来源和目的地的坐标>
origin_coords = [
'-33.8309, 151.0875',
'-33.77733, 151.119854']
destination_coords = [
'-25.5789375305176, 134.359710693359',
'-12.46029472, 130.8423157',
'-33.87905884, 151.2432098']
有人可以帮助我在python中执行
答案 0 :(得分:0)
您可以使用geopy
软件包:
from geopy.distance import geodesic
for org, dst in zip(origin_coords, destination_coords):
print(geodesic(org, dst).kilometers)
(以Kms为单位):
1855.6690160383962
3131.115522557264
编辑:
如果要查找从每个起点到目的地的距离,请使用以下方法:
import itertools
coord_orig_2_dst = list(itertools.product(origin_coords, destination_coords)) # <-- will give 6 pairs
for idx, (org, dst) in enumerate(coord_orig_2_dst):
if (idx+1)%(len(origin_coords)):
print(f"\nposition from origin{(idx+1)//(len(origin_coords))} to destination")
print(geodesic(org, dst).kilometers)
position from origin0 to destination
1855.6690160383962
3133.107252977121
position from origin1 to destination
15.3678706642723
1855.8745525856307
position from origin2 to destination
3131.115522557264
16.053573246669945
答案 1 :(得分:0)
您正在寻找类似的东西
from sklearn.metrics import pairwise_distances
import numpy as np
from math import sin, cos, sqrt, atan2, radians
origin_coords = ['-33.8309, 151.0875', '-33.77733, 151.119854']
destination_coords = ['-25.5789375305176, 134.359710693359', '-12.46029472, 130.8423157', '-33.87905884, 151.2432098']
origin_coords = [[float(v.split(",")[0]), float(v.split(",")[1])] for v in origin_coords]
destination_coords = [[float(v.split(",")[0]), float(v.split(",")[1])] for v in destination_coords]
origin_coords = np.array([[radians(v[0]), radians(v[1])] for v in origin_coords])
destination_coords = np.array([[radians(v[0]), radians(v[1])] for v in destination_coords])
# distance in km.
def geodesic_dist(x, y):
dlon = y[1] - x[1]
dlat = y[0] - x[0]
a = sin(dlat / 2)**2 + cos(x[0]) * cos(y[0]) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
return 6373.0 * c
print(pairwise_distances(origin_coords, destination_coords, metric=geodesic_dist))
产生
[[1854.93894877 3138.98807828 15.34816524]
[1855.11802065 3136.96316526 16.06085592]]
有关详细信息,请参见Getting distance between two points based on latitude/longitude。