PostgresOSM.import_osm_data

PostgresOSM.import_osm_data(osm_data, table_name, schema_names=None, table_named_as_subregion=False, schema_named_as_layer=False, if_exists='replace', force_replace=False, chunk_size=None, confirmation_required=True, verbose=False, **kwargs)[source]

Import OSM data into a database.

Parameters
  • osm_data (dict) – OSM data of a geographic region

  • table_name (str) – name of a table

  • schema_names (list or None) – names of schemas for each layer of the PBF data, if None (default), use the default layer names as schema names

  • table_named_as_subregion (bool) – whether to use subregion name to be a table name, defaults to False

  • schema_named_as_layer (bool) – whether a schema is named as a layer name, defaults to False

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

  • force_replace (bool) – whether to force to replace existing table, defaults to False

  • chunk_size (int, None) – the number of rows in each batch to be written at a time, defaults to None

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

  • verbose (bool) – 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.dir import cd
>>> from pydriosm.ios import PostgresOSM

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

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

>>> rutland_pbf_raw = osmdb_test.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

>>> # Import all layers of the raw PBF data of Rutland

>>> osmdb_test.import_osm_data(rutland_pbf_raw, table_name=sr_name,
...                            verbose=True)
Confirmed to import the data into table "Rutland"
    at postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
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.

>>> rutland_pbf = osmdb_test.Reader.read_osm_pbf(sr_name, dat_dir,
...                                              parse_raw_feat=True,
...                                              transform_geom=True,
...                                              transform_other_tags=True)

>>> # Import data into specific schemas
>>> schemas = {'schema_0': 'lines',
...            'schema_1': 'points',
...            'schema_2': 'multipolygons'}

>>> osmdb_test.import_osm_data(rutland_pbf, table_name=sr_name,
...                            schema_names=schemas, verbose=True)
Confirmed to import the data into table "Rutland"
    at postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
Importing data into "Rutland" ...
    schema_0 ... done: <total of rows> features.
    schema_1 ... done: <total of rows> features.
    schema_2 ... done: <total of rows> features.

>>> # To drop the schemas starting with 'schema_'
>>> osmdb_test.PostgreSQL.drop_schema(schemas.keys(), verbose=True)
Confirmed to drop the following schemas:
    "schema_0",
    "schema_1" and
    "schema_2"
  from postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
Dropping ...
    "schema_0" ... Done.
    "schema_1" ... Done.
    "schema_2" ... Done.

>>> # Delete the downloaded PBF data file
>>> os.remove(cd(dat_dir, "rutland-latest.osm.pbf"))

>>> rutland_shp = osmdb_test.Reader.read_shp_zip(sr_name, data_dir=dat_dir,
...                                              rm_extracts=True,
...                                              rm_shp_zip=True,
...                                              verbose=True)
Confirmed 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 all of "rutland-latest-free.shp.zip" to "\tests\rutl-...-shp" ...
In progress ... Done.
Deleting the extracts "\tests\rutland-latest-free-shp"  ... Done.
Deleting "tests\rutland-latest-free.shp.zip" ... Done.

>>> # Import all layers of the shapefile data of Rutland

>>> osmdb_test.import_osm_data(rutland_shp, table_name=sr_name, verbose=True)
Confirmed to import the data into table "Rutland"
    at postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
Importing data into "Rutland" ...
    pofw ... done: <total of rows> features.
    pois ... done: <total of rows> features.
    traffic ... done: <total of rows> features.
    places ... done: <total of rows> features.
    waterways ... done: <total of rows> features.
    buildings ... done: <total of rows> features.
    transport ... done: <total of rows> features.
    natural ... done: <total of rows> features.
    roads ... done: <total of rows> features.
    water ... done: <total of rows> features.
    railways ... done: <total of rows> features.
    landuse ... done: <total of rows> features.

>>> # Import BBBike shapefile data

>>> osmdb_test.DataSource = 'BBBike'
>>> sr_name = 'Leeds'

>>> leeds_shp = osmdb_test.Reader.read_shp_zip(sr_name, data_dir=dat_dir,
...                                            rm_extracts=True,
...                                            rm_shp_zip=True, verbose=True)
Confirmed to download .shp.zip data of the following geographic region(s):
    Leeds
? [No]|Yes: yes
Downloading "Leeds.osm.shp.zip" to "\tests" ...
Done.
Extracting all of "Leeds.osm.shp.zip" to "\tests" ...
In progress ... Done.
Parsing "\tests\Leeds-shp\shape" ... Done.
Deleting the extracts "\tests\Leeds-shp" ... Done.
Deleting "tests\Leeds.osm.shp.zip" ... Done.

>>> osmdb_test.import_osm_data(leeds_shp, table_name=sr_name, verbose=True)
Confirmed to import the data into table "Leeds"
    at postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
Importing data into "Leeds" ...
    places ... done: <total of rows> features.
    waterways ... done: <total of rows> features.
    buildings ... done: <total of rows> features.
    natural ... done: <total of rows> features.
    roads ... done: <total of rows> features.
    railways ... done: <total of rows> features.
    points ... done: <total of rows> features.
    landuse ... done: <total of rows> features.