GeofabrikReader.merge_subregion_layer_shp¶
-
GeofabrikReader.
merge_subregion_layer_shp
(subregion_names, layer_name, data_dir=None, method='pyshp', 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)¶ 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’)
method (str) – the method used to merge/save shapefiles; options include:
'pyshp'
(default) and'geopandas'
(or'gpd'
) ifmethod='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_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
(default), the default directoryrm_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 savedverbose (bool or 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 or str
Examples:
>>> import os >>> from pyhelpers.dir import cd, delete_dir >>> from pydriosm.reader import GeofabrikReader, read_shp_file >>> geofabrik_reader = GeofabrikReader() >>> # -- Example 1 --------------------------------------------------------------- >>> # To merge 'railways' of Greater Manchester and West Yorkshire >>> sr_names = ['Manchester', 'West Yorkshire'] >>> lyr_name = 'railways' >>> dat_dir = "tests" >>> path_to_merged_shp_file = geofabrik_reader.merge_subregion_layer_shp( ... sr_names, lyr_name, dat_dir, verbose=True, ret_merged_shp_path=True) Downloading "greater-manchester-latest-free.shp.zip" to "tests\" ... Done. Downloading "west-yorkshire-latest-free.shp.zip" to "tests\" ... Done. 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(path_to_merged_shp_file)) tests\...\greater-manchester_west-yorkshire_railways.shp >>> # Read the merged data >>> manchester_yorkshire_railways_shp = read_shp_file(path_to_merged_shp_file) >>> manchester_yorkshire_railways_shp.head() osm_id code ... coordinates shape_type 0 928999 6101 ... [(-2.2844594, 53.4802681), (-2.2851997, 53.480... 3 1 929904 6101 ... [(-2.2919566, 53.4619298), (-2.2924877, 53.461... 3 2 929905 6102 ... [(-2.2794048, 53.4605819), (-2.2799773, 53.460... 3 3 3663332 6102 ... [(-2.2382517, 53.4818141), (-2.2381708, 53.481... 3 4 3996086 6101 ... [(-2.6003908, 53.4602313), (-2.6009371, 53.459... 3 [5 rows x 9 columns] >>> # 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")) >>> # -- Example 2 --------------------------------------------------------------- >>> # To merge 'transport' of Greater London, Kent and Surrey >>> sr_names = ['London', 'Kent', 'Surrey'] >>> lyr_name = 'transport' >>> path_to_merged_shp_file = geofabrik_reader.merge_subregion_layer_shp( ... sr_names, lyr_name, dat_dir, verbose=True, ret_merged_shp_path=True) 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 the following layer(s): 'transport' from "tests\greater-london-latest-free.shp.zip" ... to "tests\greater-london-latest-free-shp\" Done. Extracting the following layer(s): 'transport' from "tests\kent-latest-free.shp.zip" ... to "tests\kent-latest-free-shp\" Done. Extracting the following layer(s): 'transport' from "tests\surrey-latest-free.shp.zip" ... to "tests\surrey-latest-free-shp\" 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\". >>> print(os.path.relpath(path_to_merged_shp_file)) tests\...\greater-london_kent_surrey_transport.shp >>> # Read the merged shapefile >>> merged_transport_shp = read_shp_file(path_to_merged_shp_file) >>> merged_transport_shp.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.dirname(path_to_merged_shp_file), 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"))