2 * Copyright 2006 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.gpx;
19 import java.awt.Color;
20 import java.awt.Image;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.jfree.chart.ChartFactory;
29 import org.jfree.chart.ChartFrame;
30 import org.jfree.chart.ChartUtilities;
31 import org.jfree.chart.JFreeChart;
32 import org.jfree.chart.axis.NumberAxis;
33 import org.jfree.chart.plot.PlotOrientation;
34 import org.jfree.chart.plot.XYPlot;
35 import org.jfree.chart.renderer.xy.XYItemRenderer;
36 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
37 import org.jfree.data.xy.XYSeries;
38 import org.jfree.data.xy.XYSeriesCollection;
39 import org.wamblee.general.Pair;
40 import org.wamblee.gps.geometry.Plane;
41 import org.wamblee.gps.geometry.Point;
42 import org.wamblee.gps.geometry.ReferenceCoordinateSystem;
43 import org.wamblee.gps.track.Track;
44 import org.wamblee.utils.JpegUtils;
47 * Parses a GPX file and prints out a data file with each trackpoints distance from the start of the
48 * track and its elevation, separated 0by a space.
50 public class GpxPlotter {
52 public static void main(String[] aArgs) throws Exception {
53 File file = new File(aArgs[0]);
54 GpxParser parser = new GpxParser();
55 Track track = parser.parse(new FileInputStream(file));
57 List<Pair<Double,Double>> elevationProfile = computeElevationProfile(track);
58 printTrack(elevationProfile);
59 computeTotalClimb(elevationProfile);
60 plotElevationProfile(elevationProfile);
61 List<Pair<Double,Double>> trackXy = computeTrackXY(track);
62 List<Pair<Double,Double>> trackLatLon = computeTrackLatLon(track);
63 plotTrack(trackLatLon);
66 private static List<Pair<Double, Double>> computeElevationProfile(Track aTrack) {
67 List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
68 double distance = 0.0;
69 for (int i = 0; i < aTrack.size(); i++) {
70 Point point = aTrack.getPoint(i);
71 results.add(new Pair<Double,Double>(distance, point.getCoordinates().getX3()));
72 if ( i+1 < aTrack.size()) {
73 Point nextPoint = aTrack.getPoint(i+1);
74 distance += ReferenceCoordinateSystem.distance(point, nextPoint);
80 private static List<Pair<Double, Double>> computeTrackXY(Track aTrack) {
81 Point reference = aTrack.getPoint(0);
82 Plane plane = new Plane(reference, reference); // assume the earth is spherical.
83 List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
84 for (int i = 0; i < aTrack.size(); i++) {
85 Point point = aTrack.getPoint(i);
86 Pair<Double,Double> projection = plane.normalizedProjection(point);
87 results.add(projection);
88 System.out.println(point);
93 private static List<Pair<Double, Double>> computeTrackLatLon(Track aTrack) {
94 List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
95 for (int i = 0; i < aTrack.size(); i++) {
96 Point point = aTrack.getPoint(i);
97 results.add(new Pair<Double,Double>(point.getCoordinates().getX1(), point.getCoordinates().getX2()));
104 private static void printTrack(List<Pair<Double,Double>> aHeightProfile) {
105 for (Pair<Double,Double> point: aHeightProfile) {
106 System.out.println(point.getFirst() + " " + point.getSecond());
110 private static void computeTotalClimb(List<Pair<Double,Double>> aHeightProfile) {
113 double lastHeight = aHeightProfile.get(0).getSecond();
114 for ( int i = 1; i < aHeightProfile.size(); i++) {
115 double height = aHeightProfile.get(i).getSecond();
116 if ( height > lastHeight) {
117 result += (height-lastHeight);
121 System.out.println("Total climb: " + result);
124 private static void plotElevationProfile(List<Pair<Double,Double>> aHeightProfile) throws IOException {
125 XYSeriesCollection dataset = createDataset(aHeightProfile, "height");
126 JFreeChart chart = ChartFactory.createXYLineChart(
131 PlotOrientation.VERTICAL,
135 ChartUtilities.writeChartAsPNG(new FileOutputStream("height.png"), chart, 600, 300);
136 ChartFrame frame = new ChartFrame("test", chart);
138 frame.setVisible(true);
141 private static void plotTrack(List<Pair<Double,Double>> aPoints) throws IOException, InterruptedException {
142 XYSeriesCollection dataset = createDataset(aPoints, "track");
143 JFreeChart chart = createLineChart(dataset);
145 Pair<Pair<Double,Double>,Pair<Double,Double>> bounds = getBounds(aPoints);
147 chart.getXYPlot().getDomainAxis().setLowerBound(bounds.getFirst().getFirst());
148 chart.getXYPlot().getDomainAxis().setUpperBound(bounds.getFirst().getSecond());
150 chart.getXYPlot().getRangeAxis().setLowerBound(bounds.getSecond().getFirst());
151 chart.getXYPlot().getRangeAxis().setUpperBound(bounds.getSecond().getSecond());
153 Image background = JpegUtils.loadJpegImage(new FileInputStream("/home/erik/vakantie.jpg"));
154 chart.getPlot().setBackgroundImage(background);
156 XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
157 renderer.setShapesVisible(true);
158 renderer.setShapesFilled(true);
159 renderer.setPaint(Color.BLACK);
161 ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 1280, 800);
162 ChartFrame frame = new ChartFrame("test", chart);
164 frame.setVisible(true);
171 private static JFreeChart createLineChart(XYSeriesCollection dataset) {
172 NumberAxis xAxis = new NumberAxis("S->N");
173 xAxis.setAutoRangeIncludesZero(false);
175 NumberAxis yAxis = new NumberAxis("W->E");
176 yAxis.setAutoRangeIncludesZero(false);
178 XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
179 XYPlot plot = new ZoomableBackgroundXYPlot(dataset, xAxis, yAxis, renderer);
180 plot.setOrientation(PlotOrientation.HORIZONTAL);
182 JFreeChart chart = new JFreeChart(
183 "Track", JFreeChart.DEFAULT_TITLE_FONT, plot, true
188 JFreeChart chart = ChartFactory.createXYLineChart(
193 PlotOrientation.HORIZONTAL,
202 * @param aHeightProfile
205 private static XYSeriesCollection createDataset(List<Pair<Double, Double>> aHeightProfile, String aName) {
206 XYSeries series = new XYSeries(aName, false);
207 for (Pair<Double,Double> point: aHeightProfile) {
208 series.add(point.getFirst(), point.getSecond());
210 XYSeriesCollection dataset = new XYSeriesCollection(series);
214 private static Pair<Pair<Double,Double>,Pair<Double,Double>> getBounds(List<Pair<Double,Double>> aList) {
215 Pair<Double,Double> first = aList.get(0);
216 double minx= first.getFirst();
218 double miny = first.getSecond();
221 for (int i = 0; i < aList.size(); i++) {
222 Pair<Double,Double> value = aList.get(i);
223 minx = Math.min(minx, value.getFirst());
224 maxx = Math.max(maxx, value.getFirst());
225 miny = Math.min(miny, value.getSecond());
226 maxy = Math.max(maxy, value.getSecond());
228 if ( maxx == minx ) {
229 maxx += 1.0; // to avoid problems.
231 if ( maxy == miny ) {
232 maxy += 1.0; // to avoid problems.
234 final double paddingFactor = 0.3; // allow some space around min and max
235 return new Pair<Pair<Double,Double>,Pair<Double,Double>>(
236 new Pair<Double,Double>( minx - paddingFactor*(maxx-minx),
237 maxx + paddingFactor*(maxx-minx)),
238 new Pair<Double,Double>( miny - paddingFactor*(maxy-miny),
239 maxy + paddingFactor*(maxy-miny))