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')

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

Parameters
  • subregion_name (str) – name of a geographic region (case-insensitive) that is available on Geofabrik 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, unzip_shp_zip, parse_layer_shp

>>> geofabrik_reader = GeofabrikReader()

>>> region_name = 'Rutland'
>>> file_format = ".shp"

>>> # (if "gis.osm_railways_free_1.shp" is unavailable)
>>> path_to_shp_file = geofabrik_reader.get_path_to_osm_shp(region_name)
>>> print(path_to_shp_file)
[]

>>> dwnld_dir = "tests"

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

>>> # Extract the downloaded .zip file
>>> unzip_shp_zip(path_to_rutland_shp_zip, verbose=True)
Extracting "tests\rutland-latest-free.shp.zip" ...
to "tests\rutland-latest-free-shp\"
Done.

>>> lyr_name = 'railways'

>>> # Get the file path of 'railways' shapefile
>>> path_to_rutland_railways_shp = geofabrik_reader.get_path_to_osm_shp(
...     region_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'

>>> # Get/save shapefile data of features labelled 'rail' only
>>> _ = parse_layer_shp(path_to_rutland_railways_shp, feature_names=feat_name,
...                     save_fclass_shp=True)

>>> # Get the file path to the data of 'rail'
>>> path_to_rutland_railways_rail_shp = geofabrik_reader.get_path_to_osm_shp(
...     region_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

>>> # Retrieve the data of 'rail' feature
>>> rutland_railways_rail_shp = parse_layer_shp(path_to_rutland_railways_rail_shp)

>>> rutland_railways_rail_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]

>>> # 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)