unzip_shp_zip

pydriosm.reader.unzip_shp_zip(path_to_shp_zip, path_to_extract_dir=None, layer_names=None, mode='r', clustered=False, verbose=False, ret_extract_dir=False)

Unzip a .shp.zip file.

Parameters
  • path_to_shp_zip (str) – path to a zipped shapefile data (.shp.zip)

  • path_to_extract_dir (str or None) – path to a directory where extracted files will be saved; if None (default), the same directory where the .shp.zip file is saved

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

  • mode (str) – the mode parameter of zipfile.ZipFile(), defaults to 'r'

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

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

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

Returns

the path to the directory of extracted files when ret_extract_dir is set to be True

Return type

str

Examples:

>>> import os
>>> from pyhelpers.dir import cd, delete_dir
>>> from pydriosm.reader import GeofabrikDownloader, unzip_shp_zip

>>> geofabrik_downloader = GeofabrikDownloader()

>>> path_to_rutland_shp_zip = geofabrik_downloader.download_osm_data(
...     subregion_names='Rutland', osm_file_format=".shp.zip", download_dir="tests",
...     verbose=True, ret_download_path=True)
To download .shp.zip data of the following geographic region(s):
    Rutland
? [No]|Yes: yes
Downloading "rutland-latest-free.shp.zip" to "tests\" ... Done.

>>> layer_name = 'railways'

>>> unzip_shp_zip(path_to_rutland_shp_zip, layer_names=layer_name, verbose=True)
Extracting the following layer(s):
    'railways'
from "tests\rutland-latest-free.shp.zip" ...
to "tests\rutland-latest-free-shp\"

>>> path_to_rutland_shp_dir = unzip_shp_zip(path_to_rutland_shp_zip, verbose=True,
...                                         ret_extract_dir=True)
Extracting "tests\rutland-latest-free.shp.zip" ...
to "tests\rutland-latest-free-shp\"
Done.

>>> print(os.path.relpath(path_to_rutland_shp_dir))
tests\rutland-latest-free-shp

>>> lyr_names = ['railways', 'transport', 'traffic']

>>> paths_to_layer_dirs = unzip_shp_zip(path_to_rutland_shp_zip, layer_names=lyr_names,
...                                     clustered=True, verbose=2, ret_extract_dir=True)
Extracting the following layer(s):
    'railways'
    'transport'
    'traffic'
from "tests\rutland-latest-free.shp.zip" ...
to "tests\rutland-latest-free-shp\"
Done.
Clustering layer data ...
    traffic ... Done.
    transport_a ... Done.
    transport ... Done.
    railways ... Done.
    traffic_a ... Done.
All done.

>>> for path_to_lyr_dir in paths_to_layer_dirs:
...     print(os.path.relpath(path_to_lyr_dir))
tests\rutland-latest-free-shp\railways
tests\rutland-latest-free-shp\transport
tests\rutland-latest-free-shp\traffic

>>> # Delete the extracted files
>>> delete_dir(os.path.commonpath(paths_to_layer_dirs), verbose=True)
The directory "tests\rutland-latest-free-shp\" is not empty.
Confirmed to delete it? [No]|Yes: yes
Deleting "tests\rutland-latest-free-shp\" ... Done.

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