Skip to content

Commit

Permalink
formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Aug 12, 2024
1 parent ec1ec13 commit e15cc87
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 39 deletions.
6 changes: 6 additions & 0 deletions data/external/models/roomfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class LatLonBox(PydanticConfiguration):
rotation: float

def __contains__(self, item: Coordinate):
"""
Check if the given item is within the specified boundaries.
@param item: The item to check.
@return: True if the item is within the boundaries, False otherwise.
"""
assert self.south < self.north
assert self.west < self.east
lat, lon = item["lat"], item["lon"]
Expand Down
60 changes: 25 additions & 35 deletions data/processors/maps/roomfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any

from external.models import roomfinder
from external.models.roomfinder import LatLonBox, Coordinate
from external.models.roomfinder import Coordinate, LatLonBox
from processors.maps.models import CustomBuildingMap, MapKey

BASE_PATH = Path(__file__).parent.parent.parent
Expand Down Expand Up @@ -42,12 +42,11 @@ def assign_roomfinder_maps(data: dict[str, dict[str, Any]]) -> None:
if entry["type"] in {"site", "campus", "area", "joined_building", "building"} and "children" in entry:
for _map in available_maps:
for child in entry["children"]:
if _entry_is_not_on_map(
data[child]["coords"],
_map.id,
_map.width,
_map.height,
map_assignment_data,
coords = data[child]["coords"]
assign_map = map_assignment_data[_map.id]
if (
_calc_xy_of_coords_on_map(coords, assign_map.latlonbox, assign_map.width, assign_map.width)
is None
):
available_maps.remove(_map)
break
Expand Down Expand Up @@ -95,18 +94,18 @@ def _set_maps_from_parent(data: dict[str, dict[str, Any]], entry: dict[str, Any]


def _extract_available_maps(
entry: dict[str, Any],
custom_maps: dict[MapKey, roomfinder.Map],
maps_list: list[roomfinder.Map],
entry: dict[str, Any],
custom_maps: dict[MapKey, roomfinder.Map],
maps_list: list[roomfinder.Map],
) -> list[roomfinder.Map]:
"""Extract all available maps for the given entry."""
available_maps: list[roomfinder.Map] = []
for (b_id, floor), _map in custom_maps.items():
if (
entry["type"] == "room"
and b_id in entry["parents"]
and "tumonline_data" in entry
and f".{floor}." in entry["tumonline_data"]["roomcode"]
entry["type"] == "room"
and b_id in entry["parents"]
and "tumonline_data" in entry
and f".{floor}." in entry["tumonline_data"]["roomcode"]
):
available_maps.append(_map)
available_maps += maps_list
Expand All @@ -129,9 +128,11 @@ def build_roomfinder_maps(data: dict[str, dict[str, Any]]) -> None:
if len(entry.get("maps", {}).get("roomfinder", {}).get("available", [])) > 0:
for entry_map in entry["maps"]["roomfinder"]["available"]:
assign_map = map_assignment_data[entry_map["id"]]
x_on_map, y_on_map = _calc_xy_of_coords_on_map(
assignment = _calc_xy_of_coords_on_map(
entry["coords"], assign_map.latlonbox, assign_map.width, assign_map.width
)
assert assignment is not None, "Map should have been removed in a previous step"
x_on_map, y_on_map = assignment

entry_map["x"] = x_on_map
entry_map["y"] = y_on_map
Expand All @@ -143,7 +144,7 @@ def build_roomfinder_maps(data: dict[str, dict[str, Any]]) -> None:


def _calc_xy_of_coords_on_map(
coords: Coordinate, map_latlonbox: LatLonBox, map_width: int, map_height: int
coords: Coordinate, map_latlonbox: LatLonBox, map_width: int, map_height: int
) -> tuple[int, int] | None:
"""
Calculate the x and y coordinates on a map.
Expand Down Expand Up @@ -198,32 +199,21 @@ def _generate_assignment_data() -> dict[str, roomfinder.Map]:
return {_map.id: _map for _map in roomfinder.Map.load_all() + list(CustomBuildingMap.load_all().values())}


def _entry_is_not_on_map(
coords: Coordinate,
map_id: str,
width: int,
height: int,
map_assignment_data: dict[str, roomfinder.Map],
) -> bool:
assign_map = map_assignment_data[map_id]
x_on_map, y_on_map = _calc_xy_of_coords_on_map(coords, assign_map.latlonbox, assign_map.width, assign_map.width)
x_invalid = x_on_map < 0 or width <= x_on_map
y_invalid = y_on_map < 0 or height <= y_on_map
return x_invalid or y_invalid


def remove_non_covering_maps(data: dict[str, dict[str, Any]]) -> None:
"""Remove maps from entries, that do not cover said coordinates"""
map_assignment_data = _generate_assignment_data()
for _id, entry in data.items():
if entry["type"] == "root":
continue
if rf_maps := entry["maps"].get("roomfinder"):
to_be_deleted = [
_map
for _map in rf_maps["available"]
if _entry_is_not_on_map(entry["coords"], _map["id"], _map["width"], _map["height"], map_assignment_data)
]
to_be_deleted = []
for _map in rf_maps["available"]:
assigned = map_assignment_data[_map["id"]]
if (
_calc_xy_of_coords_on_map(entry["coords"], assigned.latlonbox, _map["width"], _map["height"])
is None
):
to_be_deleted.append(_map)
for _map in to_be_deleted:
rf_maps["available"].remove(_map)
if not rf_maps["available"]:
Expand Down
7 changes: 3 additions & 4 deletions data/processors/maps/test_roomfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def test_coords_to_xy_off_map() -> None:
assign_map.height,
)
if (abs(lat) <= 100) and (abs(lon) <= 100):
assert (actual is not None)
assert actual is not None
else:
assert (actual is None)
assert actual is None


@pytest.mark.parametrize(
Expand All @@ -82,6 +82,5 @@ def test_coords_to_xy_off_map() -> None:
def test_coords_to_xy_translation_rotation(item: ExpectedCoordinate) -> None:
"""Test if xy coordinates translate and rotate correctly"""
assert (
_calc_xy_of_coords_on_map(item.coordinate, item.map.latlonbox, item.map.width,
item.map.height) == item.expected
_calc_xy_of_coords_on_map(item.coordinate, item.map.latlonbox, item.map.width, item.map.height) == item.expected
)

0 comments on commit e15cc87

Please sign in to comment.