Articles · before 2018

A Simple Way to Find Turning points for a Trajectory with Python

Using Ramer-Douglas-Peucker algorithm (or RDP) that provides piecewise approximations, construct an approximated trajectory and find "valuable" turning points.

Archived article, first published before 2018. Kept as it was written; some tools and versions have moved on since.

A trajectory is the path that a moving object follows through space as a function of time. Usually, we work with its discrete representation by calculating space positions with some interval.

For simplicity, let's consider only 2-dimensional space and ignore possible changes in Z coordinate. Therefore, our trajectory is a time series of data points (x, y) that represent position on a plane XY and have been calculated with some time interval.

Plot of a sample trajectory (path) on the XY plane

A turning point is just a changes in direction. Such points are valuable features for the trajectory analysis because they can define trajectories in a space (imaging you want to explain a path to someone, then you usually describe it in such turning points: "turn left, go 20 meters and turn right, go straight until you reach Starbucks, then turn right again.").

It's quite easy to find such turning points for humans on a plot, but a bit more tricky to do that automatically. A human brain can also quite fast detect valuable turning points when change in direction is significant (e.g. turning to another street in contrast to changing just a traffic lane).

In order to let machine make such analysis we need build a simple approximation to our trajectory curve where we can highlight such fractures. The best choice for this is Ramer-Douglas-Peucker algorithm (or RDP) that gives us a piecewise approximation to our trajectory curve where every fracture could be treated as a turning point.

Original trajectory and its simplified version produced by the RDP algorithm

Trajectory can have many small noisy turns (e.g. due to calculation errors or just by representing a traffic lane change). Same as our brain do that, we want machine to filter them out too. As one of ways to do so, we can consider as a "valuable" only such significant turn whose angle is bigger than some minimal value (for instance, we have selected π5, but you might find another value for your use case and data).

Simplified trajectory with the valuable turning points highlighted

In the code below we use rpd 0.6 - pure Python implementation of the Ramer-Douglas-Peucker algorithm, and build simplified (or approximated) trajectory and compare it to the original curve.

find_turning_points.py
from rdp import rdp

def angle(directions):
    """Return the angle between vectors
    """
    vec2 = directions[1:]
    vec1 = directions[:-1]

    norm1 = np.sqrt((vec1 ** 2).sum(axis=1))
    norm2 = np.sqrt((vec2 ** 2).sum(axis=1))
    cos = (vec1 * vec2).sum(axis=1) / (norm1 * norm2)
    return np.arccos(cos)


# Build simplified (approximated) trajectory
# using RDP algorithm.
simplified_trajectory = rdp(trajectory, epsilon=200)
sx, sy = simplified_trajectory.T

# Visualize trajectory and its simplified version.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, 'r--', label='trajectory')
ax.plot(sx, sy, 'b-', label='simplified trajectory')
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.legend(loc='best')

# Define a minimum angle to treat change in direction
# as significant (valuable turning point).
min_angle = np.pi / 5.0

# Compute the direction vectors on the simplified_trajectory.
directions = np.diff(simplified_trajectory, axis=0)
theta = angle(directions)

# Select the index of the points with the greatest theta.
# Large theta is associated with greatest change in direction.
idx = np.where(theta > min_angle)[0] + 1

# Visualize valuable turning points on the simplified trjectory.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(sx, sy, 'gx-', label='simplified trajectory')
ax.plot(sx[idx], sy[idx], 'ro', markersize = 7, label='turning points')
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.legend(loc='best')

Read More

  1. Ramer-Douglas-Peucker algorithm (RDP) is an algorithm for reducing the number of points in a curve that is approximated by a series of points.