2018-11-15 21:15:43 +00:00
|
|
|
from typing import Tuple, Set
|
2018-11-15 12:44:15 +00:00
|
|
|
|
2018-11-15 21:15:43 +00:00
|
|
|
from shapely.geometry import Point, LineString
|
2018-11-15 12:44:15 +00:00
|
|
|
from shapely.geometry import Polygon
|
2018-11-15 21:15:43 +00:00
|
|
|
from shapely.geometry.base import BaseGeometry
|
|
|
|
|
|
|
|
|
|
|
|
class Lane:
|
|
|
|
|
|
|
|
def __init__(self, lane_id: str, polygon: LineString):
|
|
|
|
self.polygon = polygon
|
|
|
|
self.lane_id = lane_id
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
"""Overrides the default implementation"""
|
|
|
|
return hash(self.lane_id)
|
2018-11-14 13:40:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Area:
|
|
|
|
|
|
|
|
def __init__(self, coords, name=''):
|
2018-11-15 21:15:43 +00:00
|
|
|
self.locked = False
|
2018-11-14 13:40:05 +00:00
|
|
|
self.rectangle = Polygon(coords)
|
|
|
|
self.name = name
|
|
|
|
self.emissions = 0.0
|
2018-11-15 21:15:43 +00:00
|
|
|
self._lanes: Set[Lane] = set()
|
2018-11-14 13:40:05 +00:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.rectangle.__eq__(other)
|
|
|
|
|
2018-11-15 11:20:19 +00:00
|
|
|
def __contains__(self, item):
|
2018-11-15 12:44:15 +00:00
|
|
|
return self.rectangle.contains(item)
|
2018-11-15 11:20:19 +00:00
|
|
|
|
2018-11-14 13:40:05 +00:00
|
|
|
@property
|
|
|
|
def bounds(self):
|
|
|
|
return self.rectangle.bounds
|
|
|
|
|
2018-11-15 21:15:43 +00:00
|
|
|
def intersects(self, other: BaseGeometry) -> bool:
|
|
|
|
return self.rectangle.intersects(other)
|
|
|
|
|
|
|
|
def add_lane(self, lane: Lane):
|
|
|
|
self._lanes.add(lane)
|
|
|
|
|
|
|
|
def remove_lane(self, lane: Lane):
|
|
|
|
self._lanes.remove(lane)
|
|
|
|
|
2018-11-14 13:40:05 +00:00
|
|
|
@classmethod
|
|
|
|
def from_bounds(cls, xmin, ymin, xmax, ymax):
|
|
|
|
return cls((
|
|
|
|
(xmin, ymin),
|
|
|
|
(xmin, ymax),
|
|
|
|
(xmax, ymax),
|
|
|
|
(xmax, ymin)))
|
2018-11-15 12:44:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Vehicle:
|
|
|
|
|
2018-11-15 21:15:43 +00:00
|
|
|
def __init__(self, veh_id: int, pos: Tuple[float, float]):
|
2018-11-20 12:52:23 +00:00
|
|
|
self.emissions: float = None
|
2018-11-15 21:15:43 +00:00
|
|
|
self.veh_id = veh_id
|
2018-11-15 12:44:15 +00:00
|
|
|
self.pos = Point(pos)
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return str(self.__dict__)
|
2018-11-20 12:52:23 +00:00
|
|
|
|