merge_layer_shps

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

Merge shapefiles over a layer for multiple geographic regions.

Parameters
  • paths_to_shp_zip_files (list) – list of absolute 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 .shp files; if 'geopandas' (default), use the geopandas.GeoDataFrame.to_file method, use shapefile.Writer otherwise

  • 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

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

>>> 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 from "greater-manchester-latest-free.shp.zip" the following layer(s):
    'railways'
to "\tests\greater-manchester-latest-free-shp" ...
In progress ... Done.
Extracting from "west-yorkshire-latest-free.shp.zip" the following layer(s):
    'railways'
to "\tests\west-yorkshire-latest-free-shp" ...
In progress ... 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 .shp file(s) at "\tests\greater-manchester_west-yorkshire_railways".

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

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