1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Function to add additional OSM data from a file or the API into the database.
10 from typing import Any, MutableMapping
11 from pathlib import Path
15 from ..db.connection import connect
16 from ..utils.url_utils import get_url
17 from .exec_utils import run_osm2pgsql
19 LOG = logging.getLogger()
22 def _run_osm2pgsql(dsn: str, options: MutableMapping[str, Any]) -> None:
23 run_osm2pgsql(options)
26 with connect(dsn) as conn:
27 with conn.cursor() as cur:
28 cur.execute('SELECT flush_deleted_places()')
32 def add_data_from_file(dsn: str, fname: str, options: MutableMapping[str, Any]) -> int:
33 """ Adds data from a OSM file to the database. The file may be a normal
34 OSM file or a diff file in all formats supported by libosmium.
36 options['import_file'] = Path(fname)
37 options['append'] = True
38 _run_osm2pgsql(dsn, options)
40 # No status update. We don't know where the file came from.
44 def add_osm_object(dsn: str, osm_type: str, osm_id: int, use_main_api: bool,
45 options: MutableMapping[str, Any]) -> int:
46 """ Add or update a single OSM object from the latest version of the
50 base_url = f'https://www.openstreetmap.org/api/0.6/{osm_type}/{osm_id}'
51 if osm_type in ('way', 'relation'):
55 if osm_type == 'node':
56 data = f'node({osm_id});out meta;'
57 elif osm_type == 'way':
58 data = f'(way({osm_id});>;);out meta;'
60 data = f'(rel(id:{osm_id});>;);out meta;'
61 base_url = 'https://overpass-api.de/api/interpreter?' \
62 + urllib.parse.urlencode({'data': data})
64 options['append'] = True
65 options['import_data'] = get_url(base_url).encode('utf-8')
67 _run_osm2pgsql(dsn, options)