PostgresOSM.import_subregion_osm_pbf

PostgresOSM.import_subregion_osm_pbf(subregion_names, data_dir=None, update_osm_pbf=False, if_exists='replace', chunk_size_limit=50, parse_raw_feat=False, transform_geom=False, transform_other_tags=False, pickle_pbf_file=False, rm_osm_pbf=False, confirmation_required=True, verbose=False, **kwargs)[source]

Import data of geographic region(s) that do not have (sub-)subregions into a database.

Parameters
  • subregion_names (str or list or None) – name(s) of geographic region(s)

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

  • update_osm_pbf (bool) – whether to update .osm.pbf data file (if available), defaults to False

  • if_exists (str) – if the table already exists, to 'replace' (default), 'append' or 'fail'

  • 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

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

  • rm_osm_pbf (bool) – whether to delete the downloaded .osm.pbf file, defaults to False

  • confirmation_required (bool) – whether to prompt a message for confirmation to proceed, defaults to True

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

  • kwargs – optional parameters of .import_osm_pbf_layer()

Examples:

>>> import os
>>> from pyhelpers import cd, load_pickle
>>> from pydriosm.ios import PostgresOSM

>>> osmdb_test = PostgresOSM(database_name='osmdb_test')
Password (postgres@localhost:5432): ***
Connecting postgres:***@localhost:5432/osmdb_test ... Successfully.

>>> dat_dir = "tests"

>>> sr_name = 'Rutland'

>>> osmdb_test.import_subregion_osm_pbf(sr_name, dat_dir, rm_osm_pbf=True,
...                                     verbose=True)
Confirmed to import Geofabrik OSM data of the following geographic region(s):
    Rutland
  into postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
Downloading "rutland-latest.osm.pbf" to "\tests" ...
Done.
Importing data into "Rutland" ...
    points ... done: <total of rows> features.
    lines ... done: <total of rows> features.
    multilinestrings ... done: <total of rows> features.
    multipolygons ... done: <total of rows> features.
    other_relations ... done: <total of rows> features.
Deleting "tests\rutland-latest.osm.pbf" ... Done.

>>> # Import free BBBike PBF data of Victoria and Waterloo
>>> osmdb_test.DataSource = 'BBBike'
>>> sr_names = ['Victoria', 'Waterloo']

>>> # Note this may take a few minutes or even longer
>>> osmdb_test.import_subregion_osm_pbf(
...     sr_names, dat_dir, parse_raw_feat=True, transform_geom=True,
...     transform_other_tags=True, pickle_pbf_file=True, rm_osm_pbf=True,
...     verbose=True)
Confirmed to import BBBike OSM data of the following geographic region(s):
    Victoria,
    Waterloo
  into postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
Downloading "Victoria.osm.pbf" to "\tests" ...
Done.
Parsing "\tests\Victoria.osm.pbf" ... Done.
Importing data into "Victoria" ...
    points ... done: <total of rows> features.
    lines ... done: <total of rows> features.
    multilinestrings ... done: <total of rows> features.
    multipolygons ... done: <total of rows> features.
    other_relations ... done: <total of rows> features.
Saving "Victoria-pbf.pickle" to "\tests" ... Done.
Deleting "tests\Victoria.osm.pbf" ... Done.
Downloading "Waterloo.osm.pbf" to "\tests" ...
Done.
Parsing "\tests\Waterloo.osm.pbf" ... Done.
Importing data into "Waterloo" ...
    points ... done: <total of rows> features.
    lines ... done: <total of rows> features.
    multilinestrings ... done: <total of rows> features.
    multipolygons ... done: <total of rows> features.
    other_relations ... done: <total of rows> features.
Saving "Waterloo-pbf.pickle" to "\tests" ... Done.
Deleting "tests\Waterloo.osm.pbf" ... Done.

>>> # The PBF data have also been saved as Pickle files
>>> victoria_pbf = load_pickle(cd(dat_dir, "Victoria-pbf.pickle"))
>>> print(victoria_pbf['points'].head())
         id                      coordinates  ... man_made other_tags
0  25832817  POINT (-123.3102145 48.4351935)  ...     None       None
1  25832953  POINT (-123.3157486 48.4309841)  ...     None       None
2  25832954  POINT (-123.3209612 48.4323984)  ...     None       None
3  25832995  POINT (-123.3224238 48.4321706)  ...     None       None
4  25833001  POINT (-123.3202181 48.4297225)  ...     None       None
[5 rows x 12 columns]

>>> waterloo_pbf = load_pickle(cd(dat_dir, "Waterloo-pbf.pickle"))
>>> print(waterloo_pbf['points'].head())
         id  ...                                 other_tags
0  10782939  ...                                       None
1  10782965  ...                                       None
2  14509209  ...                                       None
3  14657092  ...  {'traffic_signals:direction': 'backward'}
4  14657140  ...                                       None
[5 rows x 12 columns]

>>> # Delete the Pickle files
>>> os.remove(cd(dat_dir, "Victoria-pbf.pickle"))
>>> os.remove(cd(dat_dir, "Waterloo-pbf.pickle"))