SHPReadParse.read_layer_shps

classmethod SHPReadParse.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 None

  • save_feat_shp (bool) – (when fclass is not None) whether to save data of the fclass as shapefile, defaults to False

  • ret_feat_shp_path (bool) – (when save_fclass_shp=True) whether to return the path to the saved data of fclass, defaults to False

  • kwargs – [optional] parameters of the method SHPReadParse.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 import SHPReadParse
>>> from pydriosm.downloader import GeofabrikDownloader
>>> from pyhelpers.dirs import cd, delete_dir
>>> import os

>>> # Download the shapefile data of London as an example
>>> subrgn_name = 'london'
>>> file_format = ".shp"
>>> dwnld_dir = "tests\osm_data"

>>> gfd = GeofabrikDownloader()

>>> gfd.download_osm_data(subrgn_name, file_format, dwnld_dir, verbose=True)
To download .shp.zip data of the following geographic (sub)region(s):
    Greater London
? [No]|Yes: yes
Downloading "greater-london-latest-free.shp.zip"
    to "tests\osm_data\greater-london\" ... Done.

>>> london_shp_zip = gfd.data_paths[0]
>>> os.path.relpath(london_shp_zip)
'tests\osm_data\greater-london\greater-london-latest-free.shp.zip'

>>> # Extract the downloaded .shp.zip file
>>> london_shp_dir = SHPReadParse.unzip_shp_zip(
...     london_shp_zip, layer_names='railways', ret_extract_dir=True)
>>> os.listdir(london_shp_dir)
['gis_osm_railways_free_1.cpg',
 'gis_osm_railways_free_1.dbf',
 'gis_osm_railways_free_1.prj',
 'gis_osm_railways_free_1.shp',
 'gis_osm_railways_free_1.shx']
>>> london_railways_shp_path = cd(london_shp_dir, "gis_osm_railways_free_1.shp")

>>> # Read the 'railways' layer
>>> london_railways_shp = SHPReadParse.read_layer_shps(london_railways_shp_path)
>>> london_railways_shp.head()
   osm_id  code  ...                                        coordinates shape_type
0   30804  6101  ...  [(0.0048644, 51.6279262), (0.0061979, 51.62926...          3
1  101298  6103  ...  [(-0.2249906, 51.493682), (-0.2251678, 51.4945...          3
2  101486  6103  ...  [(-0.2055497, 51.5195429), (-0.2051377, 51.519...          3
3  101511  6101  ...  [(-0.2119027, 51.5241906), (-0.2108059, 51.523...          3
4  282898  6103  ...  [(-0.1862586, 51.6159083), (-0.1868721, 51.613...          3
[5 rows x 9 columns]

>>> # Extract only the features labelled 'rail' and save the extracted data to file
>>> railways_rail_shp, railways_rail_shp_path = SHPReadParse.read_layer_shps(
...     london_railways_shp_path, feature_names='rail', save_feat_shp=True,
...     ret_feat_shp_path=True)
>>> railways_rail_shp['fclass'].unique()
array(['rail'], dtype=object)

>>> type(railways_rail_shp_path)
list
>>> len(railways_rail_shp_path)
1
>>> os.path.basename(railways_rail_shp_path[0])
'gis_osm_railways_free_1_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.