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='fail', 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 (sub)region

  • table_name (str) – name of a table

  • schema_names (list | dict | None) – names of schemas for each layer of the PBF data, defaults to None; when schema_names=None, the default layer names as schema names

  • table_named_as_subregion (bool) – whether to use subregion name as 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, defaults to 'fail'; valid options include {'replace', 'append', '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 the method import_osm_layer()

Examples:

>>> from pydriosm.ios import PostgresOSM
>>> from pyhelpers.dirs import delete_dir

>>> osmdb = PostgresOSM(database_name='osmdb_test')
Password (postgres@localhost:5432): ***
Creating a database: "osmdb_test" ... Done.
Connecting postgres:***@localhost:5432/osmdb_test ... Successfully.

>>> subrgn_name = 'Rutland'  # name of a subregion
>>> dat_dir = "tests\osm_data"  # name of a data directory where the subregion data is

Example 1 - Import data of a PBF file:

>>> # First, read the PBF data of Rutland
>>> # (If the data file is not available, it'll be downloaded by confirmation)
>>> raw_rutland_pbf = osmdb.reader.read_osm_pbf(subrgn_name, dat_dir, verbose=True)
Downloading "rutland-latest.osm.pbf"
    to "tests\osm_data\rutland\" ... Done.
Reading "tests\osm_data\rutland\rutland-latest.osm.pbf" ... Done.
>>> type(raw_rutland_pbf)
dict
>>> list(raw_rutland_pbf.keys())
['points', 'lines', 'multilinestrings', 'multipolygons', 'other_relations']

>>> # Import all layers of the raw PBF data of Rutland
>>> osmdb.import_osm_data(raw_rutland_pbf, table_name=subrgn_name, verbose=True)
To import data into table "Rutland" at postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
Importing the data ...
    "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)

>>> # Get parsed PBF data
>>> parsed_rutland_pbf = osmdb.reader.read_osm_pbf(
...     subregion_name=subrgn_name, data_dir=dat_dir, expand=True, parse_geometry=True,
...     parse_other_tags=True, verbose=True)
Parsing "tests\osm_data\rutland\rutland-latest.osm.pbf" ... Done.
>>> type(parsed_rutland_pbf)
dict
>>> list(parsed_rutland_pbf.keys())
['points', 'lines', 'multilinestrings', 'multipolygons', 'other_relations']

>>> # Import data of selected layers into specific schemas
>>> schemas = {
...     "schema_0": 'lines',
...     "schema_1": 'points',
...     "schema_2": 'multipolygons',
... }
>>> osmdb.import_osm_data(parsed_rutland_pbf, subrgn_name, schemas, verbose=True)
To import data into table "Rutland" at postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
Importing the data ...
    "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 "schema_0", "schema_1" and "schema_2"
>>> osmdb.drop_schema(schemas.keys(), confirmation_required=False, verbose=True)
Dropping the following schemas from postgres:***@localhost:5432/osmdb_test:
    "schema_0" ... Done.
    "schema_1" ... Done.
    "schema_2" ... Done.

Example 2 - Import data of a shapefile:

>>> # Read shapefile data of Rutland
>>> rutland_shp = osmdb.reader.read_shp_zip(
...     subregion_name=subrgn_name, data_dir=dat_dir, rm_extracts=True, verbose=True)
Downloading "rutland-latest-free.shp.zip"
    to "tests\osm_data\rutland\" ... Done.
Extracting "tests\osm_data\rutland\rutland-latest-free.shp.zip"
    to "tests\osm_data\rutland\rutland-latest-free-shp\" ... Done.
Reading the shapefile(s) at
    "tests\osm_data\rutland\rutland-latest-free-shp\" ... Done.
Deleting the extracts "tests\osm_data\rutland\rutland-latest-free-shp\" ... Done.
>>> type(rutland_shp)
collections.OrderedDict
>>> list(rutland_shp.keys())
['buildings',
 'landuse',
 'natural',
 'places',
 'pofw',
 'pois',
 'railways',
 'roads',
 'traffic',
 'transport',
 'water',
 'waterways']

>>> # Import all layers of the shapefile data of Rutland
>>> osmdb.import_osm_data(osm_data=rutland_shp, table_name=subrgn_name, verbose=True)
To import data into table "Rutland" at postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
Importing the data ...
    "buildings" ... Done. (<total of rows> features)
    "landuse" ... Done. (<total of rows> features)
    "natural" ... Done. (<total of rows> features)
    "places" ... Done. (<total of rows> features)
    "pofw" ... Done. (<total of rows> features)
    "pois" ... Done. (<total of rows> features)
    "railways" ... Done. (<total of rows> features)
    "roads" ... Done. (<total of rows> features)
    "traffic" ... Done. (<total of rows> features)
    "transport" ... Done. (<total of rows> features)
    "water" ... Done. (<total of rows> features)
    "waterways" ... Done. (<total of rows> features)

Example 3 - Import BBBike shapefile data file of Leeds:

>>> # Change the data source
>>> osmdb.data_source = 'BBBike'
>>> subrgn_name = 'Leeds'

>>> # Read shapefile data of Leeds
>>> leeds_shp = osmdb.reader.read_shp_zip(
...     subregion_name=subrgn_name, data_dir=dat_dir, rm_extracts=True, verbose=True)
Downloading "Leeds.osm.shp.zip"
    to "tests\osm_data\leeds\" ... Done.
Extracting "tests\osm_data\leeds\Leeds.osm.shp.zip"
    to "tests\osm_data\leeds\" ... Done.
Reading the shapefile(s) at
    "tests\osm_data\leeds\Leeds-shp\shape\" ... Done.
Deleting the extracts "tests\osm_data\leeds\Leeds-shp\" ... Done.
>>> type(leeds_shp)
collections.OrderedDict
>>> list(leeds_shp.keys())
['buildings',
 'landuse',
 'natural',
 'places',
 'points',
 'railways',
 'roads',
 'waterways']

>>> # Import all layers of the shapefile data of Leeds
>>> osmdb.import_osm_data(osm_data=leeds_shp, table_name=subrgn_name, verbose=True)
To import data into table "Leeds" at postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
Importing the data ...
    "buildings" ... Done. (<total of rows> features)
    "landuse" ... Done. (<total of rows> features)
    "natural" ... Done. (<total of rows> features)
    "places" ... Done. (<total of rows> features)
    "points" ... Done. (<total of rows> features)
    "railways" ... Done. (<total of rows> features)
    "roads" ... Done. (<total of rows> features)
    "waterways" ... Done. (<total of rows> features)

Delete the test database and downloaded data files:

>>> # Delete the database 'osmdb_test'
>>> osmdb.drop_database(verbose=True)
To drop the database "osmdb_test" from postgres:***@localhost:5432
? [No]|Yes: yes
Dropping "osmdb_test" ... Done.

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