![](/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.