]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tools/add_osm_data.py
add type annotations for command line functions
[nominatim.git] / nominatim / tools / add_osm_data.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Function to add additional OSM data from a file or the API into the database.
9 """
10 from typing import Any, MutableMapping
11 from pathlib import Path
12 import logging
13 import urllib
14
15 from nominatim.tools.exec_utils import run_osm2pgsql, get_url
16
17 LOG = logging.getLogger()
18
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.
22     """
23     options['import_file'] = Path(fname)
24     options['append'] = True
25     run_osm2pgsql(options)
26
27     # No status update. We don't know where the file came from.
28     return 0
29
30
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
34         API.
35     """
36     if use_main_api:
37         base_url = f'https://www.openstreetmap.org/api/0.6/{osm_type}/{osm_id}'
38         if osm_type in ('way', 'relation'):
39             base_url += '/full'
40     else:
41         # use Overpass API
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;'
46         else:
47             data = f'(rel(id:{osm_id});>;);out meta;'
48         base_url = 'https://overpass-api.de/api/interpreter?' \
49                    + urllib.parse.urlencode({'data': data})
50
51     options['append'] = True
52     options['import_data'] = get_url(base_url).encode('utf-8')
53
54     run_osm2pgsql(options)
55
56     return 0