GeofabrikReader.read_shp_zip

GeofabrikReader.read_shp_zip(subregion_name, layer_names=None, feature_names=None, data_dir=None, update=False, download_confirmation_required=True, pickle_it=False, ret_pickle_path=False, rm_extracts=False, rm_shp_zip=False, verbose=False)

Read a .shp.zip data file of a geographic region.

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

  • layer_names (str or list or None) – name of a .shp layer, e.g. ‘railways’, or names of multiple layers; if None (default), all available layers

  • feature_names (str or list or None) – name of a feature, e.g. ‘rail’, or names of multiple features; if None (default), all available features

  • data_dir (str or None) – directory where the .shp.zip data file is located/saved; if None, the default directory

  • update (bool) – whether to check to update pickle backup (if available), defaults to False

  • download_confirmation_required (bool) – whether to ask for confirmation before starting to download a file, defaults to True

  • pickle_it (bool) – whether to save the .shp data as a .pickle file, defaults to False

  • ret_pickle_path (bool) – (when pickle_it=True) whether to return a path to the saved pickle file

  • rm_extracts (bool) – whether to delete extracted files from the .shp.zip file, defaults to False

  • rm_shp_zip (bool) – whether to delete the downloaded .shp.zip file, defaults to False

  • verbose (bool or int) – whether to print relevant information in console as the function runs, defaults to False

Returns

dictionary of the shapefile data, with keys and values being layer names and tabular data (in the format of geopandas.GeoDataFrame), respectively

Return type

dict or None

Example:

>>> from pydriosm.reader import GeofabrikReader

>>> geofabrik_reader = GeofabrikReader()

>>> sr_name = 'Rutland'
>>> dat_dir = "tests"

>>> rutland_shp = geofabrik_reader.read_shp_zip(
...     subregion_name=sr_name, data_dir=dat_dir, verbose=True)
To download .shp.zip data of the following geographic region(s):
    Rutland
? [No]|Yes: yes
Downloading "rutland-latest-free.shp.zip" to "tests\" ... Done.
Extracting "tests\rutland-latest-free.shp.zip" ...
to "tests\rutland-latest-free-shp\"
Done.

>>> list(rutland_shp.keys())
['buildings',
 'traffic',
 'water',
 'roads',
 'places',
 'pofw',
 'waterways',
 'pois',
 'landuse',
 'transport',
 'natural',
 'railways']

>>> # Data of the 'railways' layer
>>> rutland_shp_railways = rutland_shp['railways']
>>> rutland_shp_railways.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]

>>> # Read data of the 'transport' layer only from the original .shp.zip file
>>> # (and delete any extracts)
>>> sr_layer = 'transport'

>>> rutland_shp_transport = geofabrik_reader.read_shp_zip(sr_name, sr_layer,
...                                                       data_dir=dat_dir,
...                                                       verbose=True,
...                                                       rm_extracts=True)
Deleting the extracts "tests\rutland-latest-free-shp\"  ... Done.

>>> list(rutland_shp_transport.keys())
['transport']

>>> rutland_shp_transport['transport'].head()
      osm_id  ...  shape_type
0  232038062  ...           5
1  468873547  ...           5
2  468873548  ...           5
3  468873553  ...           5
4  468873559  ...           5
[5 rows x 6 columns]

>>> # Read data of only the 'bus_stop' feature (in the 'transport' layer)
>>> # from the original .shp.zip file (and delete any extracts)
>>> feat_name = 'bus_stop'

>>> rutland_bus_stop = geofabrik_reader.read_shp_zip(sr_name, sr_layer, feat_name,
...                                                  dat_dir, verbose=True,
...                                                  rm_extracts=True)
Extracting the following layer(s):
    'transport'
from "tests\rutland-latest-free.shp.zip" ...
to "tests\rutland-latest-free-shp\"
Done.
Deleting the extracts "tests\rutland-latest-free-shp\"  ... Done.

>>> list(rutland_bus_stop.keys())
['transport']

>>> print(rutland_bus_stop['transport'].fclass.unique())
['bus_stop']

>>> # Read multiple features of multiple layers
>>> # (and delete both the original .shp.zip file and extracts)
>>> sr_layers = ['traffic', 'roads']
>>> feat_names = ['parking', 'trunk']

>>> rutland_shp_tr_pt = geofabrik_reader.read_shp_zip(sr_name, sr_layers, feat_name,
...                                                   dat_dir, verbose=True,
...                                                   rm_extracts=True,
...                                                   rm_shp_zip=True)
Extracting the following layer(s):
    'traffic'
    'roads'
from "tests\rutland-latest-free.shp.zip" ...
to "tests\rutland-latest-free-shp\"
Done.
Deleting the extracts "tests\rutland-latest-free-shp\"  ... Done.
Deleting "tests\rutland-latest-free.shp.zip" ... Done.

>>> list(rutland_shp_tr_pt.keys())
['traffic', 'roads']

>>> # Data of the 'traffic' layer
>>> rutland_shp_tr_pt_traffic = rutland_shp_tr_pt['traffic']
>>> rutland_shp_tr_pt_traffic.head()
         osm_id  code  ...                 coordinates shape_type
204    14558402  5202  ...  [[-0.7266581, 52.6695058]]          1
206    14583750  5202  ...  [[-0.4704691, 52.6548803]]          1
213    18335108  5202  ...  [[-0.7384552, 52.6674072]]          1
255   862120532  5202  ...  [[-0.7320612, 52.6688328]]          1
291  1584865939  5202  ...   [[-0.7391079, 52.674775]]          1
[5 rows x 6 columns]

>>> # Data of the 'roads' layer
>>> rutland_shp_tr_pt_roads = rutland_shp_tr_pt['roads']
>>> rutland_shp_tr_pt_roads.head()
         osm_id  ...  shape_type
1320   73599134  ...           3
1321   73599136  ...           3
1557  101044857  ...           3
1561  101044867  ...           3
1682  101326487  ...           3
[5 rows x 12 columns]