示例#1
0
def download_bbbike_subregion_osm_all_files(subregion_name, download_dir=None, download_confirmation_required=True):
    """
    :param subregion_name: [str]
    :param download_dir: [str or None]
    :param download_confirmation_required: [bool]
    """
    subregion_name_ = regulate_bbbike_input_subregion_name(subregion_name)
    bbbike_download_dictionary = fetch_bbbike_download_catalogue("BBBike-download-catalogue")
    sub_download_catalogue = bbbike_download_dictionary[subregion_name_]

    data_dir = cd_dat_bbbike(subregion_name_) if not download_dir else regulate_input_data_dir(download_dir)

    if confirmed("Confirm to download all available BBBike data for \"{}\"?".format(subregion_name_),
                 confirmation_required=download_confirmation_required):
        print("\nStart to download all available OSM data for \"{}\" ... \n".format(subregion_name_))
        for download_url, osm_filename in zip(sub_download_catalogue.URL, sub_download_catalogue.Filename):
            print("\n\n\"{}\" (below): ".format(osm_filename))
            try:
                path_to_file = os.path.join(data_dir, subregion_name_, osm_filename)
                download(download_url, path_to_file)
                # if os.path.getsize(path_to_file) / (1024 ** 2) <= 5:
                #     time.sleep(5)
            except Exception as e:
                print("\nFailed to download \"{}\". {}.".format(osm_filename, e))
        print("\nCheck out the downloaded OSM data for \"{}\" at \"{}\".".format(
            subregion_name_, os.path.join(data_dir, subregion_name_)))
    else:
        print("The downloading process was not activated.")
示例#2
0
def download_bbbike_subregion_osm(*subregion_name, osm_file_format, download_dir=None, update=False,
                                  download_confirmation_required=True):
    """
    :param subregion_name: [str]
    :param osm_file_format: [str]
    :param download_dir: [str or None]
    :param update: [bool]
    :param download_confirmation_required: [bool]
    :return:
    """
    for sub_reg_name in subregion_name:

        subregion_name_, osm_filename, download_url, path_to_file = validate_bbbike_download_info(
            sub_reg_name, osm_file_format, download_dir)

        if os.path.isfile(path_to_file) and not update:
            print("\"{}\" is already available for \"{}\" at: \n\"{}\".\n".format(
                osm_filename, subregion_name_, path_to_file))
        else:
            if confirmed("\nTo download {} data for {}".format(osm_file_format, subregion_name_),
                         confirmation_required=download_confirmation_required):
                try:
                    download(download_url, path_to_file)
                    print("\n\"{}\" has been downloaded for \"{}\", which is now available at \n\"{}\".\n".format(
                        osm_filename, subregion_name_, path_to_file))

                    if os.path.getsize(path_to_file) / (1024 ** 2) <= 5:
                        time.sleep(5)

                except Exception as e:
                    print("\nFailed to download \"{}\". {}.".format(osm_filename, e))
            else:
                print("The downloading process was not activated.")
示例#3
0
def download_subregion_osm_file(*subregion_name,
                                osm_file_format,
                                download_dir=None,
                                update=False,
                                download_confirmation_required=True,
                                verbose=True):
    """
    :param subregion_name: [str] case-insensitive, e.g. 'greater London', 'london'
    :param osm_file_format: [str] ".osm.pbf", ".shp.zip", or ".osm.bz2"
    :param download_dir: [str] directory to save the downloaded file(s), or None (using default directory)
    :param update: [bool] whether to update (i.e. re-download) data
    :param download_confirmation_required: [bool] whether to confirm before downloading
    :param verbose: [bool]
    """
    for sub_reg_name in subregion_name:

        # Get download URL
        subregion_name_, download_url = get_subregion_download_url(
            sub_reg_name, osm_file_format, update=False)

        if not download_dir:
            # Download the requested OSM file to default directory
            osm_filename, path_to_file = get_default_path_to_osm_file(
                subregion_name_, osm_file_format, mkdir=True)
        else:
            regulated_dir = regulate_input_data_dir(download_dir)
            osm_filename = get_default_osm_filename(
                subregion_name_, osm_file_format=osm_file_format)
            path_to_file = os.path.join(regulated_dir, osm_filename)

        if os.path.isfile(path_to_file) and not update:
            if verbose:
                print(
                    "\n\"{}\" is already available for \"{}\" at: \n\"{}\".\n".
                    format(osm_filename, subregion_name_, path_to_file))
            else:
                pass
        else:
            if confirmed("\nTo download {} data for {}".format(
                    osm_file_format, subregion_name_),
                         confirmation_required=download_confirmation_required):

                op = "Updating" if os.path.isfile(
                    path_to_file) else "Downloading"
                try:
                    download(download_url, path_to_file)
                    print("\n{} \"{}\" for \"{}\" ... Done.".format(
                        op, osm_filename, subregion_name_))
                    print("Check out: \"{}\".".format(path_to_file))
                except Exception as e:
                    print("\nFailed to download \"{}\". {}.".format(
                        osm_filename, e))
            else:
                print("The downloading process was not activated.")
