X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=gps%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fgpx%2FTrackStatistics.java;h=964ac6eb639ae9195e9cd70fe162b1b303942a46;hb=2207a1e695ce23e79678c232cff2ceb84ebaa801;hp=6c4df6cd59ff6cc9670ac49d8309c74b09c763a4;hpb=0b5a5f0cf56868117e63083d8362e565d6478686;p=utils diff --git a/gps/src/main/java/org/wamblee/gpx/TrackStatistics.java b/gps/src/main/java/org/wamblee/gpx/TrackStatistics.java index 6c4df6cd..964ac6eb 100644 --- a/gps/src/main/java/org/wamblee/gpx/TrackStatistics.java +++ b/gps/src/main/java/org/wamblee/gpx/TrackStatistics.java @@ -1,8 +1,10 @@ package org.wamblee.gpx; +import java.awt.Color; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; @@ -15,72 +17,77 @@ 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; +import org.wamblee.gps.track.TrackSegment; -public class TrackStatistics { - - private Track _track; - - public TrackStatistics(Track aTrack) { - _track = aTrack; +public class TrackStatistics implements Serializable { + + private TrackSegment _track; + + public TrackStatistics(TrackSegment aTrack) { + _track = aTrack; } - - public void writeHeightProfileJpg(OutputStream aStream, int aWidth, int aHeight) throws IOException { - List> data = computeElevationProfile(); + + public void writeHeightProfilePng(OutputStream aStream, int aWidth, + int aHeight) throws IOException { + List> 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); + JFreeChart chart = ChartFactory.createXYLineChart("Height Profile", + "Distance(km)", "Height(m)", dataset, PlotOrientation.VERTICAL, + true, true, false); + chart.setBackgroundPaint(Color.WHITE); + ChartUtilities.writeChartAsPNG(aStream, chart, aWidth, aHeight); } - - private static XYSeriesCollection createDataset(List> aHeightProfile, String aName) { + + private static XYSeriesCollection createDataset( + List> aHeightProfile, String aName) { XYSeries series = new XYSeries(aName, false); - for (Pair point: aHeightProfile) { - series.add(point.getFirst()/1000.0, point.getSecond()); + for (Pair point : aHeightProfile) { + series.add(point.getFirst() / 1000.0, point.getSecond()); } XYSeriesCollection dataset = new XYSeriesCollection(series); return dataset; } - + public List> computeElevationProfile() { - List> results = new ArrayList>(); - double distance = 0.0; - for (int i = 0; i < _track.size(); i++) { + List> results = new ArrayList>(); + double distance = 0.0; + for (int i = 0; i < _track.size(); i++) { Point point = _track.getPoint(i); - results.add(new Pair(distance, point.getCoordinates().getX3())); - if ( i+1 < _track.size()) { - Point nextPoint = _track.getPoint(i+1); - distance += ReferenceCoordinateSystem.distance(point, nextPoint); + results.add(new Pair(distance, point + .getCoordinates().getX3())); + if (i + 1 < _track.size()) { + Point nextPoint = _track.getPoint(i + 1); + distance += ReferenceCoordinateSystem + .distance(point, nextPoint); } } - return results; + return results; } - + public double getTotalClimb() { - - List> data = computeElevationProfile(); + + List> data = computeElevationProfile(); double result = 0.0; - + if ( data.size() == 0 ) { + return result; + } + double lastHeight = data.get(0).getSecond(); - for ( int i = 1; i < data.size(); i++) { + for (int i = 1; i < data.size(); i++) { double height = data.get(i).getSecond(); - if ( height > lastHeight) { - result += (height-lastHeight); + if (height > lastHeight) { + result += (height - lastHeight); } - lastHeight = height; + lastHeight = height; } - return result; + return result; } - - public double getDistance() { - List> data = computeElevationProfile(); - return data.get(data.size()-1).getFirst(); + + public double getDistance() { + List> data = computeElevationProfile(); + if (data.size() == 0) { + return 0.0; + } + return data.get(data.size() - 1).getFirst(); } }