SHP.unzip_shp_zip

classmethod SHP.unzip_shp_zip(shp_zip_pathname, extract_to=None, layer_names=None, separate=False, ret_extract_dir=False, verbose=False, raise_error=False)[source]

Unzip a zipped shapefile.

Parameters:
  • shp_zip_pathname (str | os.PathLike[str]) – path to a zipped shapefile data (.shp.zip)

  • extract_to (str | None) – path to a directory where extracted files will be saved; when extract_to=None (default), the same directory where the .shp.zip file is saved

  • layer_names (str | list | None) – name of a .shp layer, e.g. ‘railways’, or names of multiple layers; when layer_names=None (default), all available layers

  • separate (bool) – whether to put the data files of different layer in respective folders, defaults to False

  • ret_extract_dir (bool) – whether to return the pathname of the directory where extracted files are saved, defaults to False

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

  • raise_error (bool) – Whether to raise the provided exception; if raise_error=False (default), the error will be suppressed.

Returns:

the path to the directory of extracted files when ret_extract_dir=True

Return type:

str

Examples:

>>> from pydriosm.reader._shp import SHP
>>> from pydriosm.downloader import BBBikeDownloader
>>> from pyhelpers.dirs import cd, delete_dir
>>> import os

>>> # Download the shapefile data of London as an example
>>> subrgn_name = 'birmingham'
>>> file_format = ".shp"
>>> dwnld_dir = "tests/osm_data"

>>> bbd = BBBikeDownloader()

>>> bbd.download_data(subrgn_name, file_format, dwnld_dir, verbose=True)
Proceed to update the data in the format '.shp.zip' for the following geographic (s...
    "Birmingham"
  in "./tests/osm_data/birmingham/"
? [No]|Yes: yes
Downloading "Birmingham.osm.shp.zip" 100%|██████████| 79.1M/79.1M | 17.7MB/s ...
  Updating "Birmingham.osm.shp.zip" in "./tests/osm_data/birmingham/" ... Done.

>>> path_to_shp_zip = bbd.data_paths[0]
>>> os.path.relpath(path_to_shp_zip)
'tests\osm_data\birmingham\Birmingham.osm.shp.zip'

>>> # To extract data of a specific layer 'railways'
>>> bham_railways_dir = SHP.unzip_shp_zip(
...     path_to_shp_zip, layer_names='railways', verbose=True, ret_extract_dir=True)
Extracting the following layer(s):
    'railways'
    from "tests\osm_data\greater-london\greater-london-latest-free.shp.zip"
      to "tests\osm_data\greater-london\greater-london-latest-free-shp\" ... Done.

>>> os.path.relpath(bham_railways_dir)  # Check the directory
'tests\osm_data\birmingham\Birmingham-osm-shp'

>>> # When multiple layer names are specified, the extracted files for each of the
>>> # layers can be put into a separate subdirectory by setting `separate=True`:
>>> lyr_names = ['railways', 'landuse']
>>> dirs_of_layers = SHP.unzip_shp_zip(
...     shp_zip_pathname=path_to_shp_zip, layer_names=lyr_names, separate=True,
...     verbose=2, ret_extract_dir=True)
Extracting the following layer(s):
  'railways'
  'landuse'
  from: "./tests/osm_data/birmingham/Birmingham.osm.shp.zip" ...
    to: "./tests/osm_data/birmingham/Birmingham-osm-shp/" ... Done.
Grouping files by layers ...
  landuse ... Done.
  railways ... Done.
Done.

>>> len(dirs_of_layers) == 2
True
>>> os.path.relpath(os.path.commonpath(dirs_of_layers))
'tests\osm_data\birmingham\Birmingham-osm-shp'
>>> set(map(os.path.basename, dirs_of_layers))
{'landuse', 'railways'}

>>> # Remove the subdirectories
>>> delete_dir(dirs_of_layers, confirmation_required=False)

>>> # To extract all (without specifying `layer_names`
>>> bham_shp_dir = SHP.unzip_shp_zip(
...     path_to_shp_zip, verbose=True, ret_extract_dir=True)
Extracting "./tests/osm_data/birmingham/Birmingham.osm.shp.zip"
  to "./tests/osm_data/birmingham/Birmingham-osm-shp/" ... Done.

>>> # Check the directory
>>> os.path.relpath(bham_shp_dir)
'tests\osm_data\birmingham\Birmingham-osm-shp'
>>> list_of_files = os.listdir(cd(bham_shp_dir, "Birmingham-shp/shape"))
>>> len(list_of_files)
40
>>> # Get the names of all available layers
>>> set(filter(None, map(SHP.get_layer_name, list_of_files)))
{'buildings',
 'landuse',
 'natural',
 'places',
 'points',
 'railways',
 'roads',
 'waterways'}

>>> # Delete the download/data directory
>>> delete_dir(bbd.download_dir, verbose=True)
To delete the directory "./tests/osm_data/" (Not empty)
? [No]|Yes: yes
Deleting "./tests/osm_data/" ... Done.