(no commit message)
[utils] / gps / src / main / java / org / wamblee / gpx / TrackStatistics.java
1 package org.wamblee.gpx;
2
3 import java.io.FileOutputStream;
4 import java.io.IOException;
5 import java.io.OutputStream;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import org.jfree.chart.ChartFactory;
10 import org.jfree.chart.ChartUtilities;
11 import org.jfree.chart.JFreeChart;
12 import org.jfree.chart.plot.PlotOrientation;
13 import org.jfree.data.xy.XYSeries;
14 import org.jfree.data.xy.XYSeriesCollection;
15 import org.wamblee.general.Pair;
16 import org.wamblee.gps.geometry.Point;
17 import org.wamblee.gps.geometry.ReferenceCoordinateSystem;
18 import org.wamblee.gps.track.Track;
19
20 public class TrackStatistics {
21     
22     private Track _track; 
23     
24     public TrackStatistics(Track aTrack) {
25         _track = aTrack; 
26     }
27     
28     public void writeHeightProfileJpg(OutputStream aStream, int aWidth, int aHeight) throws IOException {
29         List<Pair<Double,Double>> data = computeElevationProfile();
30         XYSeriesCollection dataset = createDataset(data, "height");
31         JFreeChart chart = ChartFactory.createXYLineChart(
32                 "Height Profile", 
33                 "Distance(m)",
34                 "Height(m)",
35                 dataset,
36                 PlotOrientation.VERTICAL,
37                 true,
38                 true,
39                 false);
40         ChartUtilities.writeChartAsJPEG(aStream, chart, aWidth, aHeight);
41     }
42     
43     private static XYSeriesCollection createDataset(List<Pair<Double, Double>> aHeightProfile, String aName) {
44         XYSeries series = new XYSeries(aName, false);
45         for (Pair<Double,Double> point: aHeightProfile) { 
46             series.add(point.getFirst()/1000.0, point.getSecond());
47         }
48         XYSeriesCollection dataset = new XYSeriesCollection(series);
49         return dataset;
50     }
51     
52     public List<Pair<Double, Double>> computeElevationProfile() {
53         List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
54         double distance = 0.0; 
55         for (int i = 0; i < _track.size(); i++) { 
56             Point point = _track.getPoint(i);
57             results.add(new Pair<Double,Double>(distance, point.getCoordinates().getX3()));
58             if ( i+1 < _track.size()) { 
59                 Point nextPoint = _track.getPoint(i+1); 
60                 distance += ReferenceCoordinateSystem.distance(point, nextPoint);
61             }
62         }
63         return results; 
64     }
65     
66     public double getTotalClimb() {
67         
68         List<Pair<Double,Double>> data = computeElevationProfile(); 
69         double result = 0.0;
70         
71         double lastHeight = data.get(0).getSecond();
72         for ( int i = 1; i < data.size(); i++) { 
73             double height = data.get(i).getSecond();
74             if ( height > lastHeight) { 
75                 result += (height-lastHeight); 
76             }
77             lastHeight = height; 
78         }
79         return result; 
80     }
81     
82     public double getDistance() { 
83         List<Pair<Double,Double>> data = computeElevationProfile(); 
84         return data.get(data.size()-1).getFirst();
85     }
86 }