From 4fb2652eafdf69219c0462ce9576de792d375555 Mon Sep 17 00:00:00 2001 From: Alexey Zakharenkov <35913079+alexey-zakharenkov@users.noreply.github.com> Date: Fri, 20 Nov 2020 11:55:28 +0300 Subject: [PATCH] Add description, simplify input processing in tiles2pb.py --- db/tiles2pg.py | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/db/tiles2pg.py b/db/tiles2pg.py index 7e46a69..2f705f7 100755 --- a/db/tiles2pg.py +++ b/db/tiles2pg.py @@ -1,7 +1,17 @@ #!/usr/bin/python3 + +"""This script takes a file where each line of the form + + + +represents the number of OSM nodes in a rectangular tile +[lat, lon, lat + 0.01, lon + 0.01]. +lat_x_100 is latitude multiplied by 100 and truncated to an integer. +""" + + import argparse import logging -import re import sys import psycopg2 @@ -17,31 +27,36 @@ if __name__ == '__main__': log_level = logging.INFO if options.verbose else logging.WARNING logging.basicConfig(level=log_level, format='%(levelname)s: %(message)s') - COUNT_LAT_LON_RE = r'^\s*(\d+)\s+(-?\d+)\s+(-?\d+)' + TILE_SIDE = 0.01 # degrees with psycopg2.connect(f'dbname={options.database}') as conn: with conn.cursor() as cur: cnt = 0 for line in sys.stdin: - m = re.match(COUNT_LAT_LON_RE, line) - if m: - (count, lat, lon) = (int(m.group(1)), - float(m.group(2))/100, - float(m.group(3))/100) - cur.execute(f''' + tokens = line.split() + if len(tokens) == 3: + try: + (count, lat, lon) = (int(t) for t in tokens) + except ValueError: + logging.critical(f"Wrong number format at line {cnt}") + conn.rollback() + sys.exit(1) + + lat /= 100.0 + lon /= 100.0 + cur.execute(f""" INSERT INTO {options.table} (count, tile) VALUES (%s, ST_SetSRID(ST_MakeBox2d(ST_Point(%s, %s), ST_Point(%s, %s)), 4326) ) - ''', (count, lon, lat, lon + 0.01, lat + 0.01) + """, (count, lon, lat, lon + TILE_SIDE, lat + TILE_SIDE) ) cnt += 1 else: logging.warning(f"Incorrect count-lat-lon line '{line}'") - logging.info('Commit') + logging.info("Commit") conn.commit() - logging.info(f'Uploaded {cnt} tiles') - + logging.info(f"Uploaded {cnt} tiles")