Skip to content

Instantly share code, notes, and snippets.

@mdeprezzo
mdeprezzo / haversine_coverage.py
Last active December 11, 2021 20:44
Calculate the percentage of the zone covered by enabled shoppers (`coverage`). One shopper covers a zone if the distance among the coordinates is less than 10 km.
import math
def haversine(lat1, lng1, lat2, lng2):
p = math.pi/180
a = 0.5 - math.cos((lat2-lat1)*p)/2 + math.cos(lat1*p) * math.cos(lat2*p) * (1-math.cos((lng2-lng1)*p))/2
return 12742 * math.asin(math.sqrt(a)) #2*R*asin...
def findIndex(my_list, prop, value):
return next((i for i, item in enumerate(my_list) if item[prop] == value), -1)