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.Graphics2D;
21 import java.awt.Image;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
31 import javax.xml.parsers.ParserConfigurationException;
33 import org.dom4j.Document;
34 import org.dom4j.Element;
35 import org.jfree.chart.ChartFactory;
36 import org.jfree.chart.ChartFrame;
37 import org.jfree.chart.ChartUtilities;
38 import org.jfree.chart.JFreeChart;
39 import org.jfree.chart.axis.NumberAxis;
40 import org.jfree.chart.labels.StandardXYToolTipGenerator;
41 import org.jfree.chart.plot.PlotOrientation;
42 import org.jfree.chart.plot.XYPlot;
43 import org.jfree.chart.renderer.xy.XYItemRenderer;
44 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
45 import org.jfree.chart.urls.StandardXYURLGenerator;
46 import org.jfree.data.Range;
47 import org.jfree.data.xy.XYSeries;
48 import org.jfree.data.xy.XYSeriesCollection;
49 import org.wamblee.general.Pair;
50 import org.wamblee.gps.geometry.Plane;
51 import org.wamblee.gps.geometry.Point;
52 import org.wamblee.gps.geometry.ReferenceCoordinateSystem;
53 import org.wamblee.gps.track.Track;
54 import org.wamblee.gps.track.TrackPoint;
55 import org.wamblee.utils.JpegUtils;
56 import org.wamblee.xml.DomUtils;
57 import org.xml.sax.SAXException;
60 * Parses a GPX file and prints out a data file with each trackpoints distance from the start of the
61 * track and its elevation, separated by a space.
63 public class GpxPlotter {
65 public static void main(String[] aArgs) throws Exception {
66 File file = new File(aArgs[0]);
67 Document doc = DomUtils.convert(DomUtils.read(new FileInputStream(file)));
69 Track track = parseTrack(doc);
71 List<Pair<Double,Double>> elevationProfile = computeElevationProfile(track);
72 printTrack(elevationProfile);
73 computeTotalClimb(elevationProfile);
74 plotElevationProfile(elevationProfile);
75 List<Pair<Double,Double>> trackXy = computeTrackXY(track);
82 private static Track parseTrack(Document doc) {
83 Track track = new Track();
84 Element root = doc.getRootElement().element("trk").element("trkseg");
85 for ( Iterator i =root.elementIterator("trkpt"); i.hasNext(); ) {
86 Element trkpt = (Element)i.next();
87 track.addPoint(parseTrackPoint(trkpt));
95 private static TrackPoint parseTrackPoint(Element trkpt) {
96 //System.out.println(trkpt.asXML() + "|\n");
97 double latitude = new Double(trkpt.attributeValue("lat"));
98 double longitude = new Double(trkpt.attributeValue("lon"));
99 double elevation = new Double(trkpt.elementText("ele"));
100 //System.out.println(" lat = " + lat + " lon = " + lon + " ele = " + ele);
101 return new TrackPoint(latitude, longitude, elevation);
104 private static List<Pair<Double, Double>> computeElevationProfile(Track aTrack) {
105 List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
106 double distance = 0.0;
107 for (int i = 0; i < aTrack.size(); i++) {
108 Point point = aTrack.getPoint(i);
109 results.add(new Pair<Double,Double>(distance, point.getCoordinates().getX3()));
110 if ( i+1 < aTrack.size()) {
111 Point nextPoint = aTrack.getPoint(i+1);
112 distance += ReferenceCoordinateSystem.distance(point, nextPoint);
118 private static List<Pair<Double, Double>> computeTrackXY(Track aTrack) {
119 Point reference = aTrack.getPoint(0);
120 Plane plane = new Plane(reference, reference); // assume the earth is spherical.
121 List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
122 for (int i = 0; i < aTrack.size(); i++) {
123 Point point = aTrack.getPoint(i);
124 Pair<Double,Double> projection = plane.normalizedProjection(point);
125 results.add(projection);
126 System.out.println(point);
133 private static void printTrack(List<Pair<Double,Double>> aHeightProfile) {
134 for (Pair<Double,Double> point: aHeightProfile) {
135 System.out.println(point.getFirst() + " " + point.getSecond());
139 private static void computeTotalClimb(List<Pair<Double,Double>> aHeightProfile) {
142 double lastHeight = aHeightProfile.get(0).getSecond();
143 for ( int i = 1; i < aHeightProfile.size(); i++) {
144 double height = aHeightProfile.get(i).getSecond();
145 if ( height > lastHeight) {
146 result += (height-lastHeight);
150 System.out.println("Total climb: " + result);
153 private static void plotElevationProfile(List<Pair<Double,Double>> aHeightProfile) throws IOException {
154 XYSeriesCollection dataset = createDataset(aHeightProfile, "height");
155 JFreeChart chart = ChartFactory.createXYLineChart(
160 PlotOrientation.VERTICAL,
164 ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 600, 300);
165 ChartFrame frame = new ChartFrame("test", chart);
167 frame.setVisible(true);
170 private static void plotTrack(List<Pair<Double,Double>> aPoints) throws IOException, InterruptedException {
171 XYSeriesCollection dataset = createDataset(aPoints, "track");
172 JFreeChart chart = createLineChart(dataset);
173 Range range = chart.getXYPlot().getRangeAxis().getRange();
175 Image background = JpegUtils.loadJpegImage(new FileInputStream("/home/erik/vakantie.jpg"));
176 chart.getPlot().setBackgroundImage(background);
179 XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
180 renderer.setShapesVisible(true);
181 renderer.setShapesFilled(true);
182 renderer.setPaint(Color.BLACK);
184 //renderer.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
185 // 1.0f, new float[] { 6.0f, 6.0f}, 0.0f));
186 System.out.println("Range: " + range.getLowerBound() + " " + range.getUpperBound());
188 ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 600, 300);
189 ChartFrame frame = new ChartFrame("test", chart);
191 frame.setVisible(true);
198 private static JFreeChart createLineChart(XYSeriesCollection dataset) {
199 NumberAxis xAxis = new NumberAxis("S->N");
200 xAxis.setAutoRangeIncludesZero(false);
201 NumberAxis yAxis = new NumberAxis("W->E");
202 XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
203 XYPlot plot = new ZoomableBackgroundXYPlot(dataset, xAxis, yAxis, renderer);
204 plot.setOrientation(PlotOrientation.HORIZONTAL);
206 JFreeChart chart = new JFreeChart(
207 "Track", JFreeChart.DEFAULT_TITLE_FONT, plot, true
212 JFreeChart chart = ChartFactory.createXYLineChart(
217 PlotOrientation.HORIZONTAL,
226 * @param aHeightProfile
229 private static XYSeriesCollection createDataset(List<Pair<Double, Double>> aHeightProfile, String aName) {
230 XYSeries series = new XYSeries(aName, false);
231 for (Pair<Double,Double> point: aHeightProfile) {
232 series.add(point.getFirst(), point.getSecond());
234 XYSeriesCollection dataset = new XYSeriesCollection(series);