GeofabrikReader.merge_subregion_layer_shp

GeofabrikReader.merge_subregion_layer_shp(subregion_names, layer_name, data_dir=None, engine='pyshp', update=False, download=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) – names of geographic region (case-insensitive) that is available on Geofabrik free download server

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

  • engine (str) – the method 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()

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

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

  • data_dir (str | None) – directory where the .shp.zip data files are located/saved; if None (default), 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 | None) – if None (default), use the layer name as the name of the folder where the merged .shp files will be saved

  • verbose (bool | int) – whether to print relevant information in console, 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 | str

Examples:

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

>>> gfr = GeofabrikReader()

Example 1:

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

>>> path_to_merged_shp_file = gfr.merge_subregion_layer_shp(
...     subrgn_name, lyr_name, dat_dir, verbose=True, ret_merged_shp_path=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.
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\".

>>> os.path.relpath(path_to_merged_shp_file)
'tests\osm_data\gre_man-wes_yor-railways\linestring.shp'

>>> # Read the merged data
>>> manchester_yorkshire_railways_shp = gfr.SHP.read_shp(path_to_merged_shp_file)
>>> manchester_yorkshire_railways_shp.head()
    osm_id  code  ...                                        coordinates shape_type
0   928999  6101  ...  [(-2.2844621, 53.4802635), (-2.2949851, 53.481...          3
1   929904  6101  ...  [(-2.2917977, 53.4619559), (-2.2924877, 53.461...          3
2   929905  6102  ...  [(-2.2794048, 53.4605819), (-2.2799722, 53.460...          3
3  3663332  6102  ...  [(-2.2382139, 53.4817985), (-2.2381708, 53.481...          3
4  3996086  6101  ...  [(-2.6003053, 53.4604346), (-2.6005261, 53.460...          3
[5 rows x 9 columns]

>>> # Delete the merged files
>>> delete_dir(os.path.dirname(path_to_merged_shp_file), verbose=True)
To delete the directory "tests\osm_data\gre_man-wes_yor-railways\" (Not empty)
? [No]|Yes: yes
Deleting "tests\osm_data\gre_man-wes_yor-railways\" ... Done.

>>> # Delete the downloaded .shp.zip data files
>>> delete_dir(list(map(os.path.dirname, gfr.downloader.data_paths)), verbose=True)
To delete the following directories:
    "tests\osm_data\greater-manchester\" (Not empty)
    "tests\osm_data\west-yorkshire\" (Not empty)
? [No]|Yes: yes
Deleting "tests\osm_data\greater-manchester\" ... Done.
Deleting "tests\osm_data\west-yorkshire\" ... Done.

Example 2:

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

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

>>> path_to_merged_shp_file = gfr.merge_subregion_layer_shp(
...     subrgn_name, lyr_name, dat_dir, verbose=True, ret_merged_shp_path=True)
To download .shp.zip data of the following geographic (sub)region(s):
    Greater London
    Kent
    Surrey
? [No]|Yes: yes
Downloading "greater-london-latest-free.shp.zip"
    to "tests\osm_data\greater-london\" ... Done.
Downloading "kent-latest-free.shp.zip"
    to "tests\osm_data\kent\" ... Done.
Downloading "surrey-latest-free.shp.zip"
    to "tests\osm_data\surrey\" ... 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 shapefile at "tests\osm_data\gre_lon-ken-sur-transport\".

>>> type(path_to_merged_shp_file)
list
>>> len(path_to_merged_shp_file)
2
>>> os.path.relpath(path_to_merged_shp_file[0])
'tests\osm_data\gre-lon_ken_sur_transport\point.shp'
>>> os.path.relpath(path_to_merged_shp_file[1])
'tests\osm_data\gre-lon_ken_sur_transport\polygon.shp'

>>> # Read the merged shapefile
>>> merged_transport_shp_1 = gfr.SHP.read_shp(path_to_merged_shp_file[1])
>>> merged_transport_shp_1.head()
     osm_id  ...  shape_type
0   5077928  ...           5
1   8610280  ...           5
2  15705264  ...           5
3  23077379  ...           5
4  24016945  ...           5
[5 rows x 6 columns]

>>> # Delete the merged files
>>> delete_dir(os.path.commonpath(path_to_merged_shp_file), verbose=True)
To delete the directory "tests\osm_data\gre_lon-ken-sur-transport\" (Not empty)
? [No]|Yes: yes
Deleting "tests\osm_data\gre_lon-ken-sur-transport\" ... Done.

>>> # Delete the example data and the test data directory
>>> delete_dir(dat_dir, verbose=True)
To delete the directory "tests\osm_data\" (Not empty)
? [No]|Yes: yes
Deleting "tests\osm_data\" ... Done.