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)

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 dict or None) – names of schemas for each layer of the PBF data, if None (default), 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, to 'replace' (default), 'append' or 'fail'

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

  • chunk_size (int or 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'  # name of a subregion
>>> dat_dir = "tests"  # 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)
>>> rutland_pbf_raw = osmdb_test.Reader.read_osm_pbf(sr_name, dat_dir, verbose=True)
To download .pbf data of the following geographic region(s):
    Rutland
? [No]|Yes: yes
Downloading "rutland-latest.osm.pbf" to "tests\" ... Done.

>>> # A quick view of the PBF data
>>> type(rutland_pbf_raw)
dict
>>> list(rutland_pbf_raw.keys())
['points', 'lines', 'multilinestrings', 'multipolygons', 'other_relations']

>>> # Data of 'points' layer
>>> rutland_pbf_raw_points = rutland_pbf_raw['points']
>>> 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...

>>> # Import all layers of the raw PBF data of Rutland
>>> osmdb_test.import_osm_data(rutland_pbf_raw, table_name=sr_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 further-parsed PBF data
>>> rutland_pbf = osmdb_test.Reader.read_osm_pbf(sr_name, dat_dir,
...                                              parse_raw_feat=True,
...                                              transform_geom=True,
...                                              transform_other_tags=True)

>>> type(rutland_pbf)
dict
>>> list(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_test.import_osm_data(rutland_pbf, sr_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_test.drop_schema(schemas.keys(), verbose=True)
To drop the following schemas from postgres:***@localhost:5432/osmdb_test:
    "schema_0"
    "schema_1"
    "schema_2"
? [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"))

>>> # -- Example 2: Import data of a shapefile -----------------------------------

>>> # Read shapefile data of Rutland
>>> rutland_shp = osmdb_test.Reader.read_shp_zip(sr_name, data_dir=dat_dir,
...                                              rm_extracts=True,
...                                              rm_shp_zip=True,
...                                              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.
Deleting the extracts "tests\rutland-latest-free-shp\"  ... Done.
Deleting "tests\rutland-latest-free.shp.zip" ... Done.

>>> # A quick view of the shapefile data
>>> type(rutland_shp)
dict
>>> list(rutland_shp.keys())
['transport',
 'railways',
 'roads',
 'places',
 'landuse',
 'pofw',
 'natural',
 'traffic',
 'pois',
 'water',
 'buildings',
 'waterways']

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

>>> # -- Example 3: Import BBBike shapefile data file of Leeds -------------------

>>> 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)
To download .shp.zip data of the following geographic region(s):
    Leeds
? [No]|Yes: yes
Downloading "Leeds.osm.shp.zip" to "tests\" ... Done.
Extracting "tests\Leeds.osm.shp.zip" ...
to "tests\"
Done.
Parsing files at "tests\Leeds-shp\shape\" ... Done.
Deleting the extracts "tests\Leeds-shp\" ... Done.
Deleting "tests\Leeds.osm.shp.zip" ... Done.

>>> # A quick view of the shapefile data
>>> type(leeds_shp)
dict
>>> list(leeds_shp.keys())
['points',
 'railways',
 'roads',
 'places',
 'landuse',
 'natural',
 'buildings',
 'waterways']

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

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