-1
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.
4

1 回答 1

0

Geo.Inverse返回字典而不是单个值。检查文档

距离用key返回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

顺便说一句,你必须使用 geodesc 吗?您可以用接受 numy.ndarray 的矢量化函数替换距离函数,您只需传递 Lat 和 Long 列,然后传递它们的移位版本。这将大大提高性能。

看看这个关于矢量化函数的 PyCon 技术讨论,你很幸运;它是关于计算两点之间的距离!

于 2018-03-14T09:40:30.343 回答