2023-03-26 11:04:25 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2023-03-30 12:56:21 +00:00
|
|
|
from dataclasses import dataclass
|
2023-03-26 11:04:25 +00:00
|
|
|
import asyncio
|
|
|
|
from os.path import basename, splitext
|
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import msgpack
|
|
|
|
import psycopg
|
|
|
|
|
|
|
|
from obs.api.app import app
|
|
|
|
from obs.api.utils import chunk
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
ROAD_BUFFER = 1000
|
|
|
|
AREA_BUFFER = 100
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Road:
|
|
|
|
way_id: int
|
|
|
|
name: str
|
|
|
|
zone: str
|
|
|
|
directionality: int
|
|
|
|
oneway: int
|
|
|
|
geometry: bytes
|
|
|
|
|
2023-03-30 12:56:21 +00:00
|
|
|
|
2023-03-31 19:06:59 +00:00
|
|
|
def read_file(filename):
|
2023-03-26 11:04:25 +00:00
|
|
|
"""
|
2023-03-31 19:06:59 +00:00
|
|
|
Reads a file iteratively, yielding
|
2023-03-26 11:04:25 +00:00
|
|
|
appear. Those may be mixed.
|
|
|
|
"""
|
2023-03-29 20:08:45 +00:00
|
|
|
|
2023-03-26 11:04:25 +00:00
|
|
|
with open(filename, "rb") as f:
|
|
|
|
unpacker = msgpack.Unpacker(f)
|
|
|
|
try:
|
|
|
|
while True:
|
2023-03-31 19:06:59 +00:00
|
|
|
type_id, *data = unpacker.unpack()
|
2023-03-26 11:04:25 +00:00
|
|
|
|
2023-03-31 19:06:59 +00:00
|
|
|
if type_id == b"\x01":
|
|
|
|
yield Road(*data)
|
2023-03-26 11:04:25 +00:00
|
|
|
|
|
|
|
except msgpack.OutOfData:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
async def import_osm(connection, filename, import_group=None):
|
|
|
|
if import_group is None:
|
|
|
|
import_group = splitext(basename(filename))[0]
|
|
|
|
|
|
|
|
# Pass 1: Find IDs only
|
|
|
|
road_ids = []
|
2023-03-31 19:06:59 +00:00
|
|
|
for item in read_file(filename):
|
2023-03-26 11:04:25 +00:00
|
|
|
road_ids.append(item.way_id)
|
|
|
|
|
|
|
|
async with connection.cursor() as cursor:
|
2023-03-30 12:56:21 +00:00
|
|
|
log.info("Pass 1: Delete previously imported data")
|
2023-03-26 11:04:25 +00:00
|
|
|
|
2023-03-30 12:56:21 +00:00
|
|
|
log.debug("Delete import group %s", import_group)
|
2023-03-26 11:04:25 +00:00
|
|
|
await cursor.execute(
|
|
|
|
"DELETE FROM road WHERE import_group = %s", (import_group,)
|
|
|
|
)
|
|
|
|
|
2023-03-30 12:56:21 +00:00
|
|
|
log.debug("Delete roads by way_id")
|
2023-03-26 11:04:25 +00:00
|
|
|
for ids in chunk(road_ids, 10000):
|
|
|
|
await cursor.execute("DELETE FROM road WHERE way_id = ANY(%s)", (ids,))
|
|
|
|
|
|
|
|
# Pass 2: Import
|
2023-03-30 12:56:21 +00:00
|
|
|
log.info("Pass 2: Import roads")
|
2023-06-10 10:56:44 +00:00
|
|
|
amount = 0
|
|
|
|
for items in chunk(read_file(filename), 10000):
|
|
|
|
amount += 10000
|
|
|
|
log.info(f"...{amount}/{len(ids)} ({100*amount/len(ids)}%)")
|
|
|
|
async with cursor.copy(
|
|
|
|
"COPY road (way_id, name, zone, directionality, oneway, geometry, import_group) FROM STDIN"
|
|
|
|
) as copy:
|
|
|
|
for item in items:
|
|
|
|
await copy.write_row(
|
|
|
|
(
|
|
|
|
item.way_id,
|
|
|
|
item.name,
|
|
|
|
item.zone,
|
|
|
|
item.directionality,
|
|
|
|
item.oneway,
|
|
|
|
bytes.hex(item.geometry),
|
|
|
|
import_group,
|
|
|
|
)
|
2023-03-26 11:04:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
2023-03-30 12:56:21 +00:00
|
|
|
logging.basicConfig(level=logging.DEBUG, format="%(levelname)s: %(message)s")
|
|
|
|
|
2023-03-26 11:04:25 +00:00
|
|
|
url = app.config.POSTGRES_URL
|
|
|
|
url = url.replace("+asyncpg", "")
|
|
|
|
|
|
|
|
async with await psycopg.AsyncConnection.connect(url) as connection:
|
|
|
|
for filename in sys.argv[1:]:
|
2023-03-30 12:56:21 +00:00
|
|
|
log.debug("Loading file: %s", filename)
|
2023-03-26 11:04:25 +00:00
|
|
|
await import_osm(connection, filename)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
asyncio.run(main())
|