Пример #1
0
def fetch_region_subregion_tier(catalogue_name,
                                file_format=".pickle",
                                update=False):
    """
    :param catalogue_name: [str] e.g. "GeoFabrik-region-subregion-tier"
    :param file_format: [str] ".pickle"(default), or ".json"
    :param update: [bool] whether to update (i.e. re-collect) all subregion tables for each continent
    :return: [dict, or list] or null
    """
    available_catalogue = ("GeoFabrik-region-subregion-tier",
                           "GeoFabrik-non-subregion-list")
    assert catalogue_name in available_catalogue, \
        "'catalogue_name' must be one of the following: \n  \"{}\".".format("\",\n  \"".join(available_catalogue))

    available_fmt = (".pickle", ".json")
    assert file_format in available_fmt, \
        "'file_format' must be one of the following: \n  \"{}\".".format("\",\n  \"".join(available_fmt))

    path_to_file = cd_dat(catalogue_name + file_format)
    if not os.path.isfile(path_to_file) or update:
        collect_region_subregion_tier(confirmation_required=True)
    try:
        index_file = load_pickle(
            path_to_file) if file_format == ".pickle" else load_json(
                path_to_file)
        return index_file
    except Exception as e:
        print(e)
Пример #2
0
def fetch_subregion_info_catalogue(catalogue_name,
                                   file_format=".pickle",
                                   update=False):
    """
    :param catalogue_name: [str] e.g. "GeoFabrik-subregion-name-list"
    :param file_format: [str] ".pickle"(default), or ".json"
    :param update: [bool] whether to update (re-collect) the catalogues of subregion information
    :return: [list, dict, pandas.DataFrame] or null
    """
    available_catalogue = ("GeoFabrik-subregion-name-list",
                           "GeoFabrik-subregion-name-url-dictionary",
                           "GeoFabrik-subregion-downloads-catalogue")
    assert catalogue_name in available_catalogue, \
        "'catalogue_name' must be one of the following: \n  \"{}\".".format("\",\n  \"".join(available_catalogue))

    available_fmt = (".pickle", ".json")
    assert file_format in available_fmt, \
        "'file_format' must be one of the following: \n  \"{}\".".format("\",\n  \"".join(available_fmt))

    path_to_catalogue = cd_dat(catalogue_name + file_format)
    if not os.path.isfile(
            path_to_catalogue) or update:  # all(paths_to_files_exist) and
        collect_subregion_info_catalogue(confirmation_required=True)
    try:
        index_file = load_pickle(
            path_to_catalogue) if file_format == ".pickle" else load_json(
                path_to_catalogue)
        return index_file
    except Exception as e:
        print(e)
Пример #3
0
def update_nr_route_names(data_set, route_col_name='Route'):
    """
    Update names of NR Routes.

    :param data_set: a given data frame that contains 'Route' column
    :type data_set: pandas.DataFrame
    :param route_col_name: name of the column for 'Route', defaults to ``'Route'``
    :return: updated data frame
    :rtype: pandas.DataFrame
    """

    assert isinstance(data_set, pd.DataFrame)
    assert route_col_name in data_set.columns
    route_names_changes = load_json(cdd_network("Routes", "name-changes.json"))
    new_route_col_name = route_col_name + 'Alias'
    data_set.rename(columns={route_col_name: new_route_col_name}, inplace=True)
    data_set[route_col_name] = data_set[new_route_col_name].replace(route_names_changes)
Пример #4
0
def make_filename(name, route_name=None, weather_category=None, *suffixes, sep="-", save_as=".pickle"):
    """
    Make a filename as appropriate.

    :param name: base name, defaults to ``None``
    :type name: str or None
    :param route_name: name of a Route, defaults to ``None``
    :type route_name: str or list or None
    :param weather_category: weather category, defaults to ``None``
    :type weather_category: str or list or None
    :param suffixes: extra suffixes to the filename
    :type suffixes: int or str or None
    :param sep: a separator in the filename, defaults to ``"-"``
    :type sep: str or None
    :param save_as: file extension, defaults to ``".pickle"``
    :type save_as: str
    :return: a filename
    :rtype: str

    **Test**::

        >>> from utils import make_filename

        name = "test"  # None
        sep = "-"
        save_as = ".pickle"

        route_name = None
        weather_category = None
        make_filename(base_name, route_name, weather_category)
        # 'test.pickle'

        route_name = None
        weather_category = 'Heat'
        make_filename(None, route_name, weather_category, "test1")
        # 'test1.pickle'

        make_filename(name, route_name, weather_category, "test1", "test2")
        # 'test-test1-test2.pickle'

        make_filename(name, 'Anglia', weather_category, "test2")
        # 'test-Anglia-test2.pickle'

        make_filename(name, 'North and East', 'Heat', "test1", "test2")
        # 'test-North_and_East-Heat-test1-test2.pickle'
    """

    base_name = "" if name is None else name

    if route_name is None:
        route_name_ = ""
    else:
        rts = list(set(load_json(cdd_network("routes", "name-changes.json")).values()))
        route_name_ = "_".join(
            [find_similar_str(x, rts).replace(" ", "") for x in
             ([route_name] if isinstance(route_name, str) else list(route_name))])
        if base_name != "":
            route_name_ = sep + route_name_

    if weather_category is None:
        weather_category_ = ""
    else:
        wcs = load_json(cdd_weather("weather-categories.json"))['WeatherCategory']
        weather_category_ = "_".join(
            [find_similar_str(x, wcs).replace(" ", "") for x in
             ([weather_category] if isinstance(weather_category, str) else list(weather_category))])
        if base_name != "":
            weather_category_ = sep + weather_category_

    if base_name + route_name_ + weather_category_ == "":
        base_name = "data"

    if suffixes:
        extra_suffixes = [suffixes] if isinstance(suffixes, str) else suffixes
        suffix_ = ["{}".format(s) for s in extra_suffixes if s]
        try:
            suffix = sep + sep.join(suffix_) if len(suffix_) > 1 else sep + suffix_[0]
        except IndexError:
            suffix = ""
        filename = base_name + route_name_ + weather_category_ + suffix + save_as

    else:
        filename = base_name + route_name_ + weather_category_ + save_as

    return filename