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
Applying Geodesic and find out the distance b/w cell_id but it will create distance column but all values is 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
output:
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 a dictionary not a single value. Check the documentation.
The distance is returned with the 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
This will result in:
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
By the way do you have to use geodesc? you can replace the distance function with a vectorized one that accepts numy.ndarray, and you would just pass your Lat and Long columns then a shifted version of them. This will greatly enhance performance.
Check this PyCon tech talk about vectorized functions, lucky you; it is about calculating distance between two points!