parse_layer_shp

pydriosm.reader.parse_layer_shp(path_to_layer_shp, feature_names=None, crs=None, save_fclass_shp=False, driver='ESRI Shapefile', ret_path_to_fclass_shp=False, **kwargs)

Parse a layer of OSM shapefile data.

Parameters
  • path_to_layer_shp (str or list) – path(s) to one (or multiple) shapefile(s)

  • feature_names (str or list or None) – class name(s) of feature(s), defaults to None

  • crs (dict) – specification of coordinate reference system; if None (default), check specify_shp_crs()

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

  • driver (str) – the OGR format driver, defaults to 'ESRI Shapefile'; see also the driver parameter of geopandas.GeoDataFrame.to_file()

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

  • kwargs – optional parameters of read_shp_file()

Returns

parsed shapefile data

Return type

geopandas.GeoDataFrame

Examples:

>>> import os
>>> from pyhelpers.dir import cd, delete_dir
>>> from pydriosm.downloader import GeofabrikDownloader
>>> from pydriosm.reader import parse_layer_shp, unzip_shp_zip

>>> # Download the .shp.zip file of Rutland as an example
>>> geofabrik_downloader = GeofabrikDownloader()

>>> sr_name = 'Rutland'

>>> path_to_rutland_shp_zip = geofabrik_downloader.download_osm_data(
...     sr_name, osm_file_format=".shp", download_dir="tests",
...     confirmation_required=False, ret_download_path=True)

>>> # Extract the downloaded .shp.zip file
>>> rutland_shp_dir = unzip_shp_zip(path_to_rutland_shp_zip, ret_extract_dir=True)
>>> path_to_railways_shp = cd(rutland_shp_dir, "gis_osm_railways_free_1.shp")

>>> # Parse the 'railways' layer
>>> rutland_railways_shp = parse_layer_shp(path_to_railways_shp)

>>> rutland_railways_shp.head()
    osm_id  code  ...                                        coordinates shape_type
0  2162114  6101  ...  [(-0.4528083, 52.6993402), (-0.4518933, 52.698...          3
1  3681043  6101  ...  [(-0.6531215, 52.5730787), (-0.6531793, 52.572...          3
2  3693985  6101  ...  [(-0.7323403, 52.6782102), (-0.7319059, 52.678...          3
3  3693986  6101  ...  [(-0.6173072, 52.6132317), (-0.6241869, 52.614...          3
4  4806329  6101  ...  [(-0.4576926, 52.7035194), (-0.4565358, 52.702...          3
[5 rows x 9 columns]

>>> rutland_railways_rail, path_to_rutland_railways_rail = parse_layer_shp(
...     path_to_railways_shp, feature_names='rail', save_fclass_shp=True,
...     ret_path_to_fclass_shp=True)

>>> rutland_railways_rail.head()
    osm_id  code  ...                                        coordinates shape_type
0  2162114  6101  ...  [(-0.4528083, 52.6993402), (-0.4518933, 52.698...          3
1  3681043  6101  ...  [(-0.6531215, 52.5730787), (-0.6531793, 52.572...          3
2  3693985  6101  ...  [(-0.7323403, 52.6782102), (-0.7319059, 52.678...          3
3  3693986  6101  ...  [(-0.6173072, 52.6132317), (-0.6241869, 52.614...          3
4  4806329  6101  ...  [(-0.4576926, 52.7035194), (-0.4565358, 52.702...          3
[5 rows x 9 columns]

>>> print(os.path.relpath(path_to_rutland_railways_rail))
tests\rutland-latest-free-shp\railways\gis_osm_railways_free_1_rail.shp

>>> # Delete the extracted data files
>>> delete_dir(rutland_shp_dir, verbose=True)
The directory "tests\rutland-latest-free-shp\" is not empty.
Confirmed to delete it? [No]|Yes: yes
Deleting "tests\rutland-latest-free-shp\" ... Done.

>>> # Delete the downloaded shapefile
>>> os.remove(path_to_rutland_shp_zip)