Coverage for src/scicom/historicalletters/space.py: 0%

1 import random

2 from typing import Dict, DefaultDict, Set

3 from collections import defaultdict

4

5 import mesa

6 import mesa_geo as mg

7 from shapely.geometry import Point

8

9 from scicom.historicalletters.agents import RegionAgent, SenderAgent

10

11

12 class Nuts2Eu(mg.GeoSpace):

13 """Define regions containing senders of letters.

14

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

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

17 """

18 _id_region_map: Dict[str, RegionAgent]

19

20 def __init__(self):

21 super().__init__(warn_crs_conversion=True)

22 self._id_region_map = {}

23

24 def add_regions(self, agents):

25 super().add_agents(agents)

26 total_area = 0

27 for agent in agents:

28 self._id_region_map[agent.unique_id] = agent

29

30 def add_sender_to_region(self, agent, region_id):

31 agent.region_id = region_id

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

33

34 def remove_sender_from_region(self, agent):

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

36 agent.region_id = None

37

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

39 super().add_agents([agent])

40 self.add_sender_to_region(agent, regionID)

41

42 def move_sender(

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

44 ) -> None:

45 self.__remove_sender(agent)

46 agent.geometry = pos

47 self.add_sender(agent, regionID)

48

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

50 super().remove_agent(agent)

51 self.remove_sender_from_region(agent)

52