GeofabrikReader.get_shp_pathname

GeofabrikReader.get_shp_pathname(subregion_name, layer_name=None, feature_name=None, data_dir=None)[source]

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

Parameters:
  • subregion_name (str) – name of a geographic (sub)region (case-insensitive) that is available on Geofabrik free download server

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

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

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

Returns:

path(s) to .shp file(s)

Return type:

list

Examples:

>>> from pydriosm.reader import GeofabrikReader
>>> from pyhelpers.dirs import delete_dir
>>> import os

>>> gfr = GeofabrikReader()

>>> subrgn_name = 'london'
>>> file_format = ".shp"
>>> dat_dir = "tests\osm_data"

>>> # Try to get the shapefiles' pathnames
>>> london_shp_path = gfr.get_shp_pathname(subrgn_name, data_dir=dat_dir)
>>> london_shp_path  # An empty list if no data is available
[]

>>> # Download the shapefiles of London
>>> path_to_london_shp_zip = gfr.downloader.download_osm_data(
...     subrgn_name, file_format, dat_dir, verbose=True, ret_download_path=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.

>>> type(path_to_london_shp_zip)
list
>>> len(path_to_london_shp_zip)
1

>>> # Extract the downloaded .zip file
>>> gfr.SHP.unzip_shp_zip(path_to_london_shp_zip[0], verbose=True)
Extracting "tests\osm_data\greater-london\greater-london-latest-free.shp.zip"
    to "tests\osm_data\greater-london\greater-london-latest-free-shp\" ... Done.

>>> # Try again to get the shapefiles' pathnames
>>> london_shp_path = gfr.get_shp_pathname(subrgn_name, data_dir=dat_dir)
>>> len(london_shp_path) > 1
True

>>> # Get the file path of 'railways' shapefile
>>> lyr_name = 'railways'
>>> railways_shp_path = gfr.get_shp_pathname(subrgn_name, lyr_name, data_dir=dat_dir)
>>> len(railways_shp_path)
1
>>> railways_shp_path = railways_shp_path[0]
>>> os.path.relpath(railways_shp_path)
'tests\osm_data\greater-london\greater-london-latest-free-shp\gis_osm_railways_fr...

>>> # Get/save shapefile data of features labelled 'rail' only
>>> feat_name = 'rail'
>>> railways_shp = gfr.SHP.read_layer_shps(
...     railways_shp_path, feature_names=feat_name, save_feat_shp=True)
>>> railways_shp.head()
    osm_id  code  ...                                        coordinates shape_type
0    30804  6101  ...  [(0.0048644, 51.6279262), (0.0061979, 51.62926...          3
3   101511  6101  ...  [(-0.2119027, 51.5241906), (-0.2108059, 51.523...          3
5   361978  6101  ...  [(-0.0298545, 51.6619398), (-0.0302322, 51.659...          3
6  2370155  6101  ...  [(-0.3379005, 51.5937776), (-0.3367807, 51.593...          3
7  2526598  6101  ...  [(-0.1886021, 51.3602632), (-0.1884216, 51.360...          3
[5 rows x 9 columns]

>>> # Get the file path to the data of 'rail'
>>> rail_shp_path = gfr.get_shp_pathname(subrgn_name, lyr_name, feat_name, dat_dir)
>>> len(rail_shp_path)
1
>>> rail_shp_path = rail_shp_path[0]
>>> os.path.relpath(rail_shp_path)
'tests\osm_data\greater-london\greater-london-latest-free-shp\railways\rail.shp'

>>> # Retrieve the data of 'rail' feature
>>> railways_rail_shp = gfr.SHP.read_layer_shps(rail_shp_path)
>>> railways_rail_shp.head()
    osm_id  code  ...                                        coordinates shape_type
0    30804  6101  ...  [(0.0048644, 51.6279262), (0.0061979, 51.62926...          3
1   101511  6101  ...  [(-0.2119027, 51.5241906), (-0.2108059, 51.523...          3
2   361978  6101  ...  [(-0.0298545, 51.6619398), (-0.0302322, 51.659...          3
3  2370155  6101  ...  [(-0.3379005, 51.5937776), (-0.3367807, 51.593...          3
4  2526598  6101  ...  [(-0.1886021, 51.3602632), (-0.1884216, 51.360...          3
[5 rows x 9 columns]

>>> # Delete the example data and the test data directory
>>> delete_dir(dat_dir, verbose=True)
To delete the directory "tests\osm_data\" (Not empty)
? [No]|Yes: yes
Deleting "tests\osm_data\" ... Done.