- // Moving gradient and moving climb/descent
- Altitude firstAlt = null, lastAlt = null;
- Altitude veryFirstAlt = null, veryLastAlt = null;
- AltitudeRange altRange = new AltitudeRange();
- double movingHeightDiff = 0.0;
- if (movingDist > 0.0)
- {
- for (int pNum = selection.getStart(); pNum <= selection.getEnd(); pNum++)
- {
- DataPoint p = _app.getTrackInfo().getTrack().getPoint(pNum);
- if (p != null && !p.isWaypoint())
- {
- // If we're starting a new segment, calculate the height diff of the previous one
- if (p.getSegmentStart())
- {
- if (firstAlt != null && firstAlt.isValid() && lastAlt != null && lastAlt.isValid())
- movingHeightDiff = movingHeightDiff + lastAlt.getMetricValue() - firstAlt.getMetricValue();
- firstAlt = null; lastAlt = null;
- }
- Altitude alt = p.getAltitude();
- if (alt != null && alt.isValid())
- {
- if (firstAlt == null) firstAlt = alt;
- else lastAlt = alt;
- if (veryFirstAlt == null) veryFirstAlt = alt;
- else veryLastAlt = alt;
- }
- // Keep track of climb and descent too
- if (p.getSegmentStart())
- altRange.ignoreValue(alt);
- else
- altRange.addValue(alt);
- }
- }
- // deal with last segment
- if (firstAlt != null && firstAlt.isValid() && lastAlt != null && lastAlt.isValid())
- movingHeightDiff = movingHeightDiff + lastAlt.getMetricValue() - firstAlt.getMetricValue();
- final double metricMovingDist = movingDist / distUnit.getMultFactorFromStd(); // convert back to metres
- final double gradient = movingHeightDiff * 100.0 / metricMovingDist;
- _movingGradientLabel.setText(FORMAT_ONE_DP.format(gradient) + " %");
- }
- if (!altRange.hasRange()) {
- _movingGradientLabel.setText("");
- }
- final boolean hasAltitudes = veryFirstAlt != null && veryFirstAlt.isValid() && veryLastAlt != null && veryLastAlt.isValid();
-
- // Total gradient
- final double metreDist = selection.getDistance() / distUnit.getMultFactorFromStd(); // convert back to metres
- if (hasAltitudes && metreDist > 0.0)
- {
- // got an altitude and range
- int altDiffInMetres = veryLastAlt.getValue(Altitude.Format.METRES) - veryFirstAlt.getValue(Altitude.Format.METRES);
- double gradient = altDiffInMetres * 100.0 / metreDist;
- _totalGradientLabel.setText(FORMAT_ONE_DP.format(gradient) + " %");