1

I have a list of coordinates:

[(52.472665, -1.8977818),
(52.47455886, -1.90080653),
(52.4515712, -1.9327772),
(52.45028622, -1.93212766),..]

I am trying to write code that will allow me to calculate the distance between each one using the Haversine module:

(pseudo code)

for points in daycoords1:

p1 = day1coords[0]

p2 = day1coords[1]

dist_miles = haversine(p1, p2, miles=True)

distday1.append(dist_miles)

Is there a way to pull in coordinates one and two, measure the distance, two and three and mesaure that distance etc.

I have been trying to use itertools and zip() but I am yet to have any luck.

JTH
  • 445
  • 1
  • 5
  • 8

3 Answers3

2

I'm not exactly sure what function you want to use from the haversine module, but to compare two consecutive items in your list of coordinates you can use a list comprehension with a zip of slices of your coordinates:

coords = [(52.472665, -1.8977818),
(52.47455886, -1.90080653),
(52.4515712, -1.9327772),
(52.45028622, -1.93212766)]

distances = [haversine(p1, p2, miles=True) for p1, p2 in zip(coords[:-1], coords[1:])]

This will compare p1 to p2, p2 to p3, p3 to p4 and so on....

gtlambert
  • 11,711
  • 2
  • 30
  • 48
1

If I understand you correctly, all you need to do is iterate over
zip(daycoords1, daycoords1[1:]) and do your calculations. Demo:

>>> daycoords1 = [(1,2), (3,4), (5,6)]
>>> for x in zip(daycoords1, daycoords1[1:]):
...     print(x)
... 
((1, 2), (3, 4))
((3, 4), (5, 6))

If you have a lot of coordinates, consider itertools.izip for memory efficiency.

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • I like the fact that you don't need to slice the first `daycoords1` `zip` argument. Neat – gtlambert Feb 02 '16 at 10:32
  • @gtlambert yes `zip` will stop when the shortest iterable is exhausted. To go by the longest iterable, there's `itertools.izip_longest`. – timgeb Feb 02 '16 at 10:35
1

You can try range as below if you want to calculate distance between two locations(one after another as shown in the d and looking forward).

from haversine import haversine

d = [(52.472665, -1.8977818),
(52.47455886, -1.90080653),
(52.4515712, -1.9327772),
(52.45028622, -1.93212766),(53.45028622, -1.93212766)]

data = []

for i in range(len(d)-1):
    data.append(haversine(d[i], d[i+1], miles=True))

print data

Output-

[0.18255943050822185, 2.0818287457607068, 0.09290096620460978, 69.0933027640562]

EDIT- If you want to get distance between all the combinations( all possible non-duplicate pair of locations) in the d then -

from haversine import haversine
from itertools import combinations

d = [(52.472665, -1.8977818),
(52.47455886, -1.90080653),
(52.4515712, -1.9327772),
(52.45028622, -1.93212766),(52.45028622, -1.93212766)]

data = []

for pair in combinations(d,2):
    data.append(haversine(pair[0], pair[1], miles=True))

print data

Output-

[0.18255943050822185, 2.072320708393457, 2.1169388238266245, 2.1169388238266245, 2.0818287457607068, 2.1333352954019116, 2.1333352954019116, 0.09290096620460978, 0.09290096620460978, 0.0]
Learner
  • 5,192
  • 1
  • 24
  • 36
  • Does this not fail if their is an odd number of coordinates? Also this only allows p1 to be compared to p2, p3 to p4 etc... – JTH Feb 02 '16 at 10:56
  • Rather than all combinations I'm trying to get it to get the distance between p1 and p2, then p2 and p3, then p3 and p4, etc. – JTH Feb 02 '16 at 11:15
  • OK see modified answer.. – Learner Feb 02 '16 at 11:38