Implements https://en.wikipedia.org/wiki/Haversine_formula. >>> int(distance(LatLon(55.747043, 37.655554), LatLon(55.754892, 37.657013))) 875 >>> int(distance(LatLon(60.013918, 29.718361), LatLon(59.951572, 30.205536))) 27910
(x, y)
| 12 | LatLon = namedtuple('LatLon', 'lat, lon') |
| 13 | |
| 14 | def distance(x, y): |
| 15 | """Implements https://en.wikipedia.org/wiki/Haversine_formula. |
| 16 | |
| 17 | >>> int(distance(LatLon(55.747043, 37.655554), LatLon(55.754892, 37.657013))) |
| 18 | 875 |
| 19 | >>> int(distance(LatLon(60.013918, 29.718361), LatLon(59.951572, 30.205536))) |
| 20 | 27910 |
| 21 | """ |
| 22 | |
| 23 | φ1, φ2 = map(radians, [x.lat, y.lat]) |
| 24 | λ1, λ2 = map(radians, [x.lon, y.lon]) |
| 25 | Δφ = φ2 - φ1 |
| 26 | Δλ = λ2 - λ1 |
| 27 | a = sin(Δφ/2)**2 + cos(φ1) * cos(φ2) * sin(Δλ/2)**2 |
| 28 | R = 6356863 # Earth radius in meters. |
| 29 | return 2 * R * atan2(sqrt(a), sqrt(1 - a)) |
| 30 | |
| 31 | def lcs(l1, l2, eq=operator.eq): |
| 32 | """Finds the longest common subsequence of l1 and l2. |