输出在 NAN 中 错误的距离无法计算
output is in NAN Wrong distance not be calculated
Data input:
cell_id Lat_Long Lat Long
15327 28.46852_76.99512 28.46852 76.99512
52695 28.46852_76.99512 28.46852 76.99512
52692 28.46852_76.99512 28.46852 76.99512
29907 28.46852_76.99512 28.46852 76.99512
29905 28.46852_76.99512 28.46852 76.99512
应用测地线并找出距离 b/w cell_id 但它会创建
距离列,但所有值都是 NAN 。
Code:
Geo = Geodesic.WGS84
n=len(df3)-1
for i in range(0, n):
#df3=df3['Lat'].astype(float)
Lat1=float(df3['Lat'].iloc[i])
Long1=float(df3['Long'].iloc[i])
Lat2=float(df3['Lat'].iloc[i+1])
Long2=float(df3['Long'].iloc[i+1])
df3['dis']=pd.Series(Geo.Inverse( Lat1, Long1, Lat2, Long2))
if(i==n):
df3['dis']=pd.Series()
print df3
输出:
cellid Lat_Long Lat Long dis
15327 28.46852_76.99512 28.46852 76.99512 NaN
52695 28.46852_76.99512 28.46852 76.99512 NaN
52692 28.46852_76.99512 28.46852 76.99512 NaN
29907 28.46852_76.99512 28.46852 76.99512 NaN
29905 28.46852_76.99512 28.46852 76.99512 NaN
39502 28.4572_77.0008 28.4572 77.0008 NaN
what is the problem in this code.
Geo.Inverse
returns 字典不是单个值。检查 documentation.
距离用键返回s12 – the distance from the first point to the second in meters
n = len(df) - 1
for i in range(0, n):
Lat1 = float(df['Lat'].iloc[i])
Long1 = float(df['Long'].iloc[i])
Lat2 = float(df['Lat'].iloc[i + 1])
Long2 = float(df['Long'].iloc[i + 1])
df['dis'] = Geo.Inverse(Lat1, Long1, Lat2, Long2)["s12"]
if (i == n):
df['dis'] = None
这将导致:
cell_id Lat_Long Lat Long dis
0 15327 28.46852_76.99512 28.46852 76.99512 0.0
1 52695 28.46852_76.99512 28.46852 76.99512 0.0
2 52692 28.46852_76.99512 28.46852 76.99512 0.0
3 29907 28.46852_76.99512 28.46852 76.99512 0.0
4 29905 28.46852_76.99512 28.46852 76.99512 0.0
顺便说一句,你必须使用测地线吗?您可以将距离函数替换为接受 numy.ndarray 的向量化函数,您只需传递 Lat 和 Long 列,然后传递它们的移位版本。这将大大提高性能。
查看this PyCon 技术讨论向量化函数,你真幸运;这是关于计算两点之间的距离!
Data input:
cell_id Lat_Long Lat Long
15327 28.46852_76.99512 28.46852 76.99512
52695 28.46852_76.99512 28.46852 76.99512
52692 28.46852_76.99512 28.46852 76.99512
29907 28.46852_76.99512 28.46852 76.99512
29905 28.46852_76.99512 28.46852 76.99512
应用测地线并找出距离 b/w cell_id 但它会创建 距离列,但所有值都是 NAN 。
Code:
Geo = Geodesic.WGS84
n=len(df3)-1
for i in range(0, n):
#df3=df3['Lat'].astype(float)
Lat1=float(df3['Lat'].iloc[i])
Long1=float(df3['Long'].iloc[i])
Lat2=float(df3['Lat'].iloc[i+1])
Long2=float(df3['Long'].iloc[i+1])
df3['dis']=pd.Series(Geo.Inverse( Lat1, Long1, Lat2, Long2))
if(i==n):
df3['dis']=pd.Series()
print df3
输出:
cellid Lat_Long Lat Long dis
15327 28.46852_76.99512 28.46852 76.99512 NaN
52695 28.46852_76.99512 28.46852 76.99512 NaN
52692 28.46852_76.99512 28.46852 76.99512 NaN
29907 28.46852_76.99512 28.46852 76.99512 NaN
29905 28.46852_76.99512 28.46852 76.99512 NaN
39502 28.4572_77.0008 28.4572 77.0008 NaN
what is the problem in this code.
Geo.Inverse
returns 字典不是单个值。检查 documentation.
距离用键返回s12 – the distance from the first point to the second in meters
n = len(df) - 1
for i in range(0, n):
Lat1 = float(df['Lat'].iloc[i])
Long1 = float(df['Long'].iloc[i])
Lat2 = float(df['Lat'].iloc[i + 1])
Long2 = float(df['Long'].iloc[i + 1])
df['dis'] = Geo.Inverse(Lat1, Long1, Lat2, Long2)["s12"]
if (i == n):
df['dis'] = None
这将导致:
cell_id Lat_Long Lat Long dis
0 15327 28.46852_76.99512 28.46852 76.99512 0.0
1 52695 28.46852_76.99512 28.46852 76.99512 0.0
2 52692 28.46852_76.99512 28.46852 76.99512 0.0
3 29907 28.46852_76.99512 28.46852 76.99512 0.0
4 29905 28.46852_76.99512 28.46852 76.99512 0.0
顺便说一句,你必须使用测地线吗?您可以将距离函数替换为接受 numy.ndarray 的向量化函数,您只需传递 Lat 和 Long 列,然后传递它们的移位版本。这将大大提高性能。
查看this PyCon 技术讨论向量化函数,你真幸运;这是关于计算两点之间的距离!