Add description, simplify input processing in tiles2pb.py

This commit is contained in:
Alexey Zakharenkov 2020-11-20 11:55:28 +03:00
parent b9c28019c1
commit 4fb2652eaf

View file

@ -1,7 +1,17 @@
#!/usr/bin/python3
"""This script takes a file where each line of the form
<count> <lat_x_100> <lon_x_100>
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")