Exemple #1
0
def test_bus_route_type_mismatch():
    bus_routes = [1,2,4]

    #Assumption, user_route is not null (checked in the previous steps of the algorithm)
    user_route = create_user_route()

    analyzer = routes_analyzer(bus_routes, user_route)

    with pytest.raises(Exception):
        assert analyzer.compute_metrics()
Exemple #2
0
def test_bus_route_list_mismatch():
    p1 = Point(9.222919, 45.480466)
    bus_routes = np.array(p1)

    #Assumption, user_route is not null (checked in the previous steps of the algorithm)
    user_route = create_user_route()

    analyzer = routes_analyzer(bus_routes, user_route)

    with pytest.raises(Exception):
        assert analyzer.compute_metrics()
Exemple #3
0
def test_1():
    bus_routes = []
    route = create_bus_route()
    bus_routes.append(route)

    user_route = create_user_route()

    analyzer = routes_analyzer(bus_routes, user_route)

    metrics = analyzer.compute_metrics()

    assert len(metrics) > 0
    assert metrics[0]['number_user_coordinates'] == 3
def detect_vehicle_and_km(raw_user_route: list, snapped_user_route: list):
    """
    Recognize the vehicle and computes the kilometers traveled by the user
    
    @param raw_user_route: the user raw data, used for train recognition
    @param snapped_user_route: the user snapped data, used for bus recognition
    @return: the type of vehicle and the kilometers traveled by the user
    """
    route_dictionaries = []

    try:
        # STEP 1 for buses
        # Retrieving initial and finhsing Point of user's trip
        bus_initial_point, bus_finishing_point = find_points(
            snapped_user_route)

        # STEP 2-4 for buses
        sliced_routes_bus = get_bus_routes(bus_initial_point,
                                           bus_finishing_point)
        sliced_routes_bus = [(x, 'BUS') for x in sliced_routes_bus]
        # STEP 5
        # For every route in sliced_routes compute its metrics.
        bus_analyzer = routes_analyzer(sliced_routes_bus, snapped_user_route)
        route_dictionaries += bus_analyzer.compute_metrics()
    except Exception as message:
        print(f"No bus route matches the user one: " + str(message))

    print(f"Searched for bus routes {str(len(route_dictionaries))}")

    try:
        # STEP 1 for trains
        # Retrieving initial and finhsing Point of user's trip
        train_initial_point, train_finishing_point = find_points(
            raw_user_route)

        # STEP 2-4 for trains
        sliced_routes_train = get_train_routes(train_initial_point,
                                               train_finishing_point)
        sliced_routes_train = [(x, 'TRAIN') for x in sliced_routes_train]
        # STEP 5
        # For every route in sliced_routes compute its metrics.
        train_analyzer = routes_analyzer(sliced_routes_train, raw_user_route)
        route_dictionaries += train_analyzer.compute_metrics()
    except Exception as message:
        print(f"No train route matches the user one: " + str(message))

    print(f"Searched for train routes {str(len(route_dictionaries))}")

    if len(route_dictionaries) != 0:
        # STEP 6
        # Search the dictionary with the maximum metrics
        print("Space race between:")
        for route in route_dictionaries:
            print(route['route'][0])
            print(route['route'][len(route['route']) - 1])
            print(route['percentage_user'])
            print(route['number_user_coordinates'])
            print(route['percentage_poly'])
            print(route['number_polygons'])
            print('-------------------------------------')
        evaluator = metrics_evaluator(route_dictionaries)
        best_route = evaluator.evaluate()

        print("winner")
        print(best_route['route'][0])
        print(best_route['route'][len(best_route['route']) - 1])
        print(best_route['percentage_user'])
        print(best_route['number_user_coordinates'])
        print(best_route['percentage_poly'])
        print(best_route['number_polygons'])

        # STEP 7
        # Calculate the distance with haversine
        print(best_route['route'][0])
        print(best_route['route'][len(best_route['route']) - 1])
        print('route len ' + str(len(best_route['route'])))

        km_travelled = compute_kilometers(best_route['route'])
        vehicle = best_route['vehicle']
        return vehicle, km_travelled

    else:
        # No match is found
        return None, 0