user2927983 - 4 years ago 383
Python Question

Pairwise haversine distance calculation

I have two arrays with lat and long. I want to calculate distance between every pair of lat and long with every other pair of lat and long in the array.
Here are my two arrays.

``````lat_array

array([ 0.33356456,  0.33355585,  0.33355585,  0.33401788,  0.33370132,
0.33370132,  0.33370132,  0.33371075,  0.33371075,  0.33370132,
0.33370132,  0.33370132,  0.33356488,  0.33356488,  0.33370132,
0.33370132,  0.33370132,  0.33401788,  0.33362632,  0.33362632,
0.33364007,  0.33370132,  0.33401788,  0.33401788,  0.33358399,
0.33358399,  0.33358399,  0.33370132,  0.33370132,  0.33362632,
0.33370132,  0.33370132,  0.33370132,  0.33370132,  0.33370132,
0.33356488,  0.33356456,  0.33391071,  0.33370132,  0.33356488,
0.33356488,  0.33356456,  0.33356456,  0.33356456,  0.33362632,
0.33364804,  0.3336314 ,  0.33370132,  0.33370132,  0.33370132,
0.33364034,  0.33359921,  0.33370132,  0.33360397,  0.33348863,
0.33370132])
long_array

array([ 1.27253229,  1.27249141,  1.27249141,  1.27259085,  1.2724337 ,
1.2724337 ,  1.2724337 ,  1.27246931,  1.27246931,  1.2724337 ,
1.2724337 ,  1.2724337 ,  1.27254305,  1.27254305,  1.2724337 ,
1.2724337 ,  1.2724337 ,  1.27259085,  1.27250461,  1.27250461,
1.27251211,  1.2724337 ,  1.27259085,  1.27259085,  1.27252134,
1.27252134,  1.27252134,  1.2724337 ,  1.2724337 ,  1.27250461,
1.2724337 ,  1.2724337 ,  1.2724337 ,  1.2724337 ,  1.2724337 ,
1.27254305,  1.27253229,  1.27266808,  1.2724337 ,  1.27254305,
1.27254305,  1.27253229,  1.27253229,  1.27253229,  1.27250461,
1.27250534,  1.27250184,  1.2724337 ,  1.2724337 ,  1.2724337 ,
1.27251339,  1.27223739,  1.2724337 ,  1.2722575 ,  1.27237575,
1.2724337 ])
``````

After conversion into radians. Now I want distance between first pair of lat and long with remaining pairs of lat and long and so on. And want to print the pairs and the corresponding distance.

This is what I am doing in python.

``````distance = []
R = 6371.0

for i in range(len(lat_array)):
for j in (i+1,len(lat_array)):
dlon = long_array[j]-long_array[i]
dlat = lat_array[j]-lat_array[i]
a = sin(dlat / 2)**2 + cos(lat_array[i]) * cos(lat_array[j]) *
sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))

distance.append(R * c)
``````

It gives me an error
`IndexError: index 56 is out of bounds for axis 0 with size 56`

Where I am doing it wrong? And how to make calculation faster if the array is big? Please help.

You have a typo in the code. Change

``````for j in (i+1,len(lat_array)):
``````

to

``````for j in range(i+1,len(lat_array)):
``````

Otherwise you're iterating a tuple consisting of two elements `i+1` and `len(lat_array)`. The second causes the error.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download