SHPReadParse.merge_layer_shps

classmethod SHPReadParse.merge_layer_shps(shp_zip_pathnames, layer_name, engine='pyshp', rm_zip_extracts=True, output_dir=None, rm_shp_temp=True, ret_shp_pathname=False, verbose=False)[source]

Merge shapefiles over a layer for multiple geographic regions.

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

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

  • engine (str) – the open-source package used to merge/save shapefiles; options include: 'pyshp' (default) and 'geopandas' (or 'gpd') if engine='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

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

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

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

Returns:

the path to the merged file when ret_merged_shp_path=True

Return type:

list

Note

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

  • For valid layer_name, check the function valid_shapefile_layer_names().

Examples:

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

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

>>> # Download the .shp.zip file of Manchester and West Yorkshire
>>> subrgn_names = ['Greater Manchester', 'West Yorkshire']
>>> file_fmt = ".shp"
>>> data_dir = "tests\osm_data"

>>> gfd = GeofabrikDownloader()

>>> gfd.download_osm_data(subrgn_names, file_fmt, data_dir, verbose=True)
To download .shp.zip data of the following geographic (sub)region(s):
    Greater Manchester
    West Yorkshire
? [No]|Yes: yes
Downloading "greater-manchester-latest-free.shp.zip"
    to "tests\osm_data\greater-manchester\" ... Done.
Downloading "west-yorkshire-latest-free.shp.zip"
    to "tests\osm_data\west-yorkshire\" ... Done.

>>> os.path.relpath(gfd.download_dir)
'tests\osm_data'
>>> len(gfd.data_paths)
2

>>> # Merge the layers of 'railways' of the two subregions
>>> merged_shp_path = SHPReadParse.merge_layer_shps(
...     gfd.data_paths, layer_name='railways', verbose=True, ret_shp_pathname=True)
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\osm_data\gre_man-wes_yor-railways\".

>>> # Check the pathname of the merged shapefile
>>> type(merged_shp_path)
list
>>> len(merged_shp_path)
1
>>> os.path.relpath(merged_shp_path[0])
'tests\osm_data\gre_man-wes_yor-railways\linestring.shp'

>>> # Read the merged .shp file
>>> merged_shp_data = SHPReadParse.read_shp(merged_shp_path[0], emulate_gpd=True)
>>> merged_shp_data.head()
    osm_id  code  ... tunnel                                           geometry
0   928999  6101  ...      F  LINESTRING (-2.2844621 53.4802635, -2.2851997 ...
1   929904  6101  ...      F  LINESTRING (-2.2917977 53.4619559, -2.2924877 ...
2   929905  6102  ...      F  LINESTRING (-2.2794048 53.4605819, -2.2799722 ...
3  3663332  6102  ...      F  LINESTRING (-2.2382139 53.4817985, -2.2381708 ...
4  3996086  6101  ...      F  LINESTRING (-2.6003053 53.4604346, -2.6005261 ...
[5 rows x 8 columns]

>>> # Delete the test 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.

See also