SHP.read_layer_shps¶
- classmethod SHP.read_layer_shps(shp_pathnames, feature_names=None, save_feat_shp=False, ret_feat_shp_path=False, **kwargs)[source]¶
Read a layer of OSM shapefile data.
- Parameters:
shp_pathnames (str | list) – pathname of a .shp file, or pathnames of multiple shapefiles
feature_names (str | list | None) – class name(s) of feature(s), defaults to
Nonesave_feat_shp (bool) – (when
fclassis notNone) whether to save data of thefclassas shapefile, defaults toFalseret_feat_shp_path (bool) – (when
save_fclass_shp=True) whether to return the path to the saved data offclass, defaults toFalsekwargs – [optional] parameters of the method
SHP.read_shp()
- Returns:
parsed shapefile data; and optionally, pathnames of the shapefiles of the specified features (when
ret_feat_shp_path=True)- Return type:
pandas.DataFrame | geopandas.GeoDataFrame | tuple
Examples:
>>> from pydriosm.reader._shp import SHP >>> from pydriosm.downloader import BBBikeDownloader >>> from pyhelpers.dirs import cd, delete_dir >>> import os >>> # Download the shapefile data of London as an example >>> subrgn_name = 'Birmingham' >>> file_format = ".shp" >>> dwnld_dir = "tests/osm_data" >>> bbd = BBBikeDownloader() >>> bbd.download_data(subrgn_name, file_format, dwnld_dir, verbose=True) Proceed to download data in the format '.shp.zip' for the following geographic (sub... "Birmingham" to "./tests/osm_data/birmingham/" ? [No]|Yes: >? yes Downloading "Birmingham.osm.shp.zip" 100%|██████████| 79.1M/79.1M | 17.7MB/s ... Saving "Birmingham.osm.shp.zip" to "./tests/osm_data/birmingham/" ... Done. >>> bham_shp_zip = bbd.data_paths[0] >>> os.path.relpath(bham_shp_zip) 'tests\osm_data\birmingham\Birmingham.osm.shp.zip' >>> # Extract the downloaded .shp.zip file >>> bham_shp_dir = SHP.unzip_shp_zip( ... bham_shp_zip, layer_names='railways', ret_extract_dir=True) >>> os.listdir(cd(bham_shp_dir, "Birmingham-shp/shape")) ['railways.cpg', 'railways.dbf', 'railways.prj', 'railways.shp', 'railways.shx'] >>> bham_railways_shp_path = cd(bham_shp_dir, "Birmingham-shp/shape", "railways.shp") >>> # Read the 'railways' layer >>> bham_railways_shp = SHP.read_layer_shps(bham_railways_shp_path) >>> bham_railways_shp.head() osm_id ... geometry 0 740 ... LINESTRING (-1.81789 52.5701, -1.81793 52.5698... 1 2148 ... LINESTRING (-1.87303 52.50542, -1.8727 52.5051... 2 2950000 ... LINESTRING (-1.87933 52.48138, -1.87962 52.481... 3 3491845 ... LINESTRING (-1.7406 52.51858, -1.73942 52.5186... 4 3981454 ... LINESTRING (-1.77412 52.52249, -1.77376 52.522... [5 rows x 4 columns] >>> # Extract only the features labelled 'rail' and save the extracted data to file >>> railways_rail_shp, railways_rail_shp_path = SHP.read_layer_shps( ... bham_railways_shp_path, feature_names='rail', save_feat_shp=True, ... ret_feat_shp_path=True) >>> railways_rail_shp['type'].unique() <StringArray> ['rail'] Length: 1, dtype: str >>> type(railways_rail_shp_path) list >>> len(railways_rail_shp_path) 1 >>> os.path.basename(railways_rail_shp_path[0]) 'railways_rail.shp' >>> # Delete the download/data directory >>> delete_dir(dwnld_dir, verbose=True) To delete the directory "./tests/osm_data/" (Not empty) ? [No]|Yes: yes Deleting "./tests/osm_data/" ... Done.