Last active
June 30, 2020 09:55
-
-
Save EricDuminil/c49d9484024239455e5b052a6a5abdb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# python citygml_give_unique_building_ids.py filename1.gml filename2.gml ... | |
import re | |
import argparse | |
parser = argparse.ArgumentParser(description='Process CityGML files and give unique building IDs if needed.') | |
parser.add_argument("citygml_filename", nargs='+') | |
args = parser.parse_args() | |
BUILDING_ID_PATTERN = re.compile("(?<=bldg:Building gml:id=\")\w+") | |
input_filenames = args.citygml_filename | |
for input_filename in input_filenames: | |
print(f'Processing {input_filename}') | |
output_filename = input_filename.replace('.gml', '_unique_ids.gml') | |
seen_ids = {} | |
with open(input_filename) as gml: | |
with open(output_filename, 'w+') as new_gml: | |
for line in gml: | |
m = re.search(BUILDING_ID_PATTERN, line) | |
if m: | |
# Building ID has been found | |
building_id = m.group(0) | |
if building_id in seen_ids: | |
# Building ID has already been used | |
seen_ids[building_id] += 1 | |
new_id = f'{building_id}_{seen_ids[building_id]}' | |
print(new_id) | |
line = re.sub(BUILDING_ID_PATTERN, new_id, line) | |
else: | |
# Building ID. | |
seen_ids[building_id] = 1 | |
new_gml.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment