write_to_shapefile

pydriosm.reader.write_to_shapefile(shp_data, path_to_shp, decimal_precision=5, prj_file=True)

Save .shp data as a shapefile by pyshp.

Parameters
  • shp_data (pandas.DataFrame) – .shp data

  • path_to_shp (str) – path where the .shp data is saved

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

  • prj_file (bool) – whether to create a .prj projection file for the shapefile, defaults to True

Example:

>>> import os
>>> import glob
>>> from pyhelpers.dir import cd, delete_dir
>>> from pydriosm.downloader import GeofabrikDownloader
>>> from pydriosm.reader import read_shp_file, unzip_shp_zip, write_to_shapefile

>>> # Download the .shp.zip file of Rutland as an example
>>> geofabrik_downloader = GeofabrikDownloader()

>>> sr_name = 'Rutland'

>>> path_to_rutland_shp_zip = geofabrik_downloader.download_osm_data(
...     sr_name, osm_file_format=".shp", download_dir="tests",
...     confirmation_required=False, ret_download_path=True)

>>> # Extract the downloaded .shp.zip file
>>> rutland_shp_dir = unzip_shp_zip(path_to_rutland_shp_zip, layer_names='railways',
...                                 ret_extract_dir=True)

>>> railways_shp_filename = glob.glob1(rutland_shp_dir, "*.shp")[0]
>>> path_to_railways_shp = cd(rutland_shp_dir, railways_shp_filename)

>>> # Read the .shp file
>>> rutland_railways_shp = read_shp_file(path_to_railways_shp)

>>> # Save the railways data as "tests\rutland\railways.shp"
>>> save_shp_path = "tests\rutland\railways"  # with or without the extension ".shp"
>>> write_to_shapefile(rutland_railways_shp, save_shp_path)

>>> # Read the saved the .shp file
>>> rutland_railways_shp_ = read_shp_file(save_shp_path)

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

>>> # Delete the extracted data files
>>> delete_dir(rutland_shp_dir, confirmation_required=False, verbose=True)
Deleting "tests\rutland-latest-free-shp\" ... Done.
>>> delete_dir("tests\rutland", confirmation_required=False, verbose=True)
Deleting "tests\rutland\" ... Done.

>>> # Delete the downloaded shapefile
>>> os.remove(path_to_rutland_shp_zip)