PostgresOSM.fetch_osm_data¶
-
PostgresOSM.
fetch_osm_data
(subregion_name, layer_names=None, table_named_as_subregion=False, schema_named_as_layer=False, chunk_size=None, method='spooled_tempfile', max_size_spooled=1, decode_geojson=False, decode_wkt=False, decode_other_tags=False, parse_geojson=False, sort_by='id', **kwargs)[source]¶ Fetch OSM data (of one or multiple layers) of a geographic region.
See also [ROP-1]
- Parameters
subregion_name (str) – name of a geographic region (or the corresponding table)
layer_names (list or None) – names of schemas for each layer of the PBF data, if
None
(default), use the default layer names as schema namestable_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
chunk_size (int, None) – the number of rows in each batch to be written at a time, defaults to
None
method (str or None) – method to be used for buffering temporary data, defaults to
'spooled_tempfile'
max_size_spooled (int, float) –
max_size_spooled
of pyhelpers.sql.PostgreSQL.read_sql_query, defaults to1
(in gigabyte)decode_geojson (bool) – whether to decode textual GeoJSON, defaults to
False
decode_wkt (bool) – whether to decode
'coordinates'
(if available and) if it is a wkt, defaults toFalse
decode_other_tags (bool) – whether to decode
'other_tags'
(if available), defaults toFalse
parse_geojson (bool) – whether to parse raw GeoJSON (as it is raw feature data), defaults to
False
sort_by (str or list) – column name(s) by which the data (fetched from PostgreSQL) is sorted, defaults to
None
- Returns
PBF (.osm.pbf) data
- Return type
dict
Example:
>>> 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' >>> # With all the examples for >>> # `.import_osm_data()` and `.import_subregion_osm_pbf()`, >>> # fetch data of all available layers >>> rutland_pbf = osmdb_test.fetch_osm_data(sr_name, ... table_named_as_subregion=True) >>> type(rutland_pbf) <class 'dict'> >>> print(list(rutland_pbf.keys())) ['points', 'lines', 'multilinestrings', 'multipolygons', 'other_relations', 'buildings', 'landuse', 'natural', 'places', 'pofw', 'pois', 'railways', 'roads', 'traffic', 'transport', 'water', 'waterways'] >>> # Fetch data of specific layers >>> lyr_names = ['points', 'multipolygons'] >>> rutland_pbf = osmdb_test.fetch_osm_data(sr_name, lyr_names, sort_by='id') >>> type(rutland_pbf) <class 'dict'> >>> print(list(rutland_pbf.keys())) # ['points', 'multipolygons'] >>> rutland_pbf_points = rutland_pbf['points'] >>> print(rutland_pbf_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... >>> rutland_pbf_ = osmdb_test.fetch_osm_data( ... sr_name, lyr_names, decode_geojson=True, decode_wkt=True, ... decode_other_tags=True, sort_by='id') >>> rutland_pbf_points_ = rutland_pbf_['points'] >>> 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]
See also
The examples about fetching data from the database provided in Quick start.