1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Functions for importing tiger data and handling tarbar and directory files
16 from psycopg2.extras import Json
18 from nominatim.db.connection import connect
19 from nominatim.db.async_connection import WorkerPool
20 from nominatim.db.sql_preprocessor import SQLPreprocessor
21 from nominatim.errors import UsageError
22 from nominatim.data.place_info import PlaceInfo
24 LOG = logging.getLogger()
27 """ Context manager that goes through Tiger input files which may
28 either be in a directory or gzipped together in a tar file.
31 def __init__(self, data_dir):
32 self.tar_handle = None
35 if data_dir.endswith('.tar.gz'):
37 self.tar_handle = tarfile.open(data_dir) # pylint: disable=consider-using-with
38 except tarfile.ReadError as err:
39 LOG.fatal("Cannot open '%s'. Is this a tar file?", data_dir)
40 raise UsageError("Cannot open Tiger data file.") from err
42 self.files = [i for i in self.tar_handle.getmembers() if i.name.endswith('.csv')]
43 LOG.warning("Found %d CSV files in tarfile with path %s", len(self.files), data_dir)
45 files = os.listdir(data_dir)
46 self.files = [os.path.join(data_dir, i) for i in files if i.endswith('.csv')]
47 LOG.warning("Found %d CSV files in path %s", len(self.files), data_dir)
50 LOG.warning("Tiger data import selected but no files found at %s", data_dir)
57 def __exit__(self, exc_type, exc_val, exc_tb):
59 self.tar_handle.close()
60 self.tar_handle = None
64 """ Return a file handle to the next file to be processed.
65 Raises an IndexError if there is no file left.
67 fname = self.files.pop(0)
69 if self.tar_handle is not None:
70 return io.TextIOWrapper(self.tar_handle.extractfile(fname))
72 return open(fname, encoding='utf-8')
76 return len(self.files)
79 def handle_threaded_sql_statements(pool, fd, analyzer):
80 """ Handles sql statement with multiplexing
83 # Using pool of database connections to execute sql statements
85 sql = "SELECT tiger_line_import(%s, %s, %s, %s, %s, %s)"
87 for row in csv.DictReader(fd, delimiter=';'):
89 address = dict(street=row['street'], postcode=row['postcode'])
90 args = ('SRID=4326;' + row['geometry'],
91 int(row['from']), int(row['to']), row['interpolation'],
92 Json(analyzer.process_place(PlaceInfo({'address': address}))),
93 analyzer.normalize_postcode(row['postcode']))
96 pool.next_free_worker().perform(sql, args=args)
100 print('.', end='', flush=True)
104 def add_tiger_data(data_dir, config, threads, tokenizer):
105 """ Import tiger data from directory or tar file `data dir`.
107 dsn = config.get_libpq_dsn()
109 with TigerInput(data_dir) as tar:
113 with connect(dsn) as conn:
114 sql = SQLPreprocessor(conn, config)
115 sql.run_sql_file(conn, 'tiger_import_start.sql')
117 # Reading files and then for each file line handling
118 # sql_query in <threads - 1> chunks.
119 place_threads = max(1, threads - 1)
121 with WorkerPool(dsn, place_threads, ignore_sql_errors=True) as pool:
122 with tokenizer.name_analyzer() as analyzer:
124 with tar.next_file() as fd:
125 handle_threaded_sql_statements(pool, fd, analyzer)
129 LOG.warning("Creating indexes on Tiger data")
130 with connect(dsn) as conn:
131 sql = SQLPreprocessor(conn, config)
132 sql.run_sql_file(conn, 'tiger_import_finish.sql')