示例#4
0
def download_subregion_osm_file(*subregion_name,
                                osm_file_format,
                                download_dir=None,
                                update=False,
                                download_confirmation_required=True,
                                deep_retry=False,
                                verbose=False):
    """
    :param subregion_name: [str] case-insensitive, e.g. 'greater London', 'london'
    :param osm_file_format: [str] ".osm.pbf", ".shp.zip", or ".osm.bz2"
    :param download_dir: [str; None (default)] directory to save the downloaded file(s); None (using default directory)
    :param update: [bool] (default: False) whether to update (i.e. re-download) data
    :param download_confirmation_required: [bool] (default: True) whether to confirm before downloading
    :param deep_retry: [bool] (default: False)
    :param verbose: [bool] (default: True)

    Example:
        subregion_name                 = 'london'
        osm_file_format                = ".osm.pbf"
        download_dir                   = None
        update                         = False
        download_confirmation_required = True
        verbose                        = True
        download_subregion_osm_file(subregion_name, osm_file_format=osm_file_format, download_dir=download_dir,
                                    update=update, download_confirmation_required=download_confirmation_required,
                                    verbose=verbose)
    """
    for sub_reg_name in subregion_name:

        # Get download URL
        subregion_name_, download_url = get_subregion_download_url(
            sub_reg_name, osm_file_format, update=False)

        if pd.isna(download_url):
            if verbose:
                print(
                    "\"{}\" data is not available for \"{}\" from the server. "
                    "Try to download the data of its subregions instead. ".
                    format(osm_file_format, subregion_name_))
            sub_subregions = retrieve_names_of_subregions_of(subregion_name_,
                                                             deep=deep_retry)
            download_dir_ = cd(
                download_dir,
                subregion_name_.replace(" ", "-").lower() +
                os.path.splitext(osm_file_format)[0])
            download_subregion_osm_file(
                *sub_subregions,
                osm_file_format=osm_file_format,
                download_dir=download_dir_,
                update=update,
                download_confirmation_required=download_confirmation_required,
                verbose=verbose)
        else:
            if not download_dir:
                # Download the requested OSM file to default directory
                osm_filename, path_to_file = get_default_path_to_osm_file(
                    subregion_name_, osm_file_format, mkdir=True)
            else:
                regulated_dir = regulate_input_data_dir(download_dir)
                osm_filename = get_default_osm_filename(
                    subregion_name_, osm_file_format=osm_file_format)
                path_to_file = os.path.join(regulated_dir, osm_filename)

            if os.path.isfile(path_to_file) and not update:
                print(
                    "\n\"{}\" for \"{}\" is already available: \"{}\".".format(
                        osm_filename, subregion_name_,
                        path_to_file)) if verbose else ""
            else:
                op = "Updating" if os.path.isfile(
                    path_to_file) else "Downloading"
                if confirmed(
                        "To download the {} data of \"{}\", saved as \"{}\"\n".
                        format(osm_file_format, subregion_name_, path_to_file),
                        confirmation_required=download_confirmation_required):
                    try:
                        from pyhelpers.download import download
                        download(download_url, path_to_file)
                        if verbose:
                            print("{} \"{}\" for \"{}\" ... Done.".format(
                                op, osm_filename, subregion_name_))
                    except Exception as e:
                        print("Failed to download \"{}\". {}.\n".format(
                            osm_filename, e)) if verbose else ""
                else:
                    print("The {} of \"{}\" was cancelled.\n".format(
                        op.lower(), osm_filename)) if verbose else ""