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)[source]

Unzip a .shp.zip file data.

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

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

  • 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=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()

>>> sr_name = 'Rutland'
>>> file_fmt = ".shp"
>>> dwnld_dir = "tests"

>>> path_to_rutland_shp_zip = geofabrik_downloader.download_osm_data(
...     sr_name, file_fmt, dwnld_dir, ret_download_path=True)
Confirmed to download .shp.zip data of the following geographic region(s):
    Rutland
? [No]|Yes: yes

>>> layer_name = 'railways'

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

>>> path_to_rutland_shp_dir = unzip_shp_zip(path_to_rutland_shp_zip, verbose=True,
...                                         ret_extract_dir=True)
Extracting all of "rutland-latest-free.shp.zip" to "\tests\rutland-latest-free-shp"
In progress ... 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 from "rutland-latest-free.shp.zip" the following layer(s):
    'railways'
    'transport'
    'traffic'
to "\tests\rutland-latest-free-shp" ...
In progress ... Done.
Clustering the layer data ...
    railways ...
    transport ...
    traffic ...
    traffic_a ...
    transport_a ...
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.dirname(path_to_lyr_dir), 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)