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='tempfile', max_size_spooled=1, decode_geojson=False, decode_wkt=False, decode_other_tags=False, parse_geojson=False, sort_by='id', **kwargs)

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), 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

  • chunk_size (int or 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 'tempfile'

  • max_size_spooled (int, float) – see pyhelpers.sql.PostgreSQL.read_sql_query, defaults to 1 (in GB)

  • decode_geojson (bool) – whether to decode textual GeoJSON, defaults to False

  • decode_wkt (bool) – whether to decode 'coordinates' (if it is wkt), defaults to False

  • decode_other_tags (bool) – whether to decode 'other_tags' (if available), defaults to False

  • 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.

>>> # Import example data into the database:

>>> sr_name = 'Rutland'
>>> dat_dir = "tests"  # a temporary data directory

>>> osmdb_test.import_subregion_osm_pbf(
...     subregion_names=sr_name, data_dir=dat_dir, rm_osm_pbf=True,
...     confirmation_required=False)

>>> rutland_shp = osmdb_test.Reader.read_shp_zip(
...     subregion_name=sr_name, data_dir=dat_dir, rm_extracts=True,
...     rm_shp_zip=True, download_confirmation_required=False)
>>> osmdb_test.import_osm_data(rutland_shp, sr_name, confirmation_required=False)

>>> # Fetch data of all available layers from the database
>>> rutland_pbf = osmdb_test.fetch_osm_data(sr_name, table_named_as_subregion=True)

>>> type(rutland_pbf)
dict
>>> 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_)
dict
>>> list(rutland_pbf_.keys())
# ['points', 'multipolygons']

>>> # Data of the 'points' layer
>>> rutland_pbf_points = rutland_pbf_['points']
>>> 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...

>>> # Parsed data
>>> rutland_pbf_parsed_ = osmdb_test.fetch_osm_data(
...     sr_name, lyr_names, decode_geojson=True, decode_wkt=True,
...     decode_other_tags=True, sort_by='id')

>>> # Parsed data of the 'points' layer
>>> rutland_pbf_parsed_points = rutland_pbf_parsed_['points']
>>> rutland_pbf_parsed_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]

>>> # 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.

See also