SHPReadParse.write_to_shapefile

classmethod SHPReadParse.write_to_shapefile(data, write_to, shp_filename=None, decimal_precision=5, ret_shp_pathname=False, verbose=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; when shp_filename=None, it is by default the basename of write_to

  • decimal_precision (int) – decimal precision for writing float records, defaults to 5

  • ret_shp_pathname (bool) – whether to return the pathname of the output .shp file, defaults to False

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

Examples:

>>> from pydriosm.reader import SHPReadParse
>>> from pydriosm.downloader import GeofabrikDownloader
>>> from pyhelpers.dirs import cd, delete_dir
>>> import os
>>> import glob

>>> # 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.

>>> london_shp_zip = gfd.data_paths[0]
>>> os.path.relpath(london_shp_zip)
'tests\osm_data\greater-london\greater-london-latest-free.shp.zip'

>>> # Extract the 'railways' layer of the downloaded .shp.zip file
>>> lyr_name = 'railways'

>>> railways_shp_dir = SHPReadParse.unzip_shp_zip(
...     london_shp_zip, layer_names=lyr_name, 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.
>>> # Check out the output directory
>>> os.path.relpath(railways_shp_dir)
'tests\osm_data\greater-london\greater-london-latest-free-shp'

>>> # Get the pathname of the .shp data of 'railways'
>>> path_to_railways_shp = glob.glob(cd(railways_shp_dir, f"*{lyr_name}*.shp"))[0]
>>> os.path.relpath(path_to_railways_shp)  # Check the pathname of the .shp file
'tests\osm_data\greater-london\greater-london-latest-free-shp\gis_osm_railwa...

>>> # Read the .shp file
>>> london_railways_shp = SHPReadParse.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\greater-london\railways'

>>> # Save the data of 'railways' to the new directory
>>> path_to_railways_shp_ = SHPReadParse.write_to_shapefile(
...     london_railways_shp, railways_subdir, ret_shp_pathname=True, verbose=True)
Writing data to "tests\osm_data\greater-london\railways\railways.*" ... Done.
>>> os.path.basename(path_to_railways_shp_)
'railways.shp'

>>> # If `shp_filename` is specified
>>> path_to_railways_shp_ = SHPReadParse.write_to_shapefile(
...     london_railways_shp, railways_subdir, shp_filename="rail_data",
...     ret_shp_pathname=True, verbose=True)
Writing data to "tests\osm_data\greater-london\railways\rail_data.*" ... Done.
>>> os.path.basename(path_to_railways_shp_)
'rail_data.shp'

>>> # Retrieve the saved the .shp file
>>> london_railways_shp_ = SHPReadParse.read_shp(path_to_railways_shp_)

>>> # Check if the retrieved .shp data is equal to the original one
>>> london_railways_shp_.equals(london_railways_shp)
True

>>> # 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.