Skip to content

Model Location Resolution

Resolve canonical room and door identities to deterministic model coordinates.

This module is deliberately independent of source databases and reference tables. It resolves an already-normalised building, floor, and room code against loaded model rooms. The resulting coordinate is a temporary modelling assumption, rather than an observed position: use the room polygon centroid when it is interior, otherwise an interior representative point.

DoorLocationResolution dataclass

Result of resolving one canonical room identity to a model door.

Parameters:

Name Type Description Default
status DoorResolutionStatus

Outcome of the room lookup and door selection.

required
room Room | None

Matched model room, when one exists.

required
door Door | None

Selected model door when the room has exactly one spatial door.

required
location Location | None

Midpoint of the selected door segment, when resolution succeeds.

required
candidate_door_count int

Number of model doors in the matched room. This allows callers to report unresolved multi-door records without re-inspecting geometry.

required
Source code in src/amr_hub_abm/location_resolution.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@dataclass(frozen=True)
class DoorLocationResolution:
    """
    Result of resolving one canonical room identity to a model door.

    Parameters
    ----------
    status : DoorResolutionStatus
        Outcome of the room lookup and door selection.
    room : Room | None
        Matched model room, when one exists.
    door : Door | None
        Selected model door when the room has exactly one spatial door.
    location : Location | None
        Midpoint of the selected door segment, when resolution succeeds.
    candidate_door_count : int
        Number of model doors in the matched room. This allows callers to
        report unresolved multi-door records without re-inspecting geometry.

    """

    status: DoorResolutionStatus
    room: Room | None
    door: Door | None
    location: Location | None
    candidate_door_count: int

DoorResolutionStatus

Bases: StrEnum

Possible outcomes when resolving a canonical room identity to a door.

Source code in src/amr_hub_abm/location_resolution.py
54
55
56
57
58
59
60
61
62
class DoorResolutionStatus(StrEnum):
    """Possible outcomes when resolving a canonical room identity to a door."""

    RESOLVED = "resolved"
    ROOM_NOT_IN_MODEL = "room_not_in_model"
    ROOM_HAS_NO_SPATIAL_GEOMETRY = "room_has_no_spatial_geometry"
    ROOM_HAS_NO_DOORS = "room_has_no_doors"
    AMBIGUOUS_DOORS = "ambiguous_doors"
    DOOR_HAS_NO_SPATIAL_GEOMETRY = "door_has_no_spatial_geometry"

RoomLocationResolution dataclass

Result of resolving one canonical room identity in a spatial model.

Parameters:

Name Type Description Default
status RoomResolutionStatus

Outcome of the room lookup and spatial placement.

required
room Room | None

Matched model room, when one exists.

required
location Location | None

Deterministic representative coordinate, when spatial resolution succeeds.

required
Source code in src/amr_hub_abm/location_resolution.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@dataclass(frozen=True)
class RoomLocationResolution:
    """
    Result of resolving one canonical room identity in a spatial model.

    Parameters
    ----------
    status : RoomResolutionStatus
        Outcome of the room lookup and spatial placement.
    room : Room | None
        Matched model room, when one exists.
    location : Location | None
        Deterministic representative coordinate, when spatial resolution
        succeeds.

    """

    status: RoomResolutionStatus
    room: Room | None
    location: Location | None

RoomResolutionStatus

Bases: StrEnum

Possible outcomes when resolving a canonical room identity.

Source code in src/amr_hub_abm/location_resolution.py
24
25
26
27
28
29
class RoomResolutionStatus(StrEnum):
    """Possible outcomes when resolving a canonical room identity."""

    RESOLVED = "resolved"
    ROOM_NOT_IN_MODEL = "room_not_in_model"
    ROOM_HAS_NO_SPATIAL_GEOMETRY = "room_has_no_spatial_geometry"

resolve_door_location(building, floor, room_code, rooms)

Resolve a canonical room identity to the midpoint of its unique door.

The source location description identifies a room rather than a model door. To avoid introducing an unsupported arbitrary selection rule, this function resolves only rooms with exactly one door. Rooms with no doors or multiple doors return explicit outcomes for later reconciliation. As with room resolution, this function has no source-data or persistence dependencies.

Parameters:

Name Type Description Default
building str

Building name matching Room.building.

required
floor int

Floor number matching Room.floor.

required
room_code str

Canonical room code matching Room.name.

required
rooms list[Room]

Rooms loaded from the spatial model.

required

Returns:

Type Description
DoorLocationResolution

Resolution result with the unique door midpoint when exactly one spatial model door belongs to the resolved room.

