Skip to content

Commit

Permalink
seperated coordinates and other data from each other
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Oct 28, 2022
1 parent 6f6274e commit edd3141
Show file tree
Hide file tree
Showing 4 changed files with 485 additions and 451 deletions.
1 change: 1 addition & 0 deletions data/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def main():

logging.info("-- 02 rooms extendend")
data = merge.merge_yaml(data, "sources/02_rooms-extended.yaml")
merge.add_coordinates(data, "sources/02_coordinates.yaml")

# Add source information for these entries, which are up to here
# always declared by navigatum
Expand Down
24 changes: 24 additions & 0 deletions data/processors/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ def merge_yaml(data, path):
return _recursive_merge(data, yaml_data)


def add_coordinates(data, path):
"""
Merge coordinates from the yaml file at path on top of the given data.
This operates on the data dict directly without creating a copy.
"""
with open(path, encoding="utf-8") as file:
yaml_data = yaml.safe_load(file.read())

if not isinstance(yaml_data, dict):
raise RuntimeError(f"Error: Coordinates are not in the expected format ({path=})")

# If the key of a root element is only numeric with 4 digits,
# we assume it is a building id (which needs to be converted to string)
ids_to_fix = [] # Cannot change dict while iterating
for _id, _data in yaml_data.items():
if isinstance(_id, int) and len(str(_id)) == 4:
ids_to_fix.append(_id)
for _id in ids_to_fix:
yaml_data[str(_id)] = yaml_data[_id]
del yaml_data[_id]

return _recursive_merge(data, {_id: {"coords": val} for _id, val in yaml_data})


def merge_object(data, obj, overwrite=True):
"""
Merge the object on top of the given data.
Expand Down
Loading

0 comments on commit edd3141

Please sign in to comment.