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
 
  18 from ..db.connection import connect
 
  19 from ..tools.freeze import is_frozen
 
  22 LOG = logging.getLogger()
 
  27     Add additional data from a file or an online source.
 
  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.
 
  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/customize/Tiger/
 
  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')
 
  65     def run(self, args: NominatimArgs) -> int:
 
  66         from ..tools import add_osm_data
 
  68         with connect(args.config.get_libpq_dsn()) as conn:
 
  70                 print('Database is marked frozen. New data can\'t be added.')
 
  74             return asyncio.run(self._add_tiger_data(args))
 
  76         osm2pgsql_params = args.osm2pgsql_options(default_cache=1000, default_threads=1)
 
  77         if args.file or args.diff:
 
  78             return add_osm_data.add_data_from_file(args.config.get_libpq_dsn(),
 
  79                                                    cast(str, args.file or args.diff),
 
  83             return add_osm_data.add_osm_object(args.config.get_libpq_dsn(),
 
  89             return add_osm_data.add_osm_object(args.config.get_libpq_dsn(),
 
  95             return add_osm_data.add_osm_object(args.config.get_libpq_dsn(),
 
  96                                                'relation', args.relation,
 
 102     async def _add_tiger_data(self, args: NominatimArgs) -> int:
 
 103         from ..tokenizer import factory as tokenizer_factory
 
 104         from ..tools import tiger_data
 
 106         assert args.tiger_data
 
 108         tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
 
 109         return await tiger_data.add_tiger_data(args.tiger_data,
 
 111                                                args.threads or psutil.cpu_count() or 1,