Source code in src/amr_hub_abm/location_resolution.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
def resolve_door_location(
    building: str,
    floor: int,
    room_code: str,
    rooms: list[Room],
) -> DoorLocationResolution:
    """
    Resolve a canonical room identity to the midpoint of its unique door.

    The source location description identifies a room rather than a model door.
    To avoid introducing an unsupported arbitrary selection rule, this function
    resolves only rooms with exactly one door. Rooms with no doors or multiple
    doors return explicit outcomes for later reconciliation. As with room
    resolution, this function has no source-data or persistence dependencies.

    Parameters
    ----------
    building : str
        Building name matching ``Room.building``.
    floor : int
        Floor number matching ``Room.floor``.
    room_code : str
        Canonical room code matching ``Room.name``.
    rooms : list[Room]
        Rooms loaded from the spatial model.

    Returns
    -------
    DoorLocationResolution
        Resolution result with the unique door midpoint when exactly one spatial
        model door belongs to the resolved room.

    """
    room_resolution = resolve_room_location(building, floor, room_code, rooms)
    if room_resolution.status is RoomResolutionStatus.ROOM_NOT_IN_MODEL:
        return DoorLocationResolution(
            status=DoorResolutionStatus.ROOM_NOT_IN_MODEL,
            room=None,
            door=None,
            location=None,
            candidate_door_count=0,
        )
    if room_resolution.status is RoomResolutionStatus.ROOM_HAS_NO_SPATIAL_GEOMETRY:
        return DoorLocationResolution(
            status=DoorResolutionStatus.ROOM_HAS_NO_SPATIAL_GEOMETRY,
            room=room_resolution.room,
            door=None,
            location=None,
            candidate_door_count=0,
        )

    room = room_resolution.room
    if room is None:
        msg = "A resolved room location must contain a room."
        raise RuntimeError(msg)

    candidate_door_count = len(room.doors)
    if candidate_door_count == 0:
        return DoorLocationResolution(
            status=DoorResolutionStatus.ROOM_HAS_NO_DOORS,
            room=room,
            door=None,
            location=None,
            candidate_door_count=0,
        )
    if candidate_door_count > 1:
        return DoorLocationResolution(
            status=DoorResolutionStatus.AMBIGUOUS_DOORS,
            room=room,
            door=None,
            location=None,
            candidate_door_count=candidate_door_count,
        )

    door = room.doors[0]
    if door.start is None or door.end is None:
        return DoorLocationResolution(
            status=DoorResolutionStatus.DOOR_HAS_NO_SPATIAL_GEOMETRY,
            room=room,
            door=door,
            location=None,
            candidate_door_count=1,
        )

    door, midpoint = room.get_door_access_point()
    return DoorLocationResolution(
        status=DoorResolutionStatus.RESOLVED,
        room=room,
        door=door,
        location=Location(
            building=room.building,
            floor=room.floor,
            x=midpoint[0],
            y=midpoint[1],
        ),
        candidate_door_count=1,
    )

resolve_room_location(building, floor, room_code, rooms)

Resolve a canonical room identity to a deterministic model coordinate.

The function has no source-data or persistence dependencies. It is intended to be called after location ingestion has produced canonical identifiers. A room centroid is used as the temporary representative location. Concave polygons can have an exterior centroid, so the function falls back to an interior representative point to ensure that successful resolutions are valid positions in the model.

Parameters:

Name Type Description Default
building str

Building name matching Room.building.

required
floor int

Floor number matching Room.floor.

required
room_code str

Canonical room code matching Room.name.

required
rooms list[Room]

Rooms loaded from the spatial model.

required

Returns:

Type Description
RoomLocationResolution

Resolution result. A missing model room or a topological room without spatial geometry returns no coordinate.

Source code in src/amr_hub_abm/location_resolution.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def resolve_room_location(
    building: str,
    floor: int,
    room_code: str,
    rooms: list[Room],
) -> RoomLocationResolution:
    """
    Resolve a canonical room identity to a deterministic model coordinate.

    The function has no source-data or persistence dependencies. It is intended
    to be called after location ingestion has produced canonical identifiers.
    A room centroid is used as the temporary representative location. Concave
    polygons can have an exterior centroid, so the function falls back to an
    interior representative point to ensure that successful resolutions are
    valid positions in the model.

    Parameters
    ----------
    building : str
        Building name matching ``Room.building``.
    floor : int
        Floor number matching ``Room.floor``.
    room_code : str
        Canonical room code matching ``Room.name``.
    rooms : list[Room]
        Rooms loaded from the spatial model.

    Returns
    -------
    RoomLocationResolution
        Resolution result. A missing model room or a topological room without
        spatial geometry returns no coordinate.

    """
    room = next(
        (
            candidate
            for candidate in rooms
            if candidate.building == building
            and candidate.floor == floor
            and candidate.name == room_code
        ),
        None,
    )
    if room is None:
        return RoomLocationResolution(
            status=RoomResolutionStatus.ROOM_NOT_IN_MODEL,
            room=None,
            location=None,
        )

    if room.region.is_empty:
        return RoomLocationResolution(
            status=RoomResolutionStatus.ROOM_HAS_NO_SPATIAL_GEOMETRY,
            room=room,
            location=None,
        )

    point = room.region.centroid
    if not room.region.contains(point):
        point = room.region.representative_point()

    return RoomLocationResolution(
        status=RoomResolutionStatus.RESOLVED,
        room=room,
        location=Location(
            building=room.building,
            floor=room.floor,
            x=point.x,
            y=point.y,
        ),
    )