PostgresOSM.drop_subregion_tables

PostgresOSM.drop_subregion_tables(subregion_names, schema_names=None, table_named_as_subregion=False, schema_named_as_layer=False, confirmation_required=True, verbose=False)[source]

Delete all or specific schemas/layers of subregion data from the database being connected.

Parameters:
  • subregion_names (str | list) – name of table for a subregion (or name of a subregion)

  • schema_names (str | list | 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

  • confirmation_required (bool) – whether to ask for confirmation to proceed, defaults to True

  • verbose (bool | int) – whether to print relevant information in console, defaults to False

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.

Import example data into the database:

>>> dat_dir = "tests\osm_data"  # Specify a temporary data directory

>>> # Import PBF data of 'Rutland' and 'Isle of Wight'
>>> subrgn_name_1 = ['Rutland', 'Isle of Wight']
>>> osmdb.import_subregion_osm_pbf(
...     subrgn_name_1, data_dir=dat_dir, expand=True, parse_geometry=True,
...     parse_properties=True, parse_other_tags=True, verbose=True)
To import .osm.pbf data of the following geographic (sub)region(s):
    "Rutland"
    "Isle of Wight"
  into postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
Downloading "rutland-latest.osm.pbf"
    to "tests\osm_data\rutland\" ... Done.
Reading "tests\osm_data\rutland\rutland-latest.osm.pbf" ... Done.
Importing the data into table "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)
Downloading "isle-of-wight-latest.osm.pbf"
    to "tests\osm_data\isle-of-wight\" ... Done.
Reading "tests\osm_data\isle-of-wight\isle-of-wight-latest.osm.pbf" ... Done.
Importing the data into table "Isle of Wight" ...
    "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)

>>> # Change the data source
>>> osmdb.data_source = 'BBBike'
>>> subrgn_name_2 = 'London'

>>> # An alternative way to import the shapefile data of 'London'
>>> london_shp = osmdb.reader.read_shp_zip(
...     subrgn_name_2, data_dir=dat_dir, rm_extracts=True, download=True, verbose=True)
Downloading "London.osm.shp.zip"
    to "tests\osm_data\london\" ... Done.
Extracting "tests\osm_data\london\London.osm.shp.zip"
    to "tests\osm_data\london\" ... Done.
Reading the shapefile(s) at
    "tests\osm_data\london\London-shp\shape\" ... Done.
Deleting the extracts "tests\osm_data\london\London-shp\" ... Done.
>>> osmdb.import_osm_data(london_shp, table_name=subrgn_name_2, verbose=True)
To import data into table "London" 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 data of ‘Rutland’:

>>> subrgn_name = 'Rutland'

>>> # Delete data of Rutland under the schemas 'buildings' and 'landuse'
>>> lyr_name = ['buildings', 'landuse']
>>> osmdb.drop_subregion_tables(subrgn_name, lyr_name, verbose=True)
None of the data exists.

>>> # Delete 'points' layer data of Rutland
>>> lyr_name = 'points'
>>> osmdb.drop_subregion_tables(subrgn_name, lyr_name, verbose=True)
To drop table "points"."Rutland"
  from postgres:***@localhost:5432/osmdb_test
? [No]|Yes: yes
Dropping the table ...
    "points"."Rutland" ... Done.

>>> # Delete all available tables of Rutland
>>> osmdb.drop_subregion_tables(subrgn_name, verbose=True)
To drop table from postgres:***@localhost:5432/osmdb_test: "Rutland"
  under the schemas:
    "lines"
    "multilinestrings"
    "multipolygons"
    "other_relations"
? [No]|Yes: yes
Dropping the tables ...
    "lines"."Rutland" ... Done.
    "multilinestrings"."Rutland" ... Done.
    "multipolygons"."Rutland" ... Done.
    "other_relations"."Rutland" ... Done.

Delete ‘buildings’ and ‘points’ data of London and Isle of Wight:

>>> # Delete 'buildings' and 'points' layers of London and Isle of Wight
>>> subrgn_names = ['London', 'Isle of Wight']
>>> lyr_names = ['buildings', 'points']
>>> osmdb.drop_subregion_tables(subrgn_names, schema_names=lyr_names, verbose=True)
To drop tables from postgres:***@localhost:5432/osmdb_test:
    "Isle of Wight"
    "London"
  under the schemas:
    "points"
    "buildings"
? [No]|Yes: yes
Dropping the tables ...
    "points"."Isle of Wight" ... Done.
    "points"."London" ... Done.
    "buildings"."London" ... Done.

>>> # Delete the rest of the data of London and Isle of Wight
>>> osmdb.drop_subregion_tables(subrgn_names, verbose=True)
To drop tables from postgres:***@localhost:5432/osmdb_test:
    "Isle of Wight"
    "London"
  under the schemas:
    "railways"
    "landuse"
    "other_relations"
    "lines"
    "multilinestrings"
    "waterways"
    "roads"
    "multipolygons"
    "natural"
    "places"
? [No]|Yes: yes
Dropping the tables ...
    "railways"."London" ... Done.
    "landuse"."London" ... Done.
    "other_relations"."Isle of Wight" ... Done.
    "lines"."Isle of Wight" ... Done.
    "multilinestrings"."Isle of Wight" ... Done.
    "waterways"."London" ... Done.
    "roads"."London" ... Done.
    "multipolygons"."Isle of Wight" ... Done.
    "natural"."London" ... Done.
    "places"."London" ... Done.

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.