Example #1
0
    def get_bus_stop_closest_to_point(bus_stop_documents, provided_point):
        """
        Get the bus stop which is closest to a geographic point.

        :param bus_stop_documents: [bus_stop_document]
        :param provided_point: Point
        :return closest_bus_stop: bus_stop_document
        """
        minimum_distance = float('Inf')
        closest_bus_stop = None

        for bus_stop_document in bus_stop_documents:
            bus_stop_document_point = bus_stop_document.get('point')

            current_distance = distance(
                point_one=provided_point,
                longitude_two=bus_stop_document_point.get('longitude'),
                latitude_two=bus_stop_document_point.get('latitude')
            )
            if current_distance == 0:
                closest_bus_stop = bus_stop_document
                break
            elif current_distance < minimum_distance:
                minimum_distance = current_distance
                closest_bus_stop = bus_stop_document
            else:
                pass

        return closest_bus_stop
def estimate_heuristic_cost(starting_point_document, ending_point_document):
    """
    Make a heuristic estimation regarding the cost of travelling from starting_point to ending_point.

    :param starting_point_document: {'longitude', 'latitude'}
    :param ending_point_document:  {'longitude', 'latitude'}
    :return: (heuristic_distance_cost, heuristic_travelling_time_cost): (float, float) in (meters, seconds)
    """
    starting_point = Point(
        longitude=starting_point_document.get('longitude'),
        latitude=starting_point_document.get('latitude')
    )
    ending_point = Point(
        longitude=ending_point_document.get('longitude'),
        latitude=ending_point_document.get('latitude')
    )
    heuristic_distance_cost = distance(
        point_one=starting_point,
        point_two=ending_point
    )
    heuristic_travelling_time_cost = estimate_travelling_time(
        distance_to_be_covered=heuristic_distance_cost,
        max_speed=standard_speed
    )
    return heuristic_distance_cost, heuristic_travelling_time_cost
def estimate_real_cost(starting_point_document, ending_point_document, max_speed, road_type, traffic_density):
    """
    Estimate the real cost of travelling from starting_point to ending_point.

    :param starting_point_document: {'longitude', 'latitude'}
    :param ending_point_document: {'longitude', 'latitude'}
    :param max_speed: float
    :param road_type: One of the road types that can be accessed by a bus (string).
    :param traffic_density: float value between 0 and 1.
    :return: (real_distance_cost, real_travelling_time_cost): (float, float) in (meters, seconds)
    """
    starting_point = Point(
        longitude=starting_point_document.get('longitude'),
        latitude=starting_point_document.get('latitude')
    )
    ending_point = Point(
        longitude=ending_point_document.get('longitude'),
        latitude=ending_point_document.get('latitude')
    )
    real_distance_cost = distance(
        point_one=starting_point,
        point_two=ending_point
    )
    real_travelling_time_cost = estimate_travelling_time(
        distance_to_be_covered=real_distance_cost,
        max_speed=max_speed,
        road_type=road_type,
        traffic_density=traffic_density
    )
    return real_distance_cost, real_travelling_time_cost
Example #4
0
def g_score_estimate(starting_point_document, ending_point_document, max_speed, road_type, traffic_density):
    """
    Estimate the cost of getting from the starting point to the ending point.

    :param starting_point_document: {'longitude', 'latitude'}}
    :param ending_point_document: {'longitude', 'latitude'}}
    :param max_speed: float
    :param road_type: One of the road types that can be accessed by a bus (string).
    :param traffic_density: float value between 0 and 1.
    :return: (f_score_distance in meters, f_score_time_on_road in seconds)
    """
    starting_point = Point(
        longitude=starting_point_document.get('longitude'),
        latitude=starting_point_document.get('latitude')
    )
    ending_point = Point(
        longitude=ending_point_document.get('longitude'),
        latitude=ending_point_document.get('latitude')
    )
    estimated_distance = distance(point_one=starting_point, point_two=ending_point)
    road_type_speed_decrease_factor = estimate_road_type_speed_decrease_factor(road_type=road_type)
    traffic_speed_decrease_factor = estimate_traffic_speed_decrease_factor(traffic_density=traffic_density)
    estimated_time_on_road = estimated_distance / (road_type_speed_decrease_factor * traffic_speed_decrease_factor *
                                                   (float(max_speed) * 1000 / 3600))
    return estimated_distance, estimated_time_on_road
    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 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
Example #7
0
    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 estimate_heuristic_cost(starting_point_document, ending_point_document):
    """
    Make a heuristic estimation regarding the cost of travelling from starting_point to ending_point.

    :param starting_point_document: {'longitude', 'latitude'}
    :param ending_point_document:  {'longitude', 'latitude'}
    :return: (heuristic_distance_cost, heuristic_travelling_time_cost): (float, float) in (meters, seconds)
    """
    starting_point = Point(longitude=starting_point_document.get('longitude'),
                           latitude=starting_point_document.get('latitude'))
    ending_point = Point(longitude=ending_point_document.get('longitude'),
                         latitude=ending_point_document.get('latitude'))
    heuristic_distance_cost = distance(point_one=starting_point,
                                       point_two=ending_point)
    heuristic_travelling_time_cost = estimate_travelling_time(
        distance_to_be_covered=heuristic_distance_cost,
        max_speed=standard_speed)
    return heuristic_distance_cost, heuristic_travelling_time_cost
Example #9
0
def heuristic_cost_estimate(starting_point_document, ending_point_document):
    """
    Make a rough estimation regarding the cost of getting from the starting point to the ending point.

    :param starting_point_document: {'longitude', 'latitude'}
    :param ending_point_document:  {'longitude', 'latitude'}
    :return: (f_score_distance in meters, f_score_time_on_road in seconds)
    """
    starting_point = Point(
        longitude=starting_point_document.get('longitude'),
        latitude=starting_point_document.get('latitude')
    )
    ending_point = Point(
        longitude=ending_point_document.get('longitude'),
        latitude=ending_point_document.get('latitude')
    )
    estimated_distance = distance(point_one=starting_point, point_two=ending_point)
    estimated_time_on_road = estimated_distance / (float(standard_speed) * 1000 / 3600)
    return estimated_distance, estimated_time_on_road
def estimate_real_cost(starting_point_document, ending_point_document,
                       max_speed, road_type, traffic_density):
    """
    Estimate the real cost of travelling from starting_point to ending_point.

    :param starting_point_document: {'longitude', 'latitude'}
    :param ending_point_document: {'longitude', 'latitude'}
    :param max_speed: float
    :param road_type: One of the road types that can be accessed by a bus (string).
    :param traffic_density: float value between 0 and 1.
    :return: (real_distance_cost, real_travelling_time_cost): (float, float) in (meters, seconds)
    """
    starting_point = Point(longitude=starting_point_document.get('longitude'),
                           latitude=starting_point_document.get('latitude'))
    ending_point = Point(longitude=ending_point_document.get('longitude'),
                         latitude=ending_point_document.get('latitude'))
    real_distance_cost = distance(point_one=starting_point,
                                  point_two=ending_point)
    real_travelling_time_cost = estimate_travelling_time(
        distance_to_be_covered=real_distance_cost,
        max_speed=max_speed,
        road_type=road_type,
        traffic_density=traffic_density)
    return real_distance_cost, real_travelling_time_cost