GeofabrikDownloader.download_data

GeofabrikDownloader.download_data(subregion_names, osm_file_formats, download_dir=None, update=False, confirmation_required=True, deep=False, interval=None, verify_download_dir=True, verbose=False, ret_download_path=False, **kwargs)[source]

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

Parameters:
  • subregion_names (str | list) – name of a geographic (sub)region (or names of multiple geographic (sub)regions) available on Geofabrik.

  • osm_file_formats (str | list) – file format/extension of the OSM data available on the download server

  • download_dir (str | None) – directory for saving the downloaded file(s), defaults to None; when download_dir=None, it refers to cdd_geofabrik().

  • 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 (bool) – whether to further check availability of sub-subregions data, defaults to False

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

  • verify_download_dir (bool) – whether to verify the pathname of the current download directory, defaults to True

  • verbose (bool | 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 | str

Examples:

>>> from pydriosm.downloader import GeofabrikDownloader
>>> from pyhelpers.dirs import delete_dir
>>> import os

*Example 1*:

>>> gfd = GeofabrikDownloader()
>>> # Download PBF data file of 'Greater London' and 'Rutland'
>>> subregion_names = ['Isle of Wight', 'rutland']  # Case-insensitive
>>> osm_file_format = ".pbf"
>>> gfd.download_data(subregion_names, osm_file_format, verbose=True)
Proceed to download data in the format '.osm.pbf' for the following geographic (sub...
    "Isle of Wight"
    "Rutland"
  to "./osm_data/geofabrik/europe/united-kingdom/england/"
? [No]|Yes: yes
Downloading "isle-of-wight-latest.osm.pbf" 100%|██████████| 8.83M/8.83M | 16....
  Saving "isle-of-wight-latest.osm.pbf" to "./osm_data/geofabrik/europe/united-king...
Downloading "rutland-latest.osm.pbf" 100%|██████████| 1.89M/1.89M | 4.58MB/s ...
  Saving "rutland-latest.osm.pbf" to "./osm_data/geofabrik/europe/united-kingdom/en...
>>> len(gfd.data_paths)
2
>>> for file_path in gfd.data_paths: print(os.path.basename(file_path))
isle-of-wight-latest.osm.pbf
rutland-latest.osm.pbf
>>> # Since `download_dir` was not specified when instantiating the class,
>>> #   the data is now in the default download directory
>>> os.path.relpath(gfd.download_dir)  # (on Windows)
'osm_data\geofabrik'
>>> download_dir_ = os.path.dirname(gfd.download_dir)

>>> # Download shapefiles of West Midlands (to a given directory "tests/osm_data")
>>> subregion_name = 'west midlands'  # Case-insensitive
>>> osm_file_format = [".shp", ".gpkg", "pbf"]
>>> download_dir = "tests/osm_data"
>>> gfd.download_data(subregion_name, osm_file_format, download_dir, verbose=True)
Proceed to download data in the formats ('.shp.zip', '.gpkg.zip', '.osm.pbf') for t...
    "West Midlands"
? [No]|Yes: yes
No '.shp.zip' data is available for "West Midlands".
Try to download the data of its subregions instead
? [No]|Yes: no
Downloading "west-midlands-latest-free.gpkg.zip" 100%|██████████| 103M/103M |...
  Saving "west-midlands-latest-free.gpkg.zip" to "./tests/osm_data/west-midlands/" ...
Downloading "west-midlands-latest.osm.pbf" 100%|██████████| 58.4M/58.4M | 10....
  Saving "west-midlands-latest.osm.pbf" to "./tests/osm_data/west-midlands/" ... Done.
>>> len(gfd.data_paths)
4
>>> os.path.relpath(gfd.data_paths[-1])  # (on Windows)
'tests\osm_data\west-midlands\west-midlands-latest.osm.pbf'
>>> # Now the `.download_dir` variable has changed to the given one `download_dir`
>>> os.path.relpath(gfd.download_dir)  # (on Windows)
'tests\osm_data'
>>> # while `.cdd()` remains the default one
>>> os.path.relpath(gfd.cdd())  # (on Windows)
'osm_data\geofabrik'
>>> # Delete the above downloaded directories
>>> delete_dir([download_dir_, gfd.download_dir], verbose=True)
To delete the following directories:
  "./osm_data/" (Not empty)
  "./tests/osm_data/" (Not empty)
? [No]|Yes: yes
Deleting:
  "./osm_data/" ... Done.
  "./tests/osm_data/" ... Done.

*Example 2*:

>>> # Create a new instance with a pre-specified download directory
>>> gfd = GeofabrikDownloader(download_dir="tests/osm_data")
>>> os.path.relpath(gfd.download_dir)  # (on Windows)
'tests\osm_data'
>>> # Download shapefiles of UK (to the directory specified by instantiation)
>>> # (Note that .shp.zip data is not available for "United Kingdom".)
>>> subregion_name = 'United Kingdom'  # Case-insensitive
>>> osm_file_format = ".shp"
>>> # By default, `deep_retry=False`
>>> gfd.download_data(subregion_name, osm_file_format, verbose=True)
Proceed to download data in the format '.shp.zip' for the following geographic (sub...
    "United Kingdom"
? [No]|Yes: yes
No '.shp.zip' data is available for "United Kingdom".
Try to download the data of its subregions instead
? [No]|Yes: yes
Downloading "bermuda-latest-free.shp.zip" 100%|██████████| 4.04M/4.04M | 9.08...
  Saving "bermuda-latest-free.shp.zip" to "./tests/osm_data/europe/united-kingdom/u...
Downloading "england-latest-free.shp.zip" 100%|██████████| 2.78G/2.78G | 36.2...
  Saving "england-latest-free.shp.zip" to "./tests/osm_data/europe/united-kingdom/u...
Downloading "falklands-latest-free.shp.zip" 100%|██████████| 11.0M/11.0M | 13...
  Saving "falklands-latest-free.shp.zip" to "./tests/osm_data/europe/united-kingdom...
Downloading "scotland-latest-free.shp.zip" 100%|██████████| 561M/561M | 33.2M...
  Saving "scotland-latest-free.shp.zip" to "./tests/osm_data/europe/united-kingdom/...
Downloading "wales-latest-free.shp.zip" 100%|██████████| 252M/252M | 33.6MB/s...
  Saving "wales-latest-free.shp.zip" to "./tests/osm_data/europe/united-kingdom/uni...
>>> len(gfd.data_paths)
5
>>> # Now set `deep_retry=True`
>>> gfd.download_data(subregion_name, osm_file_format, verbose=1, deep=True)
Proceed to download data in the format '.shp.zip' for the following geographic (sub...
    "Lancashire"
    "Scotland"
    "Merseyside"
    ...
    "West Sussex"
  to "./tests/osm_data/europe/united-kingdom/"
? [No]|Yes: yes
Downloading "wales-latest-free.shp.zip" 100%|██████████| 252M/252M | 28.7MB/s...
  Saving "wales-latest-free.shp.zip" to "./tests/osm_data/europe/united-kingdom/wal...
Downloading "scotland-latest-free.shp.zip" 100%|██████████| 561M/561M | 31.3M...
  Saving "scotland-latest-free.shp.zip" to "./tests/osm_data/europe/united-kingdom/...
Downloading "falklands-latest-free.shp.zip" 100%|██████████| 11.0M/11.0M | 5....
  Saving "falklands-latest-free.shp.zip" to "./tests/osm_data/europe/united-kingdom...
...
    ...
Downloading "rutland-latest-free.shp.zip" 100%|██████████| 2.71M/2.71M | 7.33...
  Saving "rutland-latest-free.shp.zip" to "./tests/osm_data/europe/united-kingdom/e...
...
    ...
Downloading "west-yorkshire-latest-free.shp.zip" 100%|██████████| 87.3M/87.3M...
  Saving "west-yorkshire-latest-free.shp.zip" to "./tests/osm_data/europe/united-ki...
Downloading "wiltshire-latest-free.shp.zip" 100%|██████████| 62.7M/62.7M | 28...
  Saving "wiltshire-latest-free.shp.zip" to "./tests/osm_data/europe/united-kingdom...
Downloading "worcestershire-latest-free.shp.zip" 100%|██████████| 35.0M/35.0M...
  Saving "worcestershire-latest-free.shp.zip" to "./tests/osm_data/europe/united-ki...
>>> # Check the file paths
>>> len(gfd.data_paths)
56
>>> # Check the current default `download_dir`
>>> os.path.relpath(gfd.download_dir)  # (on Windows)
'tests\osm_data'
>>> # Delete all the downloaded files
>>> delete_dir(gfd.download_dir, verbose=True)
To delete the directory "./tests/osm_data/" (Not empty)
? [No]|Yes: yes
Deleting "./tests/osm_data/" ... Done.