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