runtime
211 / 500
question 211 Modules & Packages · easy

The nearest store, using the geo module

The store finder shows a customer their nearest Runtime Market pickup point in Portland. Distances on a map aren't straight-line differences of latitude and longitude, so a provided module, geo.py, does the maths. Use it. Provided files sit next to your program (they aren't editor tabs); print(Path("geo.py").read_text(encoding="utf-8")) shows one's source.

Functionnearest_store(lat, lon, stores)
storesa list of dictionaries with name, lat and lon
Returnsthe name of the store with the smallest geo.distance_km; the first one on a tie; None for no stores
geo.pydistance_km(lat1, lon1, lat2, lon2) → the great-circle distance between two points, in kilometres
Examples
callnearest_store(45.5231, -122.6765, [{'name': 'Pearl District', 'lat': 45.529, 'lon': -122.684}, {'name': 'Hawthorne', 'lat': 45.5122, 'lon': -122.629}, {'name': 'St Johns', 'lat': 45.59, 'lon': -122.754}])returns'Pearl District'
callnearest_store(45.5, -122.62, [{'name': 'Pearl District', 'lat': 45.529, 'lon': -122.684}, {'name': 'Hawthorne', 'lat': 45.5122, 'lon': -122.629}, {'name': 'St Johns', 'lat': 45.59, 'lon': -122.754}])returns'Hawthorne'
callnearest_store(45.6, -122.76, [{'name': 'Pearl District', 'lat': 45.529, 'lon': -122.684}, {'name': 'Hawthorne', 'lat': 45.5122, 'lon': -122.629}, {'name': 'St Johns', 'lat': 45.59, 'lon': -122.754}])returns'St Johns'

Loading your progress…

The nearest store, using the geo module — Python practice · Runtime