SHP.merge_layers¶
- classmethod SHP.merge_layers(shp_zip_pathnames, layer_name, engine='geopandas', rm_zip_extracts=True, output_dir=None, rm_shp_temp=True, ret_shp_pathname=False, verbose=False, raise_error=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'and'geopandas'(default) (or'gpd') ifengine='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
Falserm_shp_temp (bool) – whether to delete temporary layer files, defaults to
Falseoutput_dir (str | None) – if
None(default), use the layer name as the name of the folder where the merged .shp files will be savedret_shp_pathname (bool) – whether to return the pathname of the merged .shp file, defaults to
Falseverbose (bool | int) – whether to print relevant information in console, defaults to
Falseraise_error (bool) – Whether to raise the provided exception; if
raise_error=False(default), the error will be suppressed.
- 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 functionvalid_shapefile_layer_names().
Examples:
>>> # To merge 'railways' layers of Greater Manchester and West Yorkshire" >>> from pydriosm.reader._shp import SHP >>> from pydriosm.downloader import BBBikeDownloader >>> from pyhelpers.dirs import delete_dir >>> import os >>> # Download the .shp.zip file of Manchester and West Yorkshire >>> subrgn_names = ['London', 'Birmingham'] >>> file_fmt = ".shp" >>> data_dir = "tests/osm_data" >>> bbd = BBBikeDownloader() >>> bbd.download_data(subrgn_names, file_fmt, data_dir, verbose=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 | 18.1MB/s | ETA:... Saving "London.osm.shp.zip" to "./tests/osm_data/london/" ... Done. Downloading "Birmingham.osm.shp.zip" 100%|██████████| 79.1M/79.1M | 16.0MB/s ... Saving "Birmingham.osm.shp.zip" to "./tests/osm_data/birmingham/" ... Done. >>> os.path.relpath(bbd.download_dir) 'tests\osm_data' >>> len(bbd.data_paths) 2 >>> # Merge the layers of 'railways' of the two subregions >>> merged_shp_path = SHP.merge_shp_layers( ... bbd.data_paths, layer_name='railways', verbose=True, ret_shp_pathname=True) Merging the following shapefiles: "london_railways.shp" "birmingham_railways.shp" In progress ... Done. Find the merged shapefile in "./tests/osm_data/lon-bir-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\lon-bir-railways\lon-bir-railways.shp' >>> # Read the merged .shp file >>> merged_shp_data = SHP.read_shp(merged_shp_path[0]) >>> merged_shp_data.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 test data directory >>> delete_dir(bbd.download_dir, verbose=True) To delete the directory "./tests/osm_data/" (Not empty) ? [No]|Yes: yes Deleting "./tests/osm_data/" ... Done.
See also
Examples for the method
GeofabrikReader.merge_shp_layers().