]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tools/add_osm_data.py
replace add-data function with native Python code
[nominatim.git] / nominatim / tools / add_osm_data.py
1 """
2 Function to add additional OSM data from a file or the API into the database.
3 """
4 from pathlib import Path
5 import logging
6 import urllib
7
8 from nominatim.tools.exec_utils import run_osm2pgsql, get_url
9
10 LOG = logging.getLogger()
11
12 def add_data_from_file(fname, options):
13     """ Adds data from a OSM file to the database. The file may be a normal
14         OSM file or a diff file in all formats supported by libosmium.
15     """
16     options['import_file'] = Path(fname)
17     options['append'] = True
18     run_osm2pgsql(options)
19
20     # No status update. We don't know where the file came from.
21     return 0
22
23
24 def add_osm_object(osm_type, osm_id, use_main_api, options):
25     """ Add or update a single OSM object from the latest version of the
26         API.
27     """
28     if use_main_api:
29         base_url = f'https://www.openstreetmap.org/api/0.6/{osm_type}/{osm_id}'
30         if osm_type in ('way', 'relation'):
31             base_url += '/full'
32     else:
33         # use Overpass API
34         if osm_type == 'node':
35             data = f'node({osm_id});out meta;'
36         elif osm_type == 'way':
37             data = f'(way({osm_id});>;);out meta;'
38         else:
39             data = f'(rel(id:{osm_id});>;);out meta;'
40         base_url = 'https://overpass-api.de/api/interpreter?' \
41                    + urllib.parse.urlencode({'data': data})
42
43     options['append'] = True
44     options['import_data'] = get_url(base_url).encode('utf-8')
45
46     run_osm2pgsql(options)