def compute_travel_times(flats_list, constraint, config): """ Compute the travel time between each flat and the points listed in the constraints. :param flats_list: A list of flats dict. :param constraint: The constraint that the ``flats_list`` should satisfy. :param config: A config dict. :return: An updated list of flats dict with computed travel times. .. note :: Requires a Navitia or CityMapper API key in the config. """ for flat in flats_list: if not flat["flatisfy"].get("matched_stations", []): # Skip any flat without matched stations LOGGER.info( "Skipping travel time computation for flat %s. No matched stations.", flat["id"], ) continue if "time_to" not in flat["flatisfy"]: # Ensure time_to key is initialized flat["flatisfy"]["time_to"] = {} # For each place, loop over the stations close to the flat, and find # the minimum travel time. for place_name, place in constraint["time_to"].items(): mode = place.get("mode", "PUBLIC_TRANSPORT") time_to_place_dict = None for station in flat["flatisfy"]["matched_stations"]: # Time from station is a dict with time and route time_from_station_dict = tools.get_travel_time_between( station["gps"], place["gps"], TimeToModes[mode], config ) if time_from_station_dict and ( time_from_station_dict["time"] < time_to_place_dict or time_to_place_dict is None ): # If starting from this station makes the route to the # specified place shorter, update time_to_place_dict = time_from_station_dict if time_to_place_dict: LOGGER.info( "Travel time between %s and flat %s by %s is %ds.", place_name, flat["id"], mode, time_to_place_dict["time"], ) flat["flatisfy"]["time_to"][place_name] = time_to_place_dict return flats_list
def compute_travel_times(flats_list, constraint, config): """ Compute the travel time between each flat and the points listed in the constraints. :param flats_list: A list of flats dict. :param constraint: The constraint that the ``flats_list`` should satisfy. :param config: A config dict. :return: An updated list of flats dict with computed travel times. .. note :: Requires a Navitia or CityMapper API key in the config. """ for flat in flats_list: if not flat["flatisfy"].get("matched_stations", []): # Skip any flat without matched stations LOGGER.info( "Skipping travel time computation for flat %s. No matched " "stations.", flat["id"]) continue if "time_to" not in flat["flatisfy"]: # Ensure time_to key is initialized flat["flatisfy"]["time_to"] = {} # For each place, loop over the stations close to the flat, and find # the minimum travel time. for place_name, place in constraint["time_to"].items(): time_to_place = None for station in flat["flatisfy"]["matched_stations"]: time_from_station = tools.get_travel_time_between( station["gps"], place["gps"], config) if time_from_station and (time_from_station < time_to_place or time_to_place is None): time_to_place = time_from_station if time_to_place: LOGGER.info("Travel time between %s and flat %s is %ds.", place_name, flat["id"], time_to_place["time"]) flat["flatisfy"]["time_to"][place_name] = time_to_place return flats_list