Coverage for .tox/p313/lib/python3.13/site-packages/scicom/historicalletters/space.py: 100%

1"""The geographical space for HistoricalLetters."""

2

3 import mesa

4 import mesa_geo as mg

5

6 from scicom.historicalletters.agents import RegionAgent, SenderAgent

7

8

9 class Nuts2Eu(mg.GeoSpace):

10 """Define regions containing senders of letters.

11

12 The space model is initialized with all EU NUTS2 regions.

13 The movement of one agent during the model run consitst of

14 the removing the sender from the old region, setting the

15 new sender position and then adding the sender to the new

16 region.

17

18 This is modified from a mesa-geo example, here

19 https://github.com/projectmesa/mesa-examples/blob/main/gis/geo_schelling_points/geo_schelling_points/space.py

20 """

21

22 def __init__(self) -> None:

23 """Initialize space model."""

24 super().__init__(warn_crs_conversion=True)

25 self._id_region_map = {}

26

27 def add_regions(self, agents: RegionAgent) -> None:

28 """Add regions to space."""

29 super().add_agents(agents)

30 for agent in agents:

31 self._id_region_map[agent.unique_id] = agent

32

33 def add_sender_to_region(self, agent: SenderAgent, region_id: str) -> None:

34 """Add sender to region."""

35 agent.region_id = region_id

36 self._id_region_map[region_id].add_sender(agent)

37

38 def remove_sender_from_region(self, agent: SenderAgent) -> None:

39 """Remove sender from region."""

40 self._id_region_map[agent.region_id].remove_sender(agent)

41 agent.region_id = None

42

43 def add_sender(self, agent: SenderAgent, regionID: str) -> None:

44 """Add sender to specific region."""

45 super().add_agents([agent])

46 self.add_sender_to_region(agent, regionID)

47

48 def move_sender(

49 self, agent: SenderAgent, pos: mesa.space.FloatCoordinate, regionID: str,

50 ) -> None:

51 """Move sender from old to new region."""

52 self.__remove_sender(agent)

53 agent.geometry = pos

54 self.add_sender(agent, regionID)

55

56 def __remove_sender(self, agent: SenderAgent) -> None:

57 """Remove sender."""

58 super().remove_agent(agent)

59 self.remove_sender_from_region(agent)