2 Functions for importing tiger data and handling tarbar and directory files
10 import psycopg2.extras
12 from nominatim.db.connection import connect
13 from nominatim.db.async_connection import WorkerPool
14 from nominatim.db.sql_preprocessor import SQLPreprocessor
17 LOG = logging.getLogger()
20 def handle_tarfile_or_directory(data_dir):
21 """ Handles tarfile or directory for importing tiger data
25 if data_dir.endswith('.tar.gz'):
26 tar = tarfile.open(data_dir)
27 csv_files = [i for i in tar.getmembers() if i.name.endswith('.csv')]
28 LOG.warning("Found %d CSV files in tarfile with path %s", len(csv_files), data_dir)
30 LOG.warning("Tiger data import selected but no files in tarfile's path %s", data_dir)
33 files = os.listdir(data_dir)
34 csv_files = [os.path.join(data_dir, i) for i in files if i.endswith('.csv')]
35 LOG.warning("Found %d CSV files in path %s", len(csv_files), data_dir)
37 LOG.warning("Tiger data import selected but no files found in path %s", data_dir)
43 def handle_threaded_sql_statements(pool, fd, analyzer):
44 """ Handles sql statement with multiplexing
47 # Using pool of database connections to execute sql statements
49 sql = "SELECT tiger_line_import(%s, %s, %s, %s, %s, %s)"
51 for row in csv.DictReader(fd, delimiter=';'):
53 address = dict(street=row['street'], postcode=row['postcode'])
54 args = ('SRID=4326;' + row['geometry'],
55 int(row['from']), int(row['to']), row['interpolation'],
56 psycopg2.extras.Json(analyzer.process_place(dict(address=address))),
57 analyzer.normalize_postcode(row['postcode']))
60 pool.next_free_worker().perform(sql, args=args)
64 print('.', end='', flush=True)
68 def add_tiger_data(data_dir, config, threads, tokenizer):
69 """ Import tiger data from directory or tar file `data dir`.
71 dsn = config.get_libpq_dsn()
72 files, tar = handle_tarfile_or_directory(data_dir)
77 with connect(dsn) as conn:
78 sql = SQLPreprocessor(conn, config)
79 sql.run_sql_file(conn, 'tiger_import_start.sql')
81 # Reading files and then for each file line handling
82 # sql_query in <threads - 1> chunks.
83 place_threads = max(1, threads - 1)
85 with WorkerPool(dsn, place_threads, ignore_sql_errors=True) as pool:
86 with tokenizer.name_analyzer() as analyzer:
91 fd = io.TextIOWrapper(tar.extractfile(fname))
93 handle_threaded_sql_statements(pool, fd, analyzer)
100 LOG.warning("Creating indexes on Tiger data")
101 with connect(dsn) as conn:
102 sql = SQLPreprocessor(conn, config)
103 sql.run_sql_file(conn, 'tiger_import_finish.sql')