GeofabrikReader.read_osm_pbf

GeofabrikReader.read_osm_pbf(subregion_name, data_dir=None, chunk_size_limit=50, parse_raw_feat=False, transform_geom=False, transform_other_tags=False, update=False, download_confirmation_required=True, pickle_it=False, ret_pickle_path=False, rm_osm_pbf=False, verbose=False)[source]

Read a PBF (.osm.pbf) data file of a geographic region.

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

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

  • chunk_size_limit (int) – threshold (in MB) that triggers the use of chunk parser, defaults to 50; if the size of the .osm.pbf file (in MB) is greater than chunk_size_limit, it will be parsed in a chunk-wise way

  • parse_raw_feat (bool) – whether to parse each feature in the raw data, defaults to False

  • transform_geom (bool) – whether to transform a single coordinate (or a collection of coordinates) into a geometric object, defaults to False

  • transform_other_tags (bool) – whether to transform a 'other_tags' into a dictionary, defaults to False

  • 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 .pbf data as a .pickle file, defaults to False

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

  • rm_osm_pbf (bool) – whether to delete the downloaded .osm.pbf 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 .osm.pbf data; when pickle_it=True, return a tuple of the dictionary and an absolute path to the pickle file

Return type

dict or tuple or None

Examples:

>>> import os
>>> from pydriosm.reader import GeofabrikReader

>>> geofabrik_reader = GeofabrikReader()

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

>>> rutland_pbf_raw = geofabrik_reader.read_osm_pbf(sr_name, dat_dir,
...                                                 verbose=True)
Confirmed to download .osm.pbf data of the following geographic region(s):
    Rutland
? [No]|Yes: yes

>>> print(list(rutland_pbf_raw.keys()))
['points', 'lines', 'multilinestrings', 'multipolygons', 'other_relations']

>>> rutland_pbf_raw_points = rutland_pbf_raw['points']
>>> print(rutland_pbf_raw_points.head())
                                              points
0  {"type": "Feature", "geometry": {"type": "Poin...
1  {"type": "Feature", "geometry": {"type": "Poin...
2  {"type": "Feature", "geometry": {"type": "Poin...
3  {"type": "Feature", "geometry": {"type": "Poin...
4  {"type": "Feature", "geometry": {"type": "Poin...

>>> rutland_pbf_parsed = geofabrik_reader.read_osm_pbf(sr_name, dat_dir,
...                                                    parse_raw_feat=True,
...                                                    verbose=True)
Parsing "\tests\rutland-latest.osm.pbf" ... Done.

>>> rutland_pbf_parsed_points = rutland_pbf_parsed['points']
>>> print(rutland_pbf_parsed_points.head())
         id               coordinates  ...                    other_tags
0    488432  [-0.5134241, 52.6555853]  ...               "odbl"=>"clean"
1    488658  [-0.5313354, 52.6737716]  ...                          None
2  13883868  [-0.7229332, 52.5889864]  ...                          None
3  14049101  [-0.7249922, 52.6748223]  ...  "traffic_calming"=>"cushion"
4  14558402  [-0.7266686, 52.6695051]  ...      "direction"=>"clockwise"
[5 rows x 12 columns]

>>> rutland_pbf_parsed_1 = geofabrik_reader.read_osm_pbf(sr_name, dat_dir,
...                                                      parse_raw_feat=True,
...                                                      transform_geom=True,
...                                                      verbose=True)
Parsing "\tests\rutland-latest.osm.pbf" ... Done.

>>> rutland_pbf_parsed_1_points = rutland_pbf_parsed_1['points']
>>> print(rutland_pbf_parsed_1_points[['coordinates']].head())
                              coordinates
0           POINT (-0.5134241 52.6555853)
1           POINT (-0.5313354 52.6737716)
2  POINT (-0.7229332000000001 52.5889864)
3           POINT (-0.7249922 52.6748223)
4           POINT (-0.7266686 52.6695051)

>>> rutland_pbf_parsed_2 = geofabrik_reader.read_osm_pbf(
...     sr_name, dat_dir, parse_raw_feat=True, transform_geom=True,
...     transform_other_tags=True, verbose=True)

>>> rutland_pbf_parsed_2_points = rutland_pbf_parsed_2['points']
>>> print(rutland_pbf_parsed_2_points[['other_tags']].head())
                       other_tags
0               {'odbl': 'clean'}
1                            None
2                            None
3  {'traffic_calming': 'cushion'}
4      {'direction': 'clockwise'}

>>> # Delete the downloaded PBF data file
>>> os.remove(f"{dat_dir}\rutland-latest.osm.pbf")