class TrafficDataParser(object): def __init__(self): self.mongodb_database_connection = MongodbDatabaseConnection(host=mongodb_host, port=mongodb_port) self.edge_documents = [] self.traffic_event_documents = [] self.minimum_longitude = float("inf") self.maximum_longitude = float("-inf") self.minimum_latitude = float("inf") self.maximum_latitude = float("-inf") log(module_name="traffic_data_parser", log_type="DEBUG", log_message="mongodb_database_connection: established") def check_borders_of_traffic_event_document(self, traffic_event_document): """ Check if a traffic_event_document corresponds to the operation area, by comparing their borders. If yes, then return True. Otherwise, False. traffic_event_document: { '_id', 'event_id', 'event_type', 'event_level', 'point': {'longitude', 'latitude'}, 'datetime' } :param traffic_event_document: traffic_event_document :return: included_in_borders: bool """ included_in_borders = True traffic_event_point_document = traffic_event_document.get("point") traffic_event_longitude = traffic_event_point_document.get("longitude") traffic_event_latitude = traffic_event_point_document.get("latitude") if ( traffic_event_longitude < self.minimum_longitude or traffic_event_longitude > self.maximum_longitude or traffic_event_latitude < self.minimum_latitude or traffic_event_latitude > self.maximum_latitude ): included_in_borders = False return included_in_borders @staticmethod def estimate_traffic_density_value(event_level): """ Estimate the traffic_density_value based on the event_level. :param event_level: int :return: traffic_density_value: float """ if event_level == 1: traffic_density_value = 0.2 elif event_level == 2: traffic_density_value = 0.4 elif event_level == 3: traffic_density_value = 0.6 elif event_level == 4: traffic_density_value = 0.8 else: traffic_density_value = 0.0 return traffic_density_value @staticmethod def get_edge_document_with_minimum_distance(traffic_event_document, edge_documents): """ Get the edge_document which corresponds to the nearest point of a traffic_event. edge_document: { '_id', 'starting_node': {'osm_id', 'point': {'longitude', 'latitude'}}, 'ending_node': {'osm_id', 'point': {'longitude', 'latitude'}}, 'max_speed', 'road_type', 'way_id', 'traffic_density' } traffic_event_document: { '_id', 'event_id', 'event_type', 'event_level', 'point': {'longitude', 'latitude'}, 'datetime' } :param traffic_event_document: traffic_event_document :param edge_documents: [edge_document] :return: """ edge_document_with_minimum_distance = None minimum_distance = float("Inf") traffic_event_point_document = traffic_event_document.get("point") traffic_event_longitude = traffic_event_point_document.get("longitude") traffic_event_latitude = traffic_event_point_document.get("latitude") traffic_event_point = Point(longitude=traffic_event_longitude, latitude=traffic_event_latitude) for edge_document in edge_documents: starting_node = edge_document.get("starting_node") starting_node_point_document = starting_node.get("point") starting_node_point = Point( longitude=starting_node_point_document.get("longitude"), latitude=starting_node_point_document.get("latitude"), ) ending_node = edge_document.get("ending_node") ending_node_point_document = ending_node.get("point") ending_node_point = Point( longitude=ending_node_point_document.get("longitude"), latitude=ending_node_point_document.get("latitude"), ) distance_of_starting_node = distance(point_one=traffic_event_point, point_two=starting_node_point) distance_of_ending_node = distance(point_one=traffic_event_point, point_two=ending_node_point) distance_of_edge_document = distance_of_starting_node + distance_of_ending_node if distance_of_edge_document < minimum_distance: edge_document_with_minimum_distance = edge_document minimum_distance = distance_of_edge_document return edge_document_with_minimum_distance def set_borders_of_operation_area(self, edge_documents): """ Set the minimum and maximum values for longitude and latitude of the operation area. edge_document: { '_id', 'starting_node': {'osm_id', 'point': {'longitude', 'latitude'}}, 'ending_node': {'osm_id', 'point': {'longitude', 'latitude'}}, 'max_speed', 'road_type', 'way_id', 'traffic_density' } :param edge_documents: [edge_document] :return: None """ for edge_document in edge_documents: starting_node = edge_document.get("starting_node") starting_node_point_document = starting_node.get("point") starting_node_longitude = starting_node_point_document.get("longitude") starting_node_latitude = starting_node_point_document.get("latitude") if starting_node_longitude < self.minimum_longitude: self.minimum_longitude = starting_node_longitude if starting_node_longitude > self.maximum_longitude: self.maximum_longitude = starting_node_longitude if starting_node_latitude < self.minimum_latitude: self.minimum_latitude = starting_node_latitude if starting_node_latitude > self.maximum_latitude: self.maximum_latitude = starting_node_latitude ending_node = edge_document.get("ending_node") ending_node_point_document = ending_node.get("point") ending_node_longitude = ending_node_point_document.get("longitude") ending_node_latitude = ending_node_point_document.get("latitude") if ending_node_longitude < self.minimum_longitude: self.minimum_longitude = ending_node_longitude if ending_node_longitude > self.maximum_longitude: self.maximum_longitude = ending_node_longitude if ending_node_latitude < self.minimum_latitude: self.minimum_latitude = ending_node_latitude if ending_node_latitude > self.maximum_latitude: self.maximum_latitude = ending_node_latitude def update_traffic_data(self): """ edge_document: { '_id', 'starting_node': {'osm_id', 'point': {'longitude', 'latitude'}}, 'ending_node': {'osm_id', 'point': {'longitude', 'latitude'}}, 'max_speed', 'road_type', 'way_id', 'traffic_density' } traffic_event_document: { '_id', 'event_id', 'event_type', 'event_level', 'point': {'longitude', 'latitude'}, 'datetime' } :return: None """ self.edge_documents = self.mongodb_database_connection.find_edge_documents() self.traffic_event_documents = self.mongodb_database_connection.find_traffic_event_documents() self.set_borders_of_operation_area(edge_documents=self.edge_documents) for traffic_event_document in self.traffic_event_documents: if self.check_borders_of_traffic_event_document(traffic_event_document=traffic_event_document): edge_document_with_minimum_distance = self.get_edge_document_with_minimum_distance( traffic_event_document=traffic_event_document, edge_documents=self.edge_documents ) traffic_density_value = self.estimate_traffic_density_value( event_level=traffic_event_document.get("event_level") ) self.mongodb_database_connection.update_traffic_density( edge_object_id=edge_document_with_minimum_distance.get("_id"), new_traffic_density_value=traffic_density_value, ) else: print "traffic_event_document: out_of_borders -", traffic_event_document
class TrafficDataSimulator(object): def __init__(self): self.mongodb_database_connection = MongodbDatabaseConnection(host=mongodb_host, port=mongodb_port) log(module_name='traffic_data_simulator', log_type='DEBUG', log_message='mongodb_database_connection: established') def clear_traffic_density(self): self.mongodb_database_connection.clear_traffic_density() def generate_traffic_data_between_two_bus_stops(self, starting_bus_stop=None, ending_bus_stop=None, starting_bus_stop_name=None, ending_bus_stop_name=None): """ Generate random traffic density values for the edges which connect two bus_stops. bus_stop_document: {'_id', 'osm_id', 'name', 'point': {'longitude', 'latitude'}} bus_stop_waypoints_document: { '_id', 'starting_bus_stop': {'_id', 'osm_id', 'name', 'point': {'longitude', 'latitude'}}, 'ending_bus_stop': {'_id', 'osm_id', 'name', 'point': {'longitude', 'latitude'}}, 'waypoints': [[edge_object_id]] } :param starting_bus_stop: bus_stop_document :param ending_bus_stop: bus_stop_document :param starting_bus_stop_name: string :param ending_bus_stop_name: string :return: None """ bus_stop_waypoints_document = self.mongodb_database_connection.find_bus_stop_waypoints_document( starting_bus_stop=starting_bus_stop, ending_bus_stop=ending_bus_stop, starting_bus_stop_name=starting_bus_stop_name, ending_bus_stop_name=ending_bus_stop_name ) edge_object_ids_included_in_bus_stop_waypoints_document = \ self.mongodb_database_connection.get_edge_object_ids_included_in_bus_stop_waypoints( bus_stop_waypoints=bus_stop_waypoints_document ) self.generate_traffic_data_for_edge_object_ids( edge_object_ids=edge_object_ids_included_in_bus_stop_waypoints_document ) def generate_traffic_data_between_multiple_bus_stops(self, bus_stops=None, bus_stop_names=None): """ Generate random traffic density values for the edges which connect multiple bus_stops. bus_stop_document: {'_id', 'osm_id', 'name', 'point': {'longitude', 'latitude'}} :param bus_stops: [bus_stop_documents] :param bus_stop_names: [string] :return: None """ if bus_stops is not None: number_of_bus_stops = len(bus_stops) for i in range(0, number_of_bus_stops - 1): starting_bus_stop = bus_stops[i] ending_bus_stop = bus_stops[i + 1] self.generate_traffic_data_between_two_bus_stops( starting_bus_stop=starting_bus_stop, ending_bus_stop=ending_bus_stop ) elif bus_stop_names is not None: number_of_bus_stop_names = len(bus_stop_names) for i in range(0, number_of_bus_stop_names - 1): starting_bus_stop_name = bus_stop_names[i] ending_bus_stop_name = bus_stop_names[i + 1] self.generate_traffic_data_between_two_bus_stops( starting_bus_stop_name=starting_bus_stop_name, ending_bus_stop_name=ending_bus_stop_name ) else: pass def generate_traffic_data_for_bus_line(self, bus_line=None, line_id=None): """ Generate random traffic density values for the edge_documents which are included in a bus_line_document. bus_line_document: { '_id', 'line_id', 'bus_stops': [{'_id', 'osm_id', 'name', 'point': {'longitude', 'latitude'}}] } :param bus_line: bus_line_document :param line_id: int :return: None """ edge_object_ids_included_in_bus_line_document = \ self.mongodb_database_connection.get_edge_object_ids_included_in_bus_line( bus_line=bus_line, line_id=line_id ) self.generate_traffic_data_for_edge_object_ids( edge_object_ids=edge_object_ids_included_in_bus_line_document ) def generate_traffic_data_for_bus_lines(self, bus_lines=None): """ Generate random traffic density values for the edge_documents which are included in a bus_line_documents. bus_line_document: { '_id', 'line_id', 'bus_stops': [{'_id', 'osm_id', 'name', 'point': {'longitude', 'latitude'}}] } :param bus_lines: [bus_line_document] :return: None """ if bus_lines is None: bus_lines = self.mongodb_database_connection.find_bus_line_documents() for bus_line in bus_lines: self.generate_traffic_data_for_bus_line(bus_line=bus_line) def generate_traffic_data_for_edge_object_ids(self, edge_object_ids): """ Generate random traffic density values and update the corresponding edge_documents. edge_document: { '_id', 'starting_node': {'osm_id', 'point': {'longitude', 'latitude'}}, 'ending_node': {'osm_id', 'point': {'longitude', 'latitude'}}, 'max_speed', 'road_type', 'way_id', 'traffic_density' } :param edge_object_ids: [ObjectId] :return: None """ number_of_edge_object_ids = len(edge_object_ids) number_of_produced_traffic_values = random.randint(0, number_of_edge_object_ids - 1) for i in range(0, number_of_produced_traffic_values): edge_object_ids_index = random.randint(0, number_of_edge_object_ids - 1) edge_object_id = edge_object_ids[edge_object_ids_index] new_traffic_density_value = random.uniform(0, 1) self.mongodb_database_connection.update_traffic_density( edge_object_id=edge_object_id, new_traffic_density_value=new_traffic_density_value )
class TrafficDataParser(object): def __init__(self): self.mongodb_database_connection = MongodbDatabaseConnection(host=mongodb_host, port=mongodb_port) self.edge_documents = [] self.traffic_event_documents = [] self.minimum_latitude = float('inf') self.maximum_latitude = float('-inf') self.minimum_longitude = float('inf') self.maximum_longitude = float('-inf') log(module_name='traffic_data_parser', log_type='DEBUG', log_message='mongodb_database_connection: established') def check_borders_of_traffic_event_document(self, traffic_event_document): """ Check if a traffic_event_document corresponds to the operation area, by comparing their borders. If yes, then return True. Otherwise, False. :param traffic_event_document: traffic_event_document :return: included_in_borders: bool """ included_in_borders = True traffic_event_point_document = traffic_event_document.get('point') traffic_event_longitude = traffic_event_point_document.get('longitude') traffic_event_latitude = traffic_event_point_document.get('latitude') if (traffic_event_longitude < self.minimum_longitude or traffic_event_longitude > self.maximum_longitude or traffic_event_latitude < self.minimum_latitude or traffic_event_latitude > self.maximum_latitude): included_in_borders = False return included_in_borders @staticmethod def estimate_traffic_density_value(event_level): """ Estimate the traffic_density_value based on the event_level. :param event_level: int :return: traffic_density_value: float """ if event_level == 1: traffic_density_value = 0.2 elif event_level == 2: traffic_density_value = 0.4 elif event_level == 3: traffic_density_value = 0.6 elif event_level == 4: traffic_density_value = 0.8 else: traffic_density_value = 0.0 return traffic_density_value def get_borders_of_operation_area(self): """ Get the minimum and maximum values for longitude and latitude of the operation area. :return: borders: {'minimum_latitude', 'maximum_latitude', 'minimum_longitude', 'maximum_longitude'} """ if len(self.edge_documents) == 0: self.retrieve_edge_documents() self.set_borders_of_operation_area() borders = { 'minimum_latitude': self.minimum_latitude, 'maximum_latitude': self.maximum_latitude, 'minimum_longitude': self.minimum_longitude, 'maximum_longitude': self.maximum_longitude } return borders @staticmethod def get_edge_document_with_minimum_distance(traffic_event_document, edge_documents): """ Get the edge_document which corresponds to the nearest point of a traffic_event. :param traffic_event_document: traffic_event_document :param edge_documents: [edge_document] :return: """ edge_document_with_minimum_distance = None minimum_distance = float('Inf') traffic_event_point_document = traffic_event_document.get('point') traffic_event_longitude = traffic_event_point_document.get('longitude') traffic_event_latitude = traffic_event_point_document.get('latitude') traffic_event_point = Point( longitude=traffic_event_longitude, latitude=traffic_event_latitude ) for edge_document in edge_documents: starting_node = edge_document.get('starting_node') starting_node_point_document = starting_node.get('point') starting_node_point = Point( longitude=starting_node_point_document.get('longitude'), latitude=starting_node_point_document.get('latitude') ) ending_node = edge_document.get('ending_node') ending_node_point_document = ending_node.get('point') ending_node_point = Point( longitude=ending_node_point_document.get('longitude'), latitude=ending_node_point_document.get('latitude') ) distance_of_starting_node = distance( point_one=traffic_event_point, point_two=starting_node_point ) distance_of_ending_node = distance( point_one=traffic_event_point, point_two=ending_node_point ) distance_of_edge_document = distance_of_starting_node + distance_of_ending_node if distance_of_edge_document < minimum_distance: edge_document_with_minimum_distance = edge_document minimum_distance = distance_of_edge_document return edge_document_with_minimum_distance def retrieve_edge_documents(self): self.edge_documents = self.mongodb_database_connection.find_edge_documents() def retrieve_traffic_event_documents(self): self.traffic_event_documents = self.mongodb_database_connection.find_traffic_event_documents() def set_borders_of_operation_area(self): """ Set the minimum and maximum values for longitude and latitude of the operation area. :return: None """ for edge_document in self.edge_documents: starting_node = edge_document.get('starting_node') starting_node_point_document = starting_node.get('point') starting_node_longitude = starting_node_point_document.get('longitude') starting_node_latitude = starting_node_point_document.get('latitude') if starting_node_longitude < self.minimum_longitude: self.minimum_longitude = starting_node_longitude if starting_node_longitude > self.maximum_longitude: self.maximum_longitude = starting_node_longitude if starting_node_latitude < self.minimum_latitude: self.minimum_latitude = starting_node_latitude if starting_node_latitude > self.maximum_latitude: self.maximum_latitude = starting_node_latitude ending_node = edge_document.get('ending_node') ending_node_point_document = ending_node.get('point') ending_node_longitude = ending_node_point_document.get('longitude') ending_node_latitude = ending_node_point_document.get('latitude') if ending_node_longitude < self.minimum_longitude: self.minimum_longitude = ending_node_longitude if ending_node_longitude > self.maximum_longitude: self.maximum_longitude = ending_node_longitude if ending_node_latitude < self.minimum_latitude: self.minimum_latitude = ending_node_latitude if ending_node_latitude > self.maximum_latitude: self.maximum_latitude = ending_node_latitude def update_traffic_data(self): """ :return: None """ self.retrieve_edge_documents() self.retrieve_traffic_event_documents() self.set_borders_of_operation_area() for traffic_event_document in self.traffic_event_documents: if self.check_borders_of_traffic_event_document(traffic_event_document=traffic_event_document): edge_document_with_minimum_distance = self.get_edge_document_with_minimum_distance( traffic_event_document=traffic_event_document, edge_documents=self.edge_documents ) traffic_density_value = self.estimate_traffic_density_value( event_level=traffic_event_document.get('event_level') ) self.mongodb_database_connection.update_traffic_density( edge_object_id=edge_document_with_minimum_distance.get('_id'), new_traffic_density_value=traffic_density_value ) else: print 'traffic_event_document: out_of_borders -', traffic_event_document
class TrafficDataSimulator(object): def __init__(self): self.mongodb_database_connection = MongodbDatabaseConnection(host=mongodb_host, port=mongodb_port) self.lowest_traffic_density_value = 0 self.highest_traffic_density_value = 1 log(module_name='traffic_data_simulator', log_type='DEBUG', log_message='mongodb_database_connection: established') def clear_traffic_density(self): self.mongodb_database_connection.clear_traffic_density() def generate_traffic_data_between_two_bus_stops(self, starting_bus_stop=None, ending_bus_stop=None, starting_bus_stop_name=None, ending_bus_stop_name=None): """ Generate random traffic density values for the edges which connect two bus_stops. :param starting_bus_stop: bus_stop_document :param ending_bus_stop: bus_stop_document :param starting_bus_stop_name: string :param ending_bus_stop_name: string :return: None """ bus_stop_waypoints_document = self.mongodb_database_connection.find_bus_stop_waypoints_document( starting_bus_stop=starting_bus_stop, ending_bus_stop=ending_bus_stop, starting_bus_stop_name=starting_bus_stop_name, ending_bus_stop_name=ending_bus_stop_name ) edge_object_ids_included_in_bus_stop_waypoints_document = \ self.mongodb_database_connection.get_edge_object_ids_included_in_bus_stop_waypoints( bus_stop_waypoints=bus_stop_waypoints_document ) self.generate_traffic_data_for_edge_object_ids( edge_object_ids=edge_object_ids_included_in_bus_stop_waypoints_document ) def generate_traffic_data_between_multiple_bus_stops(self, bus_stops=None, bus_stop_names=None): """ Generate random traffic density values for the edges which connect multiple bus_stops. :param bus_stops: [bus_stop_documents] :param bus_stop_names: [string] :return: None """ if bus_stops is not None: number_of_bus_stops = len(bus_stops) for i in range(0, number_of_bus_stops - 1): starting_bus_stop = bus_stops[i] ending_bus_stop = bus_stops[i + 1] self.generate_traffic_data_between_two_bus_stops( starting_bus_stop=starting_bus_stop, ending_bus_stop=ending_bus_stop ) elif bus_stop_names is not None: number_of_bus_stop_names = len(bus_stop_names) for i in range(0, number_of_bus_stop_names - 1): starting_bus_stop_name = bus_stop_names[i] ending_bus_stop_name = bus_stop_names[i + 1] self.generate_traffic_data_between_two_bus_stops( starting_bus_stop_name=starting_bus_stop_name, ending_bus_stop_name=ending_bus_stop_name ) else: pass def generate_traffic_data_for_bus_line(self, bus_line=None, bus_line_id=None): """ Generate random traffic density values for the edge_documents which are included in a bus_line_document. :param bus_line: bus_line_document :param bus_line_id: int :return: None """ edge_object_ids_included_in_bus_line_document = \ self.mongodb_database_connection.get_edge_object_ids_included_in_bus_line( bus_line=bus_line, bus_line_id=bus_line_id ) self.generate_traffic_data_for_edge_object_ids( edge_object_ids=edge_object_ids_included_in_bus_line_document ) def generate_traffic_data_for_bus_lines(self, bus_lines=None): """ Generate random traffic density values for the edge_documents which are included in a bus_line_documents. :param bus_lines: [bus_line_document] :return: None """ if bus_lines is None: bus_lines = self.mongodb_database_connection.find_bus_line_documents() for bus_line in bus_lines: self.generate_traffic_data_for_bus_line(bus_line=bus_line) def generate_traffic_data_for_edge_object_ids(self, edge_object_ids): """ Generate random traffic density values and update the corresponding edge_documents. :param edge_object_ids: [ObjectId] :return: None """ number_of_edge_object_ids = len(edge_object_ids) number_of_produced_traffic_values = random.randint(0, number_of_edge_object_ids - 1) for i in range(0, number_of_produced_traffic_values): # edge_object_ids_index = random.randint(0, number_of_edge_object_ids - 1) edge_object_ids_index = i edge_object_id = edge_object_ids[edge_object_ids_index] new_traffic_density_value = random.uniform( self.lowest_traffic_density_value, self.highest_traffic_density_value ) self.mongodb_database_connection.update_traffic_density( edge_object_id=edge_object_id, new_traffic_density_value=new_traffic_density_value ) def set_traffic_density_limits(self, lowest_traffic_density_value, highest_traffic_density_value): """ Set the lowest and highest traffic density values. :param lowest_traffic_density_value: float: [0, 1] :param highest_traffic_density_value: float: [0, 1] :return: None """ self.lowest_traffic_density_value = lowest_traffic_density_value self.highest_traffic_density_value = highest_traffic_density_value
class TrafficDataParser(object): def __init__(self): self.mongodb_database_connection = MongodbDatabaseConnection( host=mongodb_host, port=mongodb_port) self.edge_documents = [] self.traffic_event_documents = [] self.minimum_latitude = float('inf') self.maximum_latitude = float('-inf') self.minimum_longitude = float('inf') self.maximum_longitude = float('-inf') log(module_name='traffic_data_parser', log_type='DEBUG', log_message='mongodb_database_connection: established') def check_borders_of_traffic_event_document(self, traffic_event_document): """ Check if a traffic_event_document corresponds to the operation area, by comparing their borders. If yes, then return True. Otherwise, False. :param traffic_event_document: traffic_event_document :return: included_in_borders: bool """ included_in_borders = True traffic_event_point_document = traffic_event_document.get('point') traffic_event_longitude = traffic_event_point_document.get('longitude') traffic_event_latitude = traffic_event_point_document.get('latitude') if (traffic_event_longitude < self.minimum_longitude or traffic_event_longitude > self.maximum_longitude or traffic_event_latitude < self.minimum_latitude or traffic_event_latitude > self.maximum_latitude): included_in_borders = False return included_in_borders @staticmethod def estimate_traffic_density_value(event_level): """ Estimate the traffic_density_value based on the event_level. :param event_level: int :return: traffic_density_value: float """ if event_level == 1: traffic_density_value = 0.2 elif event_level == 2: traffic_density_value = 0.4 elif event_level == 3: traffic_density_value = 0.6 elif event_level == 4: traffic_density_value = 0.8 else: traffic_density_value = 0.0 return traffic_density_value def get_borders_of_operation_area(self): """ Get the minimum and maximum values for longitude and latitude of the operation area. :return: borders: {'minimum_latitude', 'maximum_latitude', 'minimum_longitude', 'maximum_longitude'} """ if len(self.edge_documents) == 0: self.retrieve_edge_documents() self.set_borders_of_operation_area() borders = { 'minimum_latitude': self.minimum_latitude, 'maximum_latitude': self.maximum_latitude, 'minimum_longitude': self.minimum_longitude, 'maximum_longitude': self.maximum_longitude } return borders @staticmethod def get_edge_document_with_minimum_distance(traffic_event_document, edge_documents): """ Get the edge_document which corresponds to the nearest point of a traffic_event. :param traffic_event_document: traffic_event_document :param edge_documents: [edge_document] :return: """ edge_document_with_minimum_distance = None minimum_distance = float('Inf') traffic_event_point_document = traffic_event_document.get('point') traffic_event_longitude = traffic_event_point_document.get('longitude') traffic_event_latitude = traffic_event_point_document.get('latitude') traffic_event_point = Point(longitude=traffic_event_longitude, latitude=traffic_event_latitude) for edge_document in edge_documents: starting_node = edge_document.get('starting_node') starting_node_point_document = starting_node.get('point') starting_node_point = Point( longitude=starting_node_point_document.get('longitude'), latitude=starting_node_point_document.get('latitude')) ending_node = edge_document.get('ending_node') ending_node_point_document = ending_node.get('point') ending_node_point = Point( longitude=ending_node_point_document.get('longitude'), latitude=ending_node_point_document.get('latitude')) distance_of_starting_node = distance(point_one=traffic_event_point, point_two=starting_node_point) distance_of_ending_node = distance(point_one=traffic_event_point, point_two=ending_node_point) distance_of_edge_document = distance_of_starting_node + distance_of_ending_node if distance_of_edge_document < minimum_distance: edge_document_with_minimum_distance = edge_document minimum_distance = distance_of_edge_document return edge_document_with_minimum_distance def retrieve_edge_documents(self): self.edge_documents = self.mongodb_database_connection.find_edge_documents( ) def retrieve_traffic_event_documents(self): self.traffic_event_documents = self.mongodb_database_connection.find_traffic_event_documents( ) def set_borders_of_operation_area(self): """ Set the minimum and maximum values for longitude and latitude of the operation area. :return: None """ for edge_document in self.edge_documents: starting_node = edge_document.get('starting_node') starting_node_point_document = starting_node.get('point') starting_node_longitude = starting_node_point_document.get( 'longitude') starting_node_latitude = starting_node_point_document.get( 'latitude') if starting_node_longitude < self.minimum_longitude: self.minimum_longitude = starting_node_longitude if starting_node_longitude > self.maximum_longitude: self.maximum_longitude = starting_node_longitude if starting_node_latitude < self.minimum_latitude: self.minimum_latitude = starting_node_latitude if starting_node_latitude > self.maximum_latitude: self.maximum_latitude = starting_node_latitude ending_node = edge_document.get('ending_node') ending_node_point_document = ending_node.get('point') ending_node_longitude = ending_node_point_document.get('longitude') ending_node_latitude = ending_node_point_document.get('latitude') if ending_node_longitude < self.minimum_longitude: self.minimum_longitude = ending_node_longitude if ending_node_longitude > self.maximum_longitude: self.maximum_longitude = ending_node_longitude if ending_node_latitude < self.minimum_latitude: self.minimum_latitude = ending_node_latitude if ending_node_latitude > self.maximum_latitude: self.maximum_latitude = ending_node_latitude def update_traffic_data(self): """ :return: None """ self.retrieve_edge_documents() self.retrieve_traffic_event_documents() self.set_borders_of_operation_area() for traffic_event_document in self.traffic_event_documents: if self.check_borders_of_traffic_event_document( traffic_event_document=traffic_event_document): edge_document_with_minimum_distance = self.get_edge_document_with_minimum_distance( traffic_event_document=traffic_event_document, edge_documents=self.edge_documents) traffic_density_value = self.estimate_traffic_density_value( event_level=traffic_event_document.get('event_level')) self.mongodb_database_connection.update_traffic_density( edge_object_id=edge_document_with_minimum_distance.get( '_id'), new_traffic_density_value=traffic_density_value) else: print 'traffic_event_document: out_of_borders -', traffic_event_document