SHP.write_to_shapefile¶
- classmethod SHP.write_to_shapefile(data, write_to, shp_filename=None, decimal_precision=5, ret_shp_pathname=False, verbose=False, raise_error=False)[source]¶
Save .shp data as a shapefile by PyShp.
- Parameters:
data (pandas.DataFrame) – data of a shapefile
write_to (str) – pathname of a directory where the shapefile data is to be saved
shp_filename (str | os.PahtLike[str] | None) – filename (or pathname) of the target .shp file, defaults to
None; whenshp_filename=None, it is by default the basename ofwrite_todecimal_precision (int) – decimal precision for writing float records, defaults to
5ret_shp_pathname (bool) – whether to return the pathname of the output .shp file, defaults to
Falseverbose (bool | int) – whether to print relevant information in console, defaults to
Falseraise_error (bool) – Whether to raise the provided exception; if
raise_error=False(default), the error will be suppressed.
Examples:
>>> from pydriosm.reader._shp import SHP >>> from pydriosm.downloader import BBBikeDownloader >>> from pyhelpers.dirs import cd, delete_dir >>> import os >>> import glob >>> # 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 download data in the format '.shp.zip' for the following geographic (sub... "Birmingham" to "./tests/osm_data/birmingham/" ? [No]|Yes: yes Downloading "Birmingham.osm.shp.zip" 100%|██████████| 79.1M/79.1M | 18.1MB/s ... Saving "Birmingham.osm.shp.zip" to "./tests/osm_data/birmingham/" ... Done. >>> bham_shp_zip = bbd.data_paths[0] >>> os.path.relpath(bham_shp_zip) 'tests\osm_data\birmingham\Birmingham.osm.shp.zip' >>> # Extract the 'railways' layer of the downloaded .shp.zip file >>> lyr_name = 'railways' >>> railways_shp_dir = SHP.unzip_shp_zip( ... bham_shp_zip, layer_names=lyr_name, verbose=True, ret_extract_dir=True) Extracting the following layer(s): 'railways' from: "./tests/osm_data/birmingham/Birmingham.osm.shp.zip" ... to: "./tests/osm_data/birmingham/Birmingham-osm-shp/" ... Done. >>> # Check out the output directory >>> os.path.relpath(railways_shp_dir) 'tests\osm_data\birmingham\Birmingham-osm-shp' >>> # Get the pathname of the .shp data of 'railways' >>> path_to_railways_shp = glob.glob( ... cd(railways_shp_dir, "Birmingham-shp", "shape", f"*{lyr_name}*.shp"))[0] >>> os.path.relpath(path_to_railways_shp) # Check the pathname of the .shp file 'tests\osm_data\birmingham\Birmingham-osm-shp\Birmingham-shp\shape\railways.shp' >>> # Read the .shp file >>> bham_railways_shp = SHP.read_shp(path_to_railways_shp) >>> # Create a new directory for saving the 'railways' data >>> railways_subdir = cd(os.path.dirname(railways_shp_dir), lyr_name) >>> os.path.relpath(railways_subdir) 'tests\osm_data\birmingham\railways' >>> # Save the data of 'railways' to the new directory >>> path_to_railways_shp_ = SHP.write_to_shapefile( ... bham_railways_shp, railways_subdir, ret_shp_pathname=True, verbose=True) Writing data to "tests/osm_data/birmingham/railways.*" ... Done. >>> os.path.basename(path_to_railways_shp_) 'railways.shp' >>> # If `shp_filename` is specified >>> path_to_railways_shp_ = SHP.write_to_shapefile( ... bham_railways_shp, railways_subdir, shp_filename="rail_data", ... ret_shp_pathname=True, verbose=True) Writing data to "tests/osm_data/birmingham/rail_data.*" ... Done. >>> os.path.basename(path_to_railways_shp_) 'rail_data.shp' >>> # Retrieve the saved the .shp file >>> bham_railways_shp_ = SHP.read_shp(path_to_railways_shp_) >>> # Check if the retrieved .shp data is equal to the original one >>> bham_railways_shp_.equals(bham_railways_shp) True >>> # 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.