![](/img/trans.png)
[英]Efficient way to calculate distance matrix given latitude and longitude data in Python
[英]Efficient way to convert Latitude/Longitude to XY
我有一个工作script
,可将Latitude
和Longitude
坐标转换为笛卡尔坐标。 但是,我必须在每个时间点( row by row
)针对特定点执行此操作。
我想在较大的df
上执行类似的操作。 我不确定遍历每row
的loop
是否是最有效的方法? 下面是转换单个XY点的script
。
import math
import numpy as np
import pandas as pd
point1 = [-37.83028766, 144.9539561]
r = 6371000 #radians of earth meters
phi_0 = point1[1]
cos_phi_0 = math.cos(np.radians(phi_0))
def to_xy(point, r, cos_phi_0):
lam = point[0]
phi = point[1]
return (r * np.radians(lam) * cos_phi_0, r * np.radians(phi))
point1_xy = to_xy(point1, r, cos_phi_0)
如果我想在单点之间进行转换,则效果很好。 问题是如果我有一个大的数据框或坐标列表( >100,000 rows
)。 iterates
每一row
的loop
效率低下。 有没有更好的方法来执行相同的功能?
以下是df
稍大的示例。
d = ({
'Time' : [0,1,2,3,4,5,6,7,8],
'Lat' : [37.8300,37.8200,37.8200,37.8100,37.8000,37.8000,37.7900,37.7900,37.7800],
'Long' : [144.8500,144.8400,144.8600,144.8700,144.8800,144.8900,144.8800,144.8700,144.8500],
})
df = pd.DataFrame(data = d)
如果我是你,我会做的。 (顺便说一句:元组铸造部分可以优化。
import numpy as np
import pandas as pd
point1 = [-37.83028766, 144.9539561]
def to_xy(point):
r = 6371000 #radians of earth meters
lam,phi = point
cos_phi_0 = np.cos(np.radians(phi))
return (r * np.radians(lam) * cos_phi_0,
r * np.radians(phi))
point1_xy = to_xy(point1)
print(point1_xy)
d = ({
'Lat' : [37.8300,37.8200,37.8200,37.8100,37.8000,37.8000,37.7900,37.7900,37.7800],
'Long' : [144.8500,144.8400,144.8600,144.8700,144.8800,144.8900,144.8800,144.8700,144.8500],
})
df = pd.DataFrame(d)
df['to_xy'] = df.apply(lambda x:
tuple(x.values),
axis=1).map(to_xy)
print(df)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.