/**
  * Adjusts an Itinerary's elevation fields from an elevation profile
  *
  * @return the elevation at the end of the profile
  */
 private double applyElevation(
     PackedCoordinateSequence profile, Itinerary itinerary, double previousElevation) {
   if (profile != null) {
     for (Coordinate coordinate : profile.toCoordinateArray()) {
       if (previousElevation == Double.MAX_VALUE) {
         previousElevation = coordinate.y;
         continue;
       }
       double elevationChange = previousElevation - coordinate.y;
       if (elevationChange > 0) {
         itinerary.elevationGained += elevationChange;
       } else {
         itinerary.elevationLost -= elevationChange;
       }
       previousElevation = coordinate.y;
     }
   }
   return previousElevation;
 }