GeofabrikReader.get_path_to_osm_shp

GeofabrikReader.get_path_to_osm_shp(subregion_name, layer_name=None, feature_name=None, data_dir=None, file_ext='.shp')[source]

Get the absolute path(s) to .shp file(s) for a geographic region (by searching a local data directory).

Parameters
  • subregion_name (str) – name of a region/subregion (case-insensitive) available on Geofabrik’s free download server

  • layer_name (str or None) – name of a .shp layer (e.g. 'railways'), defaults to None

  • feature_name (str or None) – name of a feature (e.g. 'rail'); if None (default), all available features included

  • data_dir (str or None) – directory where the search is conducted; if None (default), the default directory

  • file_ext (str) – file extension, defaults to ".shp"

Returns

path(s) to .shp file(s)

Return type

list or str

Examples:

>>> import os
>>> from pyhelpers.dir import delete_dir
>>> from pydriosm.reader import GeofabrikReader
>>> from pydriosm.reader import unzip_shp_zip, parse_layer_shp

>>> geofabrik_reader = GeofabrikReader()

>>> sr_name = 'Rutland'
>>> file_fmt = ".shp"

>>> path_to_shp_file = geofabrik_reader.get_path_to_osm_shp(sr_name)
>>> print(path_to_shp_file)
# (if "gis.osm_railways_free_1.shp" is unavailable at the package data directory)
[]

>>> dwnld_dir = "tests"

>>> # Download the shapefiles of Rutland
>>> path_to_rutland_shp_zip = geofabrik_reader.Downloader.download_osm_data(
...     sr_name, file_fmt, dwnld_dir, confirmation_required=False,
...     ret_download_path=True)

>>> unzip_shp_zip(path_to_rutland_shp_zip, verbose=True)
Extracting all ... to "\tests\rutland-latest-free-shp" ...
In progress ... Done.

>>> lyr_name = 'railways'

>>> path_to_rutland_railways_shp = geofabrik_reader.get_path_to_osm_shp(
...     sr_name, lyr_name, data_dir=dwnld_dir)

>>> print(os.path.relpath(path_to_rutland_railways_shp))
tests\rutland-latest-free-shp\gis_osm_railways_free_1.shp

>>> feat_name = 'rail'

>>> _ = parse_layer_shp(path_to_rutland_railways_shp, feature_names=feat_name,
...                     save_fclass_shp=True)

>>> path_to_rutland_railways_rail_shp = geofabrik_reader.get_path_to_osm_shp(
...     sr_name, lyr_name, feat_name, data_dir=dwnld_dir)

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

>>> # Delete the extracted files
>>> delete_dir(os.path.dirname(path_to_rutland_railways_shp), 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 .shp.zip file
>>> os.remove(path_to_rutland_shp_zip)