MCPcopy Create free account
hub / github.com/comaps/comaps / distance

Function distance

tools/python/openlr/quality.py:14–29  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

12LatLon = namedtuple('LatLon', 'lat, lon')
13
14def 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
31def lcs(l1, l2, eq=operator.eq):
32 """Finds the longest common subsequence of l1 and l2.

Callers 15

common_partFunction · 0.85
AddMethod · 0.85
CopyWithoutOffsetsFunction · 0.85
SkipMethod · 0.85
ReadMethod · 0.85
CalcCrossMwmTransitionsFunction · 0.85
GetAreaIndexFunction · 0.85
SerializeRestrictionsFunction · 0.85
TransformMethod · 0.85
EncodeAndWriteImplMethod · 0.85

Calls 1

mapFunction · 0.85

Tested by 5

TestPolygonPreconditionsFunction · 0.68
UNIT_TESTFunction · 0.68
UNIT_CLASS_TESTFunction · 0.68
compare_sequenceFunction · 0.68