Coverage for apps/campaigns/utils.py: 25%
16 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-08 12:47 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-08 12:47 -0600
1import math
3from django.contrib.gis.measure import Distance
6def deg2rad(deg: float):
7 return deg * (math.pi / 180)
10def distance_between_lat_long(lat1: float, long1: float, lat2: float, long2: float):
11 lat1 = float(lat1)
12 lat2 = float(lat2)
13 long1 = float(long1)
14 long2 = float(long2)
15 R = 6371
16 dLat = deg2rad(lat2 - lat1)
17 dLon = deg2rad(long2 - long1)
18 a = math.sin(dLat / 2) * math.sin(dLat / 2) + math.cos(deg2rad(lat1)) * math.cos(deg2rad(lat2)) * math.sin(
19 dLon / 2
20 ) * math.sin(dLon / 2)
21 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
22 d = Distance(km=R * c)
23 return d.yd