def get_centermost_location(df, sample, future):
    prediction_timedelta = timedelta(minutes=future)
    predictor = TrajectoryPredictor(sample.past_traj)
    predicted_location = predictor.predict_linearly(prediction_timedelta)
    if len(df) == 0:
        return predicted_location
    elif len(df) <= 2:
        x = 0
    else:
        x = get_centermost_id(df, round(len(df) / 2))
    return Point(df.iloc[x].lon, df.iloc[x].lat)
def prediction_worker(sample, prediction_timedelta):
    predictor = TrajectoryPredictor(sample.past_traj)
    if PREDICTION_MODE == 'linear':
        predicted_location = predictor.predict_linearly(prediction_timedelta)
    elif PREDICTION_MODE == 'kinetic':
        predicted_location = predictor.predict_kinetically(
            prediction_timedelta)
    errors = TrajectoryPredictionEvaluator(sample, predicted_location,
                                           'epsg:25832').get_errors()
    context = sample.past_traj.context
    prediction = EvaluatedPrediction(predicted_location, context, errors)
    return prediction
def get_location_closest_to_linear_prediction(df,
                                              sample,
                                              future,
                                              x='lon',
                                              y='lat'):
    prediction_timedelta = timedelta(minutes=future)
    predictor = TrajectoryPredictor(sample.past_traj)
    predicted_location = predictor.predict_linearly(prediction_timedelta)
    if len(df) == 0:
        return predicted_location
    main_cluster = df.as_matrix(columns=[y, x])
    return get_nearest_cluster_point(main_cluster, predicted_location)
 def test_linear_prediction_south(self):
     df = pd.DataFrame([{
         'geometry': Point(0, 0),
         't': datetime(2018, 1, 1, 12, 0, 0)
     }, {
         'geometry': Point(0, -10),
         't': datetime(2018, 1, 1, 12, 1, 0)
     }]).set_index('t')
     geo_df = GeoDataFrame(df, crs={'init': '4326'})
     traj = Trajectory(1, geo_df)
     predictor = TrajectoryPredictor(traj)
     result = predictor.predict_linearly(timedelta(minutes=1))
     expected_result = Point(0, -20)
     #print("Got {}, expected {}".format(result, expected_result))
     self.assertTrue(result.almost_equals(expected_result))
 def test_kinetic_prediction_turning_right(self):
     df = pd.DataFrame([{
         'geometry': Point(0, 0),
         't': datetime(2018, 1, 1, 12, 0, 0)
     }, {
         'geometry': Point(20, 0),
         't': datetime(2018, 1, 1, 12, 0, 1)
     }, {
         'geometry': Point(38, -10),
         't': datetime(2018, 1, 1, 12, 0, 2)
     }]).set_index('t')
     geo_df = GeoDataFrame(df, crs={'init': '4326'})
     traj = Trajectory(1, geo_df)
     predictor = TrajectoryPredictor(traj)
     result = predictor.predict_kinetically(timedelta(seconds=2))
     expected_result = Point(68.7, -37.3)
     #print("Got {}, expected {}".format(result, expected_result))
     self.assertTrue(result.almost_equals(expected_result, 0))