BBBikeReader.merge_shp_layers¶
- BBBikeReader.merge_shp_layers(subregion_names, layer_name, data_dir=None, engine='pyshp', update=False, download=False, 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') ifengine='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
Falsedownload (bool) – whether to ask for confirmation before starting to download a file, defaults to
Truedata_dir (str | 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
Falserm_shp_temp (bool) – whether to delete temporary layer files, defaults to
Falsemerged_shp_dir (str | None) – if
None(default), use the layer name as the name of the folder where the merged .shp files will be savedverbose (bool | int) – whether to print relevant information in console, defaults to
Falseret_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 BBBikeReader >>> from pyhelpers.dirs import cd, delete_dir >>> import os >>> bbr = BBBikeReader()
Example 1:
>>> # To merge 'railways' of London and Birmingham >>> subrgn_name = ['London', 'Birmingham'] >>> lyr_name = 'railways' >>> dat_dir = "tests/osm_data" >>> path_to_merged_shp_file = bbr.merge_shp_layers( ... subrgn_name, lyr_name, dat_dir, verbose=True, ret_merged_shp_path=True) Proceed to download data in the format '.shp.zip' for the following geographic (sub... "London" "Birmingham" to "./tests/osm_data/" ? [No]|Yes: yes Downloading "London.osm.shp.zip" 100%|██████████| 248M/248M | 16.5MB/s | ETA:... Saving "London.osm.shp.zip" to "./tests/osm_data/london/" ... Done. Downloading "Birmingham.osm.shp.zip" 100%|██████████| 79.1M/79.1M | 16.9MB/s ... Saving "Birmingham.osm.shp.zip" to "./tests/osm_data/birmingham/" ... Done. Merging the following shapefiles: "london_railways.shp" "birmingham_railways.shp" In progress ... Done. Find the merged shapefile in "./tests/osm_data/lon-bir-railways/". >>> path_to_merged_shp_file = path_to_merged_shp_file[0] >>> os.path.relpath(path_to_merged_shp_file) 'tests\osm_data\lon-bir-railways\lon-bir-railways.shp' >>> # Read the merged data >>> lon_bir_railways_shp = bbr.SHP.read_shp(path_to_merged_shp_file) >>> lon_bir_railways_shp.head() osm_id ... geometry 0 30804 ... LINESTRING (0.00486 51.62793, 0.0062 51.62927) 1 101298 ... LINESTRING (-0.22499 51.4937, -0.22516 51.4945... 2 101486 ... LINESTRING (-0.20555 51.51954, -0.20514 51.519... 3 101511 ... LINESTRING (-0.2119 51.52419, -0.21081 51.5239... 4 282898 ... LINESTRING (-0.1862 51.61592, -0.18687 51.61386) [5 rows x 4 columns] >>> # Delete the merged files >>> delete_dir(os.path.dirname(path_to_merged_shp_file), verbose=True) To delete the directory "./tests/osm_data/lon-bir-railways/" (Not empty) ? [No]|Yes: yes Deleting "./tests/osm_data/lon-bir-railways/" ... Done. >>> # Delete the downloaded .shp.zip data files >>> delete_dir(list(map(os.path.dirname, bbr.downloader.data_paths)), verbose=True) To delete the following directories: "./tests/osm_data/london/" (Not empty) "./tests/osm_data/birmingham/" (Not empty) ? [No]|Yes: yes Deleting: "./tests/osm_data/london/" ... Done. "./tests/osm_data/birmingham/" ... 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.