Skip to content

Station

CityStation

Bases: Station

This is a city station for internal commuting

Source code in june/geography/station.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class CityStation(Station):
    """This is a city station for internal commuting"""

    def __init__(self, city: str = None, super_area: SuperArea = None):
        super().__init__(city=city, super_area=super_area)
        self.city_transports = []

    @property
    def n_city_transports(self):
        """ """
        return len(self.city_transports)

    def get_commute_subgroup(self):
        """ """
        return self.city_transports[randint(0, self.n_city_transports - 1)][0]

    @property
    def station_type(self):
        """ """
        return "city"

n_city_transports property

station_type property

get_commute_subgroup()

Source code in june/geography/station.py
50
51
52
def get_commute_subgroup(self):
    """ """
    return self.city_transports[randint(0, self.n_city_transports - 1)][0]

ExternalCityStation

Bases: ExternalStation

This an external city station that lives outside the simulated domain.

Source code in june/geography/station.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class ExternalCityStation(ExternalStation):
    """This an external city station that lives outside the simulated domain."""

    def __init__(self, id: int, domain_id: int, city: str = None):
        super().__init__(id=id, domain_id=domain_id, city=city)
        self.city_transports = []

    @property
    def n_city_transports(self):
        """ """
        return len(self.city_transports)

    def get_commute_subgroup(self):
        """ """
        group = self.city_transports[randint(0, self.n_city_transports - 1)]
        return ExternalSubgroup(group=group, subgroup_type=0)

n_city_transports property

get_commute_subgroup()

Source code in june/geography/station.py
188
189
190
191
def get_commute_subgroup(self):
    """ """
    group = self.city_transports[randint(0, self.n_city_transports - 1)]
    return ExternalSubgroup(group=group, subgroup_type=0)

ExternalInterCityStation

Bases: ExternalStation

This an external city station that lives outside the simulated domain.

Source code in june/geography/station.py
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
class ExternalInterCityStation(ExternalStation):
    """This an external city station that lives outside the simulated domain."""

    def __init__(self, id: int, domain_id: int, city: str = None):
        super().__init__(id=id, domain_id=domain_id, city=city)
        self.inter_city_transports = []

    @property
    def n_inter_city_transports(self):
        """ """
        return len(self.inter_city_transports)

    def get_commute_subgroup(self):
        """ """
        group = self.inter_city_transports[randint(0, self.n_inter_city_transports - 1)]
        return ExternalSubgroup(group=group, subgroup_type=0)

n_inter_city_transports property

get_commute_subgroup()

Source code in june/geography/station.py
206
207
208
209
def get_commute_subgroup(self):
    """ """
    group = self.inter_city_transports[randint(0, self.n_inter_city_transports - 1)]
    return ExternalSubgroup(group=group, subgroup_type=0)

ExternalStation

Bases: ExternalGroup

Source code in june/geography/station.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
class ExternalStation(ExternalGroup):
    """ """
    external = True

    def __init__(self, id: int, domain_id: int, city: str = None):
        super().__init__(spec="station", domain_id=domain_id, id=id)
        self.commuter_ids = set()
        self.city = city

    @property
    def coordinates(self):
        """ """
        return self.super_area.coordinates

    def get_commute_subgroup(self):
        """ """
        raise NotImplementedError

coordinates property

get_commute_subgroup()

Source code in june/geography/station.py
171
172
173
def get_commute_subgroup(self):
    """ """
    raise NotImplementedError

InterCityStation

Bases: Station

This is an inter-city station for inter-city commuting

Source code in june/geography/station.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class InterCityStation(Station):
    """This is an inter-city station for inter-city commuting"""

    def __init__(self, city: str = None, super_area: SuperArea = None):
        super().__init__(city=city, super_area=super_area)
        self.inter_city_transports = []

    @property
    def n_inter_city_transports(self):
        """ """
        return len(self.inter_city_transports)

    def get_commute_subgroup(self):
        """ """
        return self.inter_city_transports[randint(0, self.n_inter_city_transports - 1)][
            0
        ]

    @property
    def station_type(self):
        """ """
        return "inter_city"

n_inter_city_transports property

station_type property

get_commute_subgroup()

Source code in june/geography/station.py
72
73
74
75
76
def get_commute_subgroup(self):
    """ """
    return self.inter_city_transports[randint(0, self.n_inter_city_transports - 1)][
        0
    ]

Station

This represents a general station.

Source code in june/geography/station.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Station:
    """This represents a general station."""

    external = False
    _id = count()

    def __init__(self, city: str = None, super_area: SuperArea = None):
        self.id = next(self._id)
        self.commuter_ids = set()
        self.city = city
        self.super_area = super_area

    @property
    def coordinates(self):
        """ """
        return self.super_area.coordinates

coordinates property

