File indexing completed on 2024-05-05 03:51:20
0001 #!/usr/bin/env python3 0002 # SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org> 0003 # SPDX-License-Identifier: LGPL-2.0-or-later 0004 0005 import argparse 0006 import glob 0007 import re 0008 import os 0009 import subprocess 0010 import sys 0011 import tempfile 0012 0013 parser = argparse.ArgumentParser(description='Test driver for raw data tile creator tirex backend') 0014 parser.add_argument('--tirex-backend', help='Path to the tile creator binary', type=str) 0015 parser.add_argument('--data', help='Path to the test data files', type=str) 0016 arguments = parser.parse_args() 0017 0018 def metaTileHash(x, y): 0019 h = [] 0020 for i in range(5): 0021 h += [((x & 0x0f) << 4) | (y & 0x0f)]; 0022 x >>= 4; 0023 y >>= 4; 0024 return h 0025 0026 # extract tiles from a meta tile 0027 def readTile(metaTileFile, x, y): 0028 tile = open(metaTileFile, 'rb') 0029 tiledata = tile.read() 0030 if not tiledata[0:4] == b'META': 0031 print('Not a metatile file') 0032 sys.exit(1) 0033 count = int.from_bytes(tiledata[4:8], byteorder='little') 0034 xCount = int.from_bytes(tiledata[8:12], byteorder='little') 0035 yCount = int.from_bytes(tiledata[12:16], byteorder='little') 0036 z = int.from_bytes(tiledata[16:20], byteorder='little') 0037 entries = [] 0038 for i in range(count): 0039 offset = int.from_bytes(tiledata[(20+i*8):(24+i*8)], byteorder='little') 0040 size = int.from_bytes(tiledata[(24+i*8):(28+i*8)], byteorder='little') 0041 entries.append((offset, size)) 0042 0043 idx = x * xCount + y 0044 entry = entries[idx] 0045 return tiledata[entry[0]:(entry[0]+entry[1])] 0046 0047 failCount = 0 0048 for refFile in glob.iglob('*-z*-*-*.osm', root_dir=arguments.data): 0049 m = re.search(r'(.*)-z(\d+)-(\d+)-(\d+).osm', refFile) 0050 inputFile = f"{m.group(1)}-input.osm" 0051 x = int(m.group(3)) 0052 y = int(m.group(4)) 0053 z = int(m.group(2)) 0054 print (inputFile, refFile, x, y, z) 0055 0056 # run the tile creator in single-shot fake mode 0057 env = os.environ.copy() 0058 env['TIREX_BACKEND_SOCKET_FILENO'] = '1' 0059 env['QT_HASH_SEED'] = '0' 0060 subprocess.run([arguments.tirex_backend, '-x', str(x), '-y', str(y), '-z', str(z), '-c', '.', 0061 '--source', os.path.join(arguments.data, inputFile)], env=env) 0062 0063 # read output tile from metatile 0064 h = metaTileHash(x, y) 0065 metaTileName = os.path.join('output', f"{z}", f"{h[4]}", f"{h[3]}", f"{h[2]}", f"{h[1]}", f"{h[0]}.meta") 0066 tileData = readTile(metaTileName, 0, 0) 0067 0068 with tempfile.TemporaryDirectory() as tmpdir: 0069 o5mOutFile = os.path.join(tmpdir, f"{m.group(1)}-z{z}-{x}-{y}.out.o5m") 0070 with open(o5mOutFile, 'wb') as f: 0071 f.write(tileData) 0072 0073 # convert to OSM format 0074 osmOutFile = os.path.join(tmpdir, f"{m.group(1)}-z{z}-{x}-{y}.out.osm") 0075 subprocess.run(['osmconvert', f"-o={osmOutFile}", o5mOutFile]) 0076 0077 # normalize result for easier comparison 0078 subprocess.run(['python3', os.path.join(os.path.dirname(os.path.abspath(__file__)), 'osm-normalize.py'), osmOutFile]) 0079 0080 # compare 0081 r = subprocess.run(['diff', '-u', os.path.join(arguments.data, refFile), osmOutFile]) 0082 if r.returncode != 0: 0083 subprocess.run(['cp', osmOutFile, f"{os.path.join(arguments.data, refFile)}.fail"]) 0084 failCount += 1 0085 0086 print(f"Found {failCount} failures.") 0087 if failCount > 0: 0088 sys.exit(1)