(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 20 Sep 2009 10:39:16 +0000 (10:39 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 20 Sep 2009 10:39:16 +0000 (10:39 +0000)
trunk/gps/src/main/java/org/wamblee/gpx/GpxPlotter.java
trunk/gps/src/main/java/org/wamblee/gpx/TrackStatistics.java [new file with mode: 0644]

index a14851013e18bffc44377236d608678cc78c9cf7..232c70139c7ffb594d5c902121281e15a4567dfd 100644 (file)
@@ -55,6 +55,9 @@ public class GpxPlotter {
         File file = new File(aArgs[0]);
         GpxParser parser = new GpxParser(); 
         Track track = parser.parse(new FileInputStream(file));
+        
+        TrackStatistics profile = new TrackStatistics(track);
+        profile.writeHeightProfileJpg(new FileOutputStream("x.jpg"), 600, 300);
     
         List<Pair<Double,Double>> elevationProfile = computeElevationProfile(track);
         printTrack(elevationProfile); 
diff --git a/trunk/gps/src/main/java/org/wamblee/gpx/TrackStatistics.java b/trunk/gps/src/main/java/org/wamblee/gpx/TrackStatistics.java
new file mode 100644 (file)
index 0000000..6c4df6c
--- /dev/null
@@ -0,0 +1,86 @@
+package org.wamblee.gpx;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jfree.chart.ChartFactory;
+import org.jfree.chart.ChartUtilities;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.plot.PlotOrientation;
+import org.jfree.data.xy.XYSeries;
+import org.jfree.data.xy.XYSeriesCollection;
+import org.wamblee.general.Pair;
+import org.wamblee.gps.geometry.Point;
+import org.wamblee.gps.geometry.ReferenceCoordinateSystem;
+import org.wamblee.gps.track.Track;
+
+public class TrackStatistics {
+    
+    private Track _track; 
+    
+    public TrackStatistics(Track aTrack) {
+        _track = aTrack; 
+    }
+    
+    public void writeHeightProfileJpg(OutputStream aStream, int aWidth, int aHeight) throws IOException {
+        List<Pair<Double,Double>> data = computeElevationProfile();
+        XYSeriesCollection dataset = createDataset(data, "height");
+        JFreeChart chart = ChartFactory.createXYLineChart(
+                "Height Profile", 
+                "Distance(m)",
+                "Height(m)",
+                dataset,
+                PlotOrientation.VERTICAL,
+                true,
+                true,
+                false);
+        ChartUtilities.writeChartAsJPEG(aStream, chart, aWidth, aHeight);
+    }
+    
+    private static XYSeriesCollection createDataset(List<Pair<Double, Double>> aHeightProfile, String aName) {
+        XYSeries series = new XYSeries(aName, false);
+        for (Pair<Double,Double> point: aHeightProfile) { 
+            series.add(point.getFirst()/1000.0, point.getSecond());
+        }
+        XYSeriesCollection dataset = new XYSeriesCollection(series);
+        return dataset;
+    }
+    
+    public List<Pair<Double, Double>> computeElevationProfile() {
+        List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
+        double distance = 0.0; 
+        for (int i = 0; i < _track.size(); i++) { 
+            Point point = _track.getPoint(i);
+            results.add(new Pair<Double,Double>(distance, point.getCoordinates().getX3()));
+            if ( i+1 < _track.size()) { 
+                Point nextPoint = _track.getPoint(i+1); 
+                distance += ReferenceCoordinateSystem.distance(point, nextPoint);
+            }
+        }
+        return results; 
+    }
+    
+    public double getTotalClimb() {
+        
+        List<Pair<Double,Double>> data = computeElevationProfile(); 
+        double result = 0.0;
+        
+        double lastHeight = data.get(0).getSecond();
+        for ( int i = 1; i < data.size(); i++) { 
+            double height = data.get(i).getSecond();
+            if ( height > lastHeight) { 
+                result += (height-lastHeight); 
+            }
+            lastHeight = height; 
+        }
+        return result; 
+    }
+    
+    public double getDistance() { 
+        List<Pair<Double,Double>> data = computeElevationProfile(); 
+        return data.get(data.size()-1).getFirst();
+    }
+}