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.tools.exec_utils import run_osm2pgsql, get_url
 
  17 LOG = logging.getLogger()
 
  19 def add_data_from_file(fname: str, options: MutableMapping[str, Any]) -> int:
 
  20     """ Adds data from a OSM file to the database. The file may be a normal
 
  21         OSM file or a diff file in all formats supported by libosmium.
 
  23     options['import_file'] = Path(fname)
 
  24     options['append'] = True
 
  25     run_osm2pgsql(options)
 
  27     # No status update. We don't know where the file came from.
 
  31 def add_osm_object(osm_type: str, osm_id: int, use_main_api: bool,
 
  32                    options: MutableMapping[str, Any]) -> int:
 
  33     """ Add or update a single OSM object from the latest version of the
 
  37         base_url = f'https://www.openstreetmap.org/api/0.6/{osm_type}/{osm_id}'
 
  38         if osm_type in ('way', 'relation'):
 
  42         if osm_type == 'node':
 
  43             data = f'node({osm_id});out meta;'
 
  44         elif osm_type == 'way':
 
  45             data = f'(way({osm_id});>;);out meta;'
 
  47             data = f'(rel(id:{osm_id});>;);out meta;'
 
  48         base_url = 'https://overpass-api.de/api/interpreter?' \
 
  49                    + urllib.parse.urlencode({'data': data})
 
  51     options['append'] = True
 
  52     options['import_data'] = get_url(base_url).encode('utf-8')
 
  54     run_osm2pgsql(options)