File indexing completed on 2024-11-10 12:47:53
0001 #!/usr/bin/env python3 0002 # 0003 # SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org> 0004 # SPDX-License-Identifier: LGPL-2.0-or-later 0005 # 0006 # Generates a GeoJSON document from coverage areas of the Transport API Repository 0007 # 0008 0009 import argparse 0010 import glob 0011 import json 0012 import os 0013 import re 0014 0015 parser = argparse.ArgumentParser(description='Generates a GeoJSON document from coverage areas of the Transport API Repository') 0016 parser.add_argument('--transport-api', type=str, required=True, help='Path to the Transport API Repository checkout') 0017 arguments = parser.parse_args() 0018 0019 output = {} 0020 output['type'] = 'FeatureCollection' 0021 output['name'] = 'Transport API Repository Coverage Data' 0022 output['features'] = [] 0023 0024 transportApiFiles = glob.glob(arguments.transport_api + "/*.json", recursive=True) 0025 for transportApiFile in transportApiFiles: 0026 f = open(transportApiFile, 'r') 0027 j = json.load(f) 0028 nameMatch = re.search('/([a-z]{2})/(.*)\\.json', transportApiFile) 0029 if nameMatch: 0030 name = nameMatch.group(1) + '-' + nameMatch.group(2) 0031 else: 0032 name = os.path.splitext(os.path.basename(transportApiFile))[0] 0033 0034 for cov in ['anyCoverage', 'regularCoverage', 'realtimeCoverage']: 0035 if not 'coverage' in j or not cov in j['coverage'] or not 'area' in j['coverage'][cov]: 0036 continue 0037 properties = {} 0038 properties['name'] = name + '-' + cov 0039 feature = {} 0040 feature['type'] = 'Feature' 0041 feature['properties'] = properties 0042 feature['geometry'] = j['coverage'][cov]['area'] 0043 output['features'].append(feature) 0044 0045 print(json.dumps(output))