1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Implementation of the 'add-data' subcommand.
10 from typing import cast
17 from .args import NominatimArgs
19 # Do not repeat documentation of subcommand classes.
20 # pylint: disable=C0111
21 # Using non-top-level imports to avoid eventually unused imports.
22 # pylint: disable=E0012,C0415
24 LOG = logging.getLogger()
28 Add additional data from a file or an online source.
30 This command allows to add or update the search data in the database.
31 The data can come either from an OSM file or single OSM objects can
32 directly be downloaded from the OSM API. This function only loads the
33 data into the database. Afterwards it still needs to be integrated
34 in the search index. Use the `nominatim index` command for that.
36 The command can also be used to add external non-OSM data to the
37 database. At the moment the only supported format is TIGER housenumber
38 data. See the online documentation at
39 https://nominatim.org/release-docs/latest/admin/Import/#installing-tiger-housenumber-data-for-the-us
43 def add_args(self, parser: argparse.ArgumentParser) -> None:
44 group_name = parser.add_argument_group('Source')
45 group1 = group_name.add_mutually_exclusive_group(required=True)
46 group1.add_argument('--file', metavar='FILE',
47 help='Import data from an OSM file or diff file')
48 group1.add_argument('--diff', metavar='FILE',
49 help='Import data from an OSM diff file (deprecated: use --file)')
50 group1.add_argument('--node', metavar='ID', type=int,
51 help='Import a single node from the API')
52 group1.add_argument('--way', metavar='ID', type=int,
53 help='Import a single way from the API')
54 group1.add_argument('--relation', metavar='ID', type=int,
55 help='Import a single relation from the API')
56 group1.add_argument('--tiger-data', metavar='DIR',
57 help='Add housenumbers from the US TIGER census database')
58 group2 = parser.add_argument_group('Extra arguments')
59 group2.add_argument('--use-main-api', action='store_true',
60 help='Use OSM API instead of Overpass to download objects')
61 group2.add_argument('--osm2pgsql-cache', metavar='SIZE', type=int,
62 help='Size of cache to be used by osm2pgsql (in MB)')
63 group2.add_argument('--socket-timeout', dest='socket_timeout', type=int, default=60,
64 help='Set timeout for file downloads')
67 def run(self, args: NominatimArgs) -> int:
68 from ..tools import add_osm_data
71 return asyncio.run(self._add_tiger_data(args))
73 osm2pgsql_params = args.osm2pgsql_options(default_cache=1000, default_threads=1)
74 if args.file or args.diff:
75 return add_osm_data.add_data_from_file(args.config.get_libpq_dsn(),
76 cast(str, args.file or args.diff),
80 return add_osm_data.add_osm_object(args.config.get_libpq_dsn(),
86 return add_osm_data.add_osm_object(args.config.get_libpq_dsn(),
92 return add_osm_data.add_osm_object(args.config.get_libpq_dsn(),
93 'relation', args.relation,
100 async def _add_tiger_data(self, args: NominatimArgs) -> int:
101 from ..tokenizer import factory as tokenizer_factory
102 from ..tools import tiger_data
104 assert args.tiger_data
106 tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
107 return await tiger_data.add_tiger_data(args.tiger_data,
109 args.threads or psutil.cpu_count() or 1,