]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/add_data.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / nominatim / clicmd / add_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 Implementation of the 'add-data' subcommand.
9 """
10 from typing import cast
11 import argparse
12 import logging
13
14 import psutil
15
16 from nominatim.clicmd.args import NominatimArgs
17
18 # Do not repeat documentation of subcommand classes.
19 # pylint: disable=C0111
20 # Using non-top-level imports to avoid eventually unused imports.
21 # pylint: disable=E0012,C0415
22
23 LOG = logging.getLogger()
24
25 class UpdateAddData:
26     """\
27     Add additional data from a file or an online source.
28
29     This command allows to add or update the search data in the database.
30     The data can come either from an OSM file or single OSM objects can
31     directly be downloaded from the OSM API. This function only loads the
32     data into the database. Afterwards it still needs to be integrated
33     in the search index. Use the `nominatim index` command for that.
34
35     The command can also be used to add external non-OSM data to the
36     database. At the moment the only supported format is TIGER housenumber
37     data. See the online documentation at
38     https://nominatim.org/release-docs/latest/admin/Import/#installing-tiger-housenumber-data-for-the-us
39     for more information.
40     """
41
42     def add_args(self, parser: argparse.ArgumentParser) -> None:
43         group_name = parser.add_argument_group('Source')
44         group1 = group_name.add_mutually_exclusive_group(required=True)
45         group1.add_argument('--file', metavar='FILE',
46                             help='Import data from an OSM file or diff file')
47         group1.add_argument('--diff', metavar='FILE',
48                             help='Import data from an OSM diff file (deprecated: use --file)')
49         group1.add_argument('--node', metavar='ID', type=int,
50                             help='Import a single node from the API')
51         group1.add_argument('--way', metavar='ID', type=int,
52                             help='Import a single way from the API')
53         group1.add_argument('--relation', metavar='ID', type=int,
54                             help='Import a single relation from the API')
55         group1.add_argument('--tiger-data', metavar='DIR',
56                             help='Add housenumbers from the US TIGER census database')
57         group2 = parser.add_argument_group('Extra arguments')
58         group2.add_argument('--use-main-api', action='store_true',
59                             help='Use OSM API instead of Overpass to download objects')
60         group2.add_argument('--osm2pgsql-cache', metavar='SIZE', type=int,
61                             help='Size of cache to be used by osm2pgsql (in MB)')
62         group2.add_argument('--socket-timeout', dest='socket_timeout', type=int, default=60,
63                             help='Set timeout for file downloads')
64
65
66     def run(self, args: NominatimArgs) -> int:
67         from nominatim.tokenizer import factory as tokenizer_factory
68         from nominatim.tools import tiger_data, add_osm_data
69
70         if args.tiger_data:
71             tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
72             return tiger_data.add_tiger_data(args.tiger_data,
73                                              args.config,
74                                              args.threads or psutil.cpu_count()  or 1,
75                                              tokenizer)
76
77         osm2pgsql_params = args.osm2pgsql_options(default_cache=1000, default_threads=1)
78         if args.file or args.diff:
79             return add_osm_data.add_data_from_file(args.config.get_libpq_dsn(),
80                                                    cast(str, args.file or args.diff),
81                                                    osm2pgsql_params)
82
83         if args.node:
84             return add_osm_data.add_osm_object(args.config.get_libpq_dsn(),
85                                                'node', args.node,
86                                                args.use_main_api,
87                                                osm2pgsql_params)
88
89         if args.way:
90             return add_osm_data.add_osm_object(args.config.get_libpq_dsn(),
91                                                'way', args.way,
92                                                args.use_main_api,
93                                                osm2pgsql_params)
94
95         if args.relation:
96             return add_osm_data.add_osm_object(args.config.get_libpq_dsn(),
97                                                'relation', args.relation,
98                                                args.use_main_api,
99                                                osm2pgsql_params)
100
101         return 0