1 # SPDX-License-Identifier: GPL-2.0-only
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2022 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 nominatim.db.connection import connect
 
  16 from nominatim.tools.exec_utils import run_osm2pgsql, get_url
 
  18 LOG = logging.getLogger()
 
  20 def _run_osm2pgsql(dsn: str, options: MutableMapping[str, Any]) -> None:
 
  21     run_osm2pgsql(options)
 
  24     with connect(dsn) as conn:
 
  25         with conn.cursor() as cur:
 
  26             cur.execute('SELECT flush_deleted_places()')
 
  30 def add_data_from_file(dsn: str, fname: str, options: MutableMapping[str, Any]) -> int:
 
  31     """ Adds data from a OSM file to the database. The file may be a normal
 
  32         OSM file or a diff file in all formats supported by libosmium.
 
  34     options['import_file'] = Path(fname)
 
  35     options['append'] = True
 
  36     _run_osm2pgsql(dsn, options)
 
  38     # No status update. We don't know where the file came from.
 
  42 def add_osm_object(dsn: str, osm_type: str, osm_id: int, use_main_api: bool,
 
  43                    options: MutableMapping[str, Any]) -> int:
 
  44     """ Add or update a single OSM object from the latest version of the
 
  48         base_url = f'https://www.openstreetmap.org/api/0.6/{osm_type}/{osm_id}'
 
  49         if osm_type in ('way', 'relation'):
 
  53         if osm_type == 'node':
 
  54             data = f'node({osm_id});out meta;'
 
  55         elif osm_type == 'way':
 
  56             data = f'(way({osm_id});>;);out meta;'
 
  58             data = f'(rel(id:{osm_id});>;);out meta;'
 
  59         base_url = 'https://overpass-api.de/api/interpreter?' \
 
  60                    + urllib.parse.urlencode({'data': data})
 
  62     options['append'] = True
 
  63     options['import_data'] = get_url(base_url).encode('utf-8')
 
  65     _run_osm2pgsql(dsn, options)