SHPReadParse.unzip_shp_zip
- classmethod SHPReadParse.unzip_shp_zip(shp_zip_pathname, extract_to=None, layer_names=None, separate=False, ret_extract_dir=False, verbose=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 savedlayer_names (str | list | None) – name of a .shp layer, e.g. ‘railways’, or names of multiple layers; when
layer_names=None
(default), all available layersseparate (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
- Returns:
the path to the directory of extracted files when
ret_extract_dir=True
- Return type:
str
Examples:
>>> from pydriosm.reader import SHPReadParse >>> from pydriosm.downloader import GeofabrikDownloader >>> from pyhelpers.dirs import cd, delete_dir >>> import os >>> # Download the shapefile data of London as an example >>> subrgn_name = 'london' >>> file_format = ".shp" >>> dwnld_dir = "tests\osm_data" >>> gfd = GeofabrikDownloader() >>> gfd.download_osm_data(subrgn_name, file_format, dwnld_dir, verbose=True) To download .shp.zip data of the following geographic (sub)region(s): Greater London ? [No]|Yes: yes Downloading "greater-london-latest-free.shp.zip" to "tests\osm_data\greater-london\" ... Done. >>> path_to_shp_zip = gfd.data_paths[0] >>> os.path.relpath(path_to_shp_zip) 'tests\osm_data\greater-london\greater-london-latest-free.shp.zip' >>> # To extract data of a specific layer 'railways' >>> london_railways_dir = SHPReadParse.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(london_railways_dir) # Check the directory 'tests\osm_data\greater-london\greater-london-latest-free-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', 'transport', 'traffic'] >>> dirs_of_layers = SHPReadParse.unzip_shp_zip( ... path_to_shp_zip, layer_names=lyr_names, separate=True, verbose=2, ... ret_extract_dir=True) Extracting the following layer(s): 'railways' 'transport' 'traffic' from "tests\osm_data\greater-london\greater-london-latest-free.shp.zip" to "tests\osm_data\greater-london\greater-london-latest-free-shp\" ... Done. Grouping files by layers ... railways ... Done. transport_a ... Done. transport ... Done. traffic_a ... Done. traffic ... Done. Done. >>> len(dirs_of_layers) == 3 True >>> os.path.relpath(os.path.commonpath(dirs_of_layers)) 'tests\osm_data\greater-london\greater-london-latest-free-shp' >>> set(map(os.path.basename, dirs_of_layers)) {'railways', 'traffic', 'transport'} >>> # Remove the subdirectories >>> delete_dir(dirs_of_layers, confirmation_required=False) >>> # To extract all (without specifying `layer_names` >>> london_shp_dir = SHPReadParse.unzip_shp_zip( ... path_to_shp_zip, verbose=True, ret_extract_dir=True) Extracting "tests\osm_data\greater-london\greater-london-latest-free.shp.zip" to "tests\osm_data\greater-london\greater-london-latest-free-shp\" ... Done. >>> # Check the directory >>> os.path.relpath(london_shp_dir) 'tests\osm_data\greater-london\greater-london-latest-free-shp' >>> len(os.listdir(london_shp_dir)) 91 >>> # Get the names of all available layers >>> set(filter(None, map(SHPReadParse.find_shp_layer_name, os.listdir(london_shp_dir)))) {'buildings', 'landuse', 'natural', 'places', 'pofw', 'pois', 'railways', 'roads', 'traffic', 'transport', 'water', 'waterways'} >>> # Delete the download/data directory >>> delete_dir(gfd.download_dir, verbose=True) To delete the directory "tests\osm_data\" (Not empty) ? [No]|Yes: yes Deleting "tests\osm_data\" ... Done.