GeofabrikReader.merge_subregion_layer_shp

GeofabrikReader.merge_subregion_layer_shp(layer_name, subregion_names, data_dir=None, method='geopandas', update=False, download_confirmation_required=True, rm_zip_extracts=True, merged_shp_dir=None, rm_shp_temp=True, verbose=False, ret_merged_shp_path=False)[source]

Merge shapefiles for a specific layer of two or multiple geographic regions.

Parameters
  • subregion_names (list) – a list of region/subregion names (case-insensitive) that are available on Geofabrik’s free download server

  • 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, otherwise, use shapefile.Writer

  • update (bool) – whether to update the source .shp.zip files, defaults to False

  • download_confirmation_required (bool) – whether to ask for confirmation before starting to download a file, defaults to True

  • data_dir (str or None) – directory where the .shp.zip data files are located/saved; if None, the default directory

  • 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

Examples:

>>> import os
>>> from pyhelpers.dir import cd, delete_dir
>>> from pydriosm.reader import GeofabrikReader

>>> geofabrik_reader = GeofabrikReader()

>>> # To merge 'railways' of Greater Manchester and West Yorkshire
>>> lyr_name = 'railways'
>>> sr_names = ['Manchester', 'West Yorkshire']
>>> dat_dir = "tests"

>>> path_to_merged_shp_file = geofabrik_reader.merge_subregion_layer_shp(
...     lyr_name, sr_names, dat_dir, verbose=True, ret_merged_shp_path=True)
Confirmed to download .shp.zip data of the following geographic region(s):
    Greater Manchester
    West Yorkshire
? [No]|Yes: yes
Downloading "greater-manchester-latest-free.shp.zip" to "\tests" ...
Done.
Downloading "west-yorkshire-latest-free.shp.zip" to "\tests" ...
Done.
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 ... file(s) at "\tests\greater-manchester_west-yorkshire_railways".

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

>>> # Delete the merged files
>>> delete_dir(os.path.dirname(path_to_merged_shp_file), 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 .shp.zip data files
>>> os.remove(cd(dat_dir, "greater-manchester-latest-free.shp.zip"))
>>> os.remove(cd(dat_dir, "west-yorkshire-latest-free.shp.zip"))

>>> # To merge 'transport' of Greater London, Kent and Surrey

>>> lyr_name = 'transport'
>>> sr_names = ['London', 'Kent', 'Surrey']

>>> path_to_merged_shp_files = geofabrik_reader.merge_subregion_layer_shp(
...     lyr_name, sr_names, dat_dir, verbose=True, ret_merged_shp_path=True)
Confirmed to download .shp.zip data of the following geographic region(s):
    Greater London
    Kent
    Surrey
? [No]|Yes: yes
Downloading "greater-london-latest-free.shp.zip" to "\tests" ...
Done.
Downloading "kent-latest-free.shp.zip" to "\tests" ...
Done.
Downloading "surrey-latest-free.shp.zip" to "\tests" ...
Done.
Extracting from "greater-london-latest-free.shp.zip" the following layer(s):
    'transport'
to "\tests\greater-london-latest-free-shp" ...
In progress ... Done.
Extracting from "kent-latest-free.shp.zip" the following layer(s):
    'transport'
to "\tests\kent-latest-free-shp" ...
In progress ... Done.
Extracting from "surrey-latest-free.shp.zip" the following layer(s):
    'transport'
to "\tests\surrey-latest-free-shp" ...
In progress ... Done.
Merging the following shapefiles:
    "greater-london_gis_osm_transport_a_free_1.shp"
    "greater-london_gis_osm_transport_free_1.shp"
    "kent_gis_osm_transport_a_free_1.shp"
    "kent_gis_osm_transport_free_1.shp"
    "surrey_gis_osm_transport_a_free_1.shp"
    "surrey_gis_osm_transport_free_1.shp"
In progress ... Done.
Find the merged .shp file(s) at "\tests\greater-london_kent_surrey_transport".

>>> for path_to_merged_shp_file in path_to_merged_shp_files:
...     print(os.path.relpath(path_to_merged_shp_file))
tests\...\greater-london_kent_surrey_transport_point.shp
tests\...\greater-london_kent_surrey_transport_polygon.shp

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

>>> # Delete the downloaded .shp.zip data files
>>> os.remove(cd(dat_dir, "greater-london-latest-free.shp.zip"))
>>> os.remove(cd(dat_dir, "kent-latest-free.shp.zip"))
>>> os.remove(cd(dat_dir, "surrey-latest-free.shp.zip"))