Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separated Coordinate Handling #243

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]

_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