Stations

Bases: Supergroup

A collection of stations belonging to a city.

Source code in june/geography/station.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 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
class Stations(Supergroup):
    """A collection of stations belonging to a city."""

    def __init__(self, stations: List[Station]):
        super().__init__(stations)
        self._ball_tree = None

    @classmethod
    def from_city_center(
        cls,
        city: City,
        type: str,
        super_areas: SuperAreas,
        number_of_stations: int = 4,
        distance_to_city_center: int = 20,
    ):
        """Initialises ``number_of_stations`` radially around the city center.

        Args:
            city (City): 
            type (str): 
            super_areas (SuperAreas): The super_areas where to put the hubs on
            number_of_stations (int, optional): How many stations to initialise (Default value = 4)
            distance_to_city_center (int, optional): The distance from the center to the each station (Default value = 20)

        """
        stations = []
        angle = 0
        delta_angle = 2 * np.pi / number_of_stations
        x = distance_to_city_center
        y = 0
        city_coordinates = city.coordinates
        for i in range(number_of_stations):
            station_position = add_distance_to_lat_lon(
                city_coordinates[0], city_coordinates[1], x=x, y=y
            )
            angle += delta_angle
            x = distance_to_city_center * np.cos(angle)
            y = distance_to_city_center * np.sin(angle)
            super_area = super_areas.get_closest_super_area(np.array(station_position))
            if type == "city_station":
                station = CityStation(city=city.name, super_area=super_area)
            elif type == "inter_city_station":
                station = InterCityStation(city=city.name, super_area=super_area)
            else:
                raise ValueError
            stations.append(station)
        return cls(stations)

    def _construct_ball_tree(self):
        """ """
        coordinates = np.array([np.deg2rad(station.coordinates) for station in self])
        self._ball_tree = BallTree(coordinates, metric="haversine")

    def get_closest_station(self, coordinates):
        """

        Args:
            coordinates: 

        """
        coordinates = np.array(coordinates)
        if self._ball_tree is None:
            raise ValueError("Stations initialised without a BallTree")
        if coordinates.shape == (2,):
            coordinates = coordinates.reshape(1, -1)
        indcs = self._ball_tree.query(
            np.deg2rad(coordinates), return_distance=False, k=1
        )
        super_areas = [self[idx] for idx in indcs[:, 0]]
        return super_areas[0]

from_city_center(city, type, super_areas, number_of_stations=4, distance_to_city_center=20) classmethod

Initialises number_of_stations radially around the city center.

Parameters:

Name Type Description Default
city City
required
type str
required
super_areas SuperAreas

The super_areas where to put the hubs on

required
number_of_stations int

How many stations to initialise (Default value = 4)

4
distance_to_city_center int

The distance from the center to the each station (Default value = 20)

20
Source code in june/geography/station.py
 91
 92
 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
@classmethod
def from_city_center(
    cls,
    city: City,
    type: str,
    super_areas: SuperAreas,
    number_of_stations: int = 4,
    distance_to_city_center: int = 20,
):
    """Initialises ``number_of_stations`` radially around the city center.

    Args:
        city (City): 
        type (str): 
        super_areas (SuperAreas): The super_areas where to put the hubs on
        number_of_stations (int, optional): How many stations to initialise (Default value = 4)
        distance_to_city_center (int, optional): The distance from the center to the each station (Default value = 20)

    """
    stations = []
    angle = 0
    delta_angle = 2 * np.pi / number_of_stations
    x = distance_to_city_center
    y = 0
    city_coordinates = city.coordinates
    for i in range(number_of_stations):
        station_position = add_distance_to_lat_lon(
            city_coordinates[0], city_coordinates[1], x=x, y=y
        )
        angle += delta_angle
        x = distance_to_city_center * np.cos(angle)
        y = distance_to_city_center * np.sin(angle)
        super_area = super_areas.get_closest_super_area(np.array(station_position))
        if type == "city_station":
            station = CityStation(city=city.name, super_area=super_area)
        elif type == "inter_city_station":
            station = InterCityStation(city=city.name, super_area=super_area)
        else:
            raise ValueError
        stations.append(station)
    return cls(stations)

get_closest_station(coordinates)

Parameters:

Name Type Description Default
coordinates
required
Source code in june/geography/station.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def get_closest_station(self, coordinates):
    """

    Args:
        coordinates: 

    """
    coordinates = np.array(coordinates)
    if self._ball_tree is None:
        raise ValueError("Stations initialised without a BallTree")
    if coordinates.shape == (2,):
        coordinates = coordinates.reshape(1, -1)
    indcs = self._ball_tree.query(
        np.deg2rad(coordinates), return_distance=False, k=1
    )
    super_areas = [self[idx] for idx in indcs[:, 0]]
    return super_areas[0]