GeofabrikDownloader.download_osm_data

GeofabrikDownloader.download_osm_data(subregion_names, osm_file_format, download_dir=None, update=False, confirmation_required=True, deep_retry=False, interval=None, verbose=False, ret_download_path=False, **kwargs)

Download OSM data (in a specific format) of one (or multiple) geographic region(s).

Parameters
  • subregion_names (str or list) – name of a geographic region (or names of multiple geographic regions) available on Geofabrik free download server

  • osm_file_format (str) – file format of the OSM data available on the free download server

  • download_dir (str or None) – directory for saving the downloaded file(s); if None (default), the default directory created by the package

  • update (bool) – whether to update the data if it already exists, defaults to False

  • confirmation_required (bool) – whether asking for confirmation to proceed, defaults to True

  • deep_retry (bool) – whether to further check availability of sub-subregions data, defaults to False

  • interval (int or None) – interval (in sec) between downloading two subregions, defaults to None

  • verbose (bool or int) – whether to print relevant information in console, defaults to False

  • ret_download_path (bool) – whether to return the path(s) to the downloaded file(s), defaults to False

  • kwargs – optional parameters of pyhelpers.ops.download_file_from_url()

Returns

absolute path(s) to downloaded file(s) when ret_download_path is True

Return type

list or str

Examples:

>>> import os
>>> from pyhelpers.dir import delete_dir
>>> from pydriosm.downloader import GeofabrikDownloader, cd_dat_geofabrik

>>> geofabrik_downloader = GeofabrikDownloader()

>>> # Download PBF data file of Greater London and Rutland
>>> region_names = ['London', 'Rutland']  # Case-insensitive
>>> file_format = ".pbf"

>>> dwnld_paths = geofabrik_downloader.download_osm_data(region_names, file_format,
...                                                      verbose=True,
...                                                      ret_download_path=True)
To download .osm.pbf data of the following geographic region(s):
    Greater London
    Rutland
? [No]|Yes: yes
Downloading "greater-london-latest.osm.pbf" to "... ...\Greater London\" ... Done.
Downloading "rutland-latest.osm.pbf" to "... ...\Rutland\" ... Done.

>>> for dwnld_path in dwnld_paths:
...     print(os.path.relpath(dwnld_path))
osm_geofabrik\Europe\Great Britain\England\Greater London\greater-lond...
osm_geofabrik\Europe\Great Britain\England\Rutland\rutland-latest.osm.pbf

>>> # Delete the directory generated above
>>> delete_dir(cd_dat_geofabrik(), verbose=True)
The directory "osm_geofabrik\" is not empty.
Confirmed to delete it? [No]|Yes: yes
Deleting "osm_geofabrik\" ... Done.

>>> # Download shapefiles of West Midlands
>>> region_name = 'west midlands'  # Case-insensitive
>>> file_format = ".shp"
>>> dwnld_dir = "tests"

>>> dwnld_path = geofabrik_downloader.download_osm_data(region_name, file_format,
...                                                     dwnld_dir, verbose=True,
...                                                     ret_download_path=True)
To download .shp.zip data of the following geographic region(s):
    West Midlands
? [No]|Yes: yes
Downloading "west-midlands-latest-free.shp.zip" to "tests\" ... Done.

>>> print(os.path.relpath(dwnld_path))
tests\west-midlands-latest-free.shp.zip

>>> # Delete the downloaded .shp.zip file
>>> os.remove(dwnld_path)

>>> # Download shapefiles of Great Britain
>>> region_name = 'Great Britain'  # Case-insensitive
>>> file_format = ".shp"

>>> # By default, `deep_retry` is `False`
>>> dwnld_path = geofabrik_downloader.download_osm_data(region_name, file_format,
...                                                     dwnld_dir, verbose=True,
...                                                     ret_download_path=True)
To download .shp.zip data of the following geographic region(s):
    Great Britain
? [No]|Yes: yes
No .shp.zip data is found for "Great Britain".
Try to download the data of its subregions instead
? [No]|Yes: yes
Downloading "england-latest-free.shp.zip" to "tests\great-britain-sh...\ ... Done.
Downloading "scotland-latest-free.shp.zip" to "tests\great-britain-s...\ ... Done.
Downloading "wales-latest-free.shp.zip" to "tests\great-britain-shp-zip\ ... Done.

>>> # Set `deep_retry` to `True`
>>> dwnld_path = geofabrik_downloader.download_osm_data(region_name, file_format,
...                                                     dwnld_dir, deep_retry=True,
...                                                     verbose=True,
...                                                     ret_download_path=True)
To download .shp.zip data of the following geographic region(s):
    Great Britain
? [No]|Yes: yes
No .shp.zip data is found for "Great Britain".
Try to download the data of its subregions instead
? [No]|Yes: yes
"scotland-latest-free.shp.zip" is already available at "tests\great-britain-...\".
"wales-latest-free.shp.zip" is already available at "tests\great-britain-shp...\".
Downloading "bedfordshire-latest-free.shp.zip" to "tests\great-britain-\ ... Done.
... ...
Downloading "west-yorkshire-latest-free.shp.zip" to "tests\great-bri...\ ... Done.
Downloading "wiltshire-latest-free.shp.zip" to "tests\great-britain-...\ ... Done.
Downloading "worcestershire-latest-free.shp.zip" to "tests\great-bri...\ ... Done.

>>> # Check the file paths
>>> len(dwnld_path)
49
>>> os.path.commonpath(dwnld_path) == geofabrik_downloader.DownloadDir
True
>>> print(os.path.relpath(geofabrik_downloader.DownloadDir))
tests\great-britain-shp-zip

>>> # Delete the downloaded files
>>> delete_dir(geofabrik_downloader.DownloadDir, verbose=True)
The directory "tests\great-britain-shp-zip\" is not empty.
Confirmed to delete it? [No]|Yes: yes
Deleting "tests\great-britain-shp-zip\" ... Done.