PostgresOSM.import_osm_layer¶
-
PostgresOSM.
import_osm_layer
(osm_layer_data, table_name, schema_name, 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 one layer of OSM data into a table.
- Parameters
osm_layer_data (pandas.DataFrame or geopandas.GeoDataFrame) – one layer of OSM data
schema_name (str) – name of a schema (or name of a PBF layer)
table_name (str) – name of a table
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 pyhelpers.sql.PostgreSQL.dump_data
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" >>> # Import PBF data of Rutland >>> 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 >>> tbl_name = sr_name >>> schema = list(rutland_pbf_raw.keys())[0] # 'points' >>> rutland_pbf_raw_points = rutland_pbf_raw[schema] >>> print(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... >>> osmdb_test.import_osm_layer(rutland_pbf_raw_points, tbl_name, schema, ... verbose=True) Confirmed to import the data into table '"points"."Rutland"' at postgres:***@localhost:5432/osmdb_test ? [No]|Yes: yes Creating a schema "points" ... Done. Importing data into '"points"."Rutland"' ... Done. >>> column_info = osmdb_test.get_subregion_table_column_info(tbl_name, schema) >>> print(column_info.head()) column_0 table_catalog osmdb_test table_schema points table_name Rutland column_name points ordinal_position 1 >>> rutland_pbf = osmdb_test.Reader.read_osm_pbf(sr_name, dat_dir, ... parse_raw_feat=True, ... transform_geom=True) >>> rutland_pbf_points = rutland_pbf[schema] >>> print(rutland_pbf_points.head()) id ... other_tags 0 488432 ... "odbl"=>"clean" 1 488658 ... None 2 13883868 ... None 3 14049101 ... "traffic_calming"=>"cushion" 4 14558402 ... "direction"=>"clockwise" [5 rows x 12 columns] >>> osmdb_test.import_osm_layer(rutland_pbf_points, tbl_name, schema, ... verbose=True) Confirmed to import the data into table '"points"."Rutland"' at postgres:***@localhost:5432/osmdb_test ? [No]|Yes: yes The table "points"."Rutland" already exists and is replaced ... Importing data into '"points"."Rutland"' ... Done. >>> # Delete the downloaded PBF data file >>> os.remove(cd(dat_dir, "rutland-latest.osm.pbf")) >>> # Import shapefile data of Rutland >>> lyr_name = 'railways' >>> rutland_railways_shp = osmdb_test.Reader.read_shp_zip( ... sr_name, lyr_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 from "rutland-latest-free.shp.zip" the following layer(s): 'railways' to "\tests\rutland-latest-free-shp" ... In progress ... Done. Deleting the extracts "\tests\rutland-latest-free-shp" ... Done. Deleting "tests\rutland-latest-free.shp.zip" ... Done. >>> type(rutland_railways_shp) <class 'dict'> >>> print(list(rutland_railways_shp.keys())) # ['railways'] >>> rutland_railways_shp_ = rutland_railways_shp[lyr_name] >>> osmdb_test.import_osm_layer(rutland_railways_shp_, table_name=sr_name, ... schema_name=lyr_name, verbose=True) Confirmed to import the data into table '"railways"."Rutland"' at postgres:***@localhost:5432/osmdb_test ? [No]|Yes: yes Creating a schema "railways" ... Done. Importing data into '"railways"."Rutland"' ... Done. >>> col_info = osmdb_test.get_subregion_table_column_info(tbl_name, lyr_name) >>> print(col_info.head()) column_0 column_1 ... column_6 column_7 table_catalog osmdb_test osmdb_test ... osmdb_test osmdb_test table_schema railways railways ... railways railways table_name Rutland Rutland ... Rutland Rutland column_name osm_id code ... tunnel geometry ordinal_position 1 2 ... 7 8 [5 rows x 8 columns]