merge_layer_shps

pydriosm.reader.merge_layer_shps(paths_to_shp_zip_files, layer_name, method='pyshp', rm_zip_extracts=True, merged_shp_dir=None, rm_shp_temp=True, verbose=False, ret_merged_shp_path=False)

Merge shapefiles over a layer for multiple geographic regions.

Parameters
  • paths_to_shp_zip_files (list) – list of paths to data of shapefiles (in .shp.zip format)

  • layer_name (str) – name of a layer (e.g. ‘railways’)

  • method (str) – the method used to merge/save shapefiles; options include: 'pyshp' (default) and 'geopandas' (or 'gpd') if method='geopandas', this function relies on geopandas.GeoDataFrame.to_file(); otherwise, it by default uses shapefile.Writer()

  • rm_zip_extracts (bool) – whether to delete the extracted files, defaults to False

  • rm_shp_temp (bool) – whether to delete temporary layer files, defaults to False

  • merged_shp_dir (str or None) – if None (default), use the layer name as the name of the folder where the merged .shp files will be saved

  • verbose (bool or int) – whether to print relevant information in console as the function runs, defaults to False

  • ret_merged_shp_path (bool) – whether to return the path to the merged .shp file, defaults to False

Returns

the path to the merged file when ret_merged_shp_path=True

Return type

list or str

Note

This function does not create projection (.prj) for the merged map (see also [MMS-1])

For valid layer_name, check get_valid_shp_layer_names().

Example:

>>> import os
>>> from pyhelpers.dir import delete_dir
>>> from pydriosm.downloader import GeofabrikDownloader
>>> from pydriosm.reader import merge_layer_shps, read_shp_file

>>> # To merge 'railways' layers of Greater Manchester and West Yorkshire"

>>> geofabrik_downloader = GeofabrikDownloader()

>>> sr_names = ['Greater Manchester', 'West Yorkshire']
>>> dat_dir = "tests"

>>> shp_zip_file_paths = geofabrik_downloader.download_osm_data(
...     sr_names, osm_file_format=".shp", download_dir=dat_dir,
...     confirmation_required=False, ret_download_path=True, verbose=True)
Downloading "greater-manchester-latest-free.shp.zip" to "tests\" ... Done.
Downloading "west-yorkshire-latest-free.shp.zip" to "tests\" ... Done.

>>> lyr_name = 'railways'

>>> merged_shp_path = merge_layer_shps(shp_zip_file_paths, layer_name=lyr_name,
...                                    verbose=True, ret_merged_shp_path=True)
Extracting the following layer(s):
    'railways'
from "tests\greater-manchester-latest-free.shp.zip" ...
to "tests\greater-manchester-latest-free-shp\"
Done.
Extracting the following layer(s):
    'railways'
from "tests\west-yorkshire-latest-free.shp.zip" ...
to "tests\west-yorkshire-latest-free-shp\"
Done.
Merging the following shapefiles:
    "greater-manchester_gis_osm_railways_free_1.shp"
    "west-yorkshire_gis_osm_railways_free_1.shp"
In progress ... Done.
Find the merged shapefile at "tests\greater-manchester_west-yorkshire_railways\".

>>> print(os.path.relpath(merged_shp_path))
tests\greater-manchester_west-yorksh...\greater-manchester_west-yorkshire_railways.shp

>>> # Read the merged .shp file
>>> merged_shp_data = read_shp_file(merged_shp_path)

>>> # Delete the merged shapefile
>>> delete_dir(os.path.dirname(merged_shp_path), verbose=True)
The directory "tests\greater-manchester_west-yorkshire_railways\" is not empty.
Confirmed to delete it? [No]|Yes: yes
Deleting "tests\greater-manchester_west-yorkshire_railways\" ... Done.

>>> # Delete the downloaded shapefiles
>>> for shp_zip_file_path in shp_zip_file_paths:
...     os.remove(shp_zip_file_path)

See also

The examples for the method GeofabrikReader.merge_subregion_layer_shp().