def test_haversine(): lat_lng_dists = [(0,0,0,0,0), (76,1,76,1,0), (76,1,76,361,0), (0,0,0,90,10007.54339801), (0,0,0,180,20015.08679602), (0,-180,0,180,0), (-90,0,90,0,20015.08679602), (-90,0,90,180,20015.08679602), ] for lat1,lng1,lat2,lng2,dist in lat_lng_dists: assert_allclose(haversine(lat1,lng1,lat2,lng2),dist,rtol=RTOL,atol=ATOL)
def _find_nearby_station(self, station): if self.stations is None: self.stations = self._load_stations() if self.station_to_lat_lng is None: self.station_to_lat_lng = self._load_station_locations() lat, lng = self.station_to_lat_lng[station] index_list = list(self.station_to_lat_lng.items()) dists = [haversine(lat, lng, stat_lat, stat_lng) for _, (stat_lat, stat_lng) in index_list] for dist, (nearby_station, _) in zip(dists, index_list): if nearby_station in self.stations: warnings.warn("Using station {} instead".format(nearby_station)) return nearby_station return None
def _find_nearby_station(self, station): if self.stations is None: self.stations = self._load_stations() if self.station_to_lat_lng is None: self.station_to_lat_lng = self._load_station_locations() try: lat, lng = self.station_to_lat_lng[station] except KeyError: warnings.warn( "Could not locate station {}; " "nearby station look-up failed".format(station) ) return None else: index_list = list(self.station_to_lat_lng.items()) dists = [haversine(lat, lng, stat_lat, stat_lng) for _, (stat_lat, stat_lng) in index_list] for dist, (nearby_station, _) in zip(dists, index_list): if nearby_station in self.stations: warnings.warn("Using station {} instead".format(nearby_station)) return nearby_station return None
def test_haversine(lat_lng_dist): lat1, lng1, lat2, lng2, dist = lat_lng_dist haversine_dist = haversine(lat1, lng1, lat2, lng2) assert_allclose(haversine_dist, dist, rtol=RTOL, atol=ATOL)