Optimizing Trail Recording

I coded a prototype how we could record fewer GPS positions in Mobile Trail Explorer and therefore save some memory but still have the same level of detail of the trail. Video might explain it best. Unoptimized recording is on the left and optimized trail is on the right.

The algorithm simply checks if the current direction have changed since last recorded position. If direction isn’t changed over the tolerance value then we can replace the last position with current position. Otherwise we only append the new position to the trail.

  1. public boolean canRemovePreviousPosition(
  2.         Vector<GpsPosition> positions,
  3.         GpsPosition pos1) {
  4.  
  5.     if(positions.size()<2) {
  6.         return false;
  7.     }
  8.  
  9.     GpsPosition pos2 = positions.elementAt(
  10.             positions.size()-2 );
  11.     GpsPosition pos3 = positions.elementAt(
  12.             positions.size()-1 );
  13.  
  14.     // Calculate last angle of trail
  15.     double latDelta = pos2.lat – pos3.lat;
  16.     double lonDelta = pos2.lon – pos3.lon;
  17.     double lastAngle = Math.atan(latDelta/lonDelta);
  18.  
  19.     // Calculate current angle
  20.     double latDelta2 = pos3.lat – pos1.lat;
  21.     double lonDelta2 = pos3.lon – pos1.lon;
  22.     double currentAngle =
  23.             Math.atan(latDelta2/lonDelta2);
  24.  
  25.     // Get absolute value of direction change
  26.     double angleDelta =
  27.             Math.abs(lastAngle – currentAngle);
  28.  
  29.     // Check the tolerance (0.105 radians = 6 degrees)
  30.     if(angleDelta>0.105) {
  31.         return false;
  32.     } else {
  33.         return true;
  34.     }
  35. }

  • Paul

    What about the 3rd dimension

  • http://www.substanceofcode.com Tommi Laukkanen

    @Paul: Yes, you could add the altitude to this equation but since GPS doesn’t often give that precise altitude information I didn’t include it. That could be configurable option to optimization.

  • Paul

    Having a separate stream for latitude would be very nice. With optional period, minimum delta, filtering…

  • http://www.substanceofcode.com Tommi Laukkanen

    @Paul: Yes, it would be a good idea to make the minimum delta value as configurable. You can test the filtering in the latest beta version of MTE as I just implemented this to the trunk just few days ago.

  • Phil

    Hi Tommi hi all,
    Firstly i would like to say you that your softs are wonderfull.

    For few new phone with accelerometer it should be possible to use accelerometer for filter the GPS data?

    Best regards

  • http://www.substanceofcode.com Tommi Laukkanen

    @Phil: Thanks! Good idea to use the accelerometer for filtering. Basically we could could only record points when acceleration is noticed.

  • mpele

    I am not so shure about mathematical values, but maybe it would be better to be:

    if(angleDelta>0.105 and angleDelta < 6.28-0.105)

    Diference between 2 and 358 degrees is 4 deegree and it should not trigger recording.