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()
21 def _run_osm2pgsql(dsn: str, options: MutableMapping[str, Any]) -> None:
22 run_osm2pgsql(options)
25 with connect(dsn) as conn:
26 with conn.cursor() as cur:
27 cur.execute('SELECT flush_deleted_places()')
31 def add_data_from_file(dsn: str, fname: str, options: MutableMapping[str, Any]) -> int:
32 """ Adds data from a OSM file to the database. The file may be a normal
33 OSM file or a diff file in all formats supported by libosmium.
35 options['import_file'] = Path(fname)
36 options['append'] = True
37 _run_osm2pgsql(dsn, options)
39 # No status update. We don't know where the file came from.
43 def add_osm_object(dsn: str, osm_type: str, osm_id: int, use_main_api: bool,
44 options: MutableMapping[str, Any]) -> int:
45 """ Add or update a single OSM object from the latest version of the
49 base_url = f'https://www.openstreetmap.org/api/0.6/{osm_type}/{osm_id}'
50 if osm_type in ('way', 'relation'):
54 if osm_type == 'node':
55 data = f'node({osm_id});out meta;'
56 elif osm_type == 'way':
57 data = f'(way({osm_id});>;);out meta;'
59 data = f'(rel(id:{osm_id});>;);out meta;'
60 base_url = 'https://overpass-api.de/api/interpreter?' \
61 + urllib.parse.urlencode({'data': data})
63 options['append'] = True
64 options['import_data'] = get_url(base_url).encode('utf-8')
66 _run_osm2pgsql(dsn, options)