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;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
28 import javax.xml.parsers.ParserConfigurationException;
30 import org.dom4j.Document;
31 import org.dom4j.Element;
32 import org.jfree.chart.ChartFactory;
33 import org.jfree.chart.ChartFrame;
34 import org.jfree.chart.ChartUtilities;
35 import org.jfree.chart.JFreeChart;
36 import org.jfree.chart.plot.PlotOrientation;
37 import org.jfree.data.xy.XYSeries;
38 import org.jfree.data.xy.XYSeriesCollection;
39 import org.wamblee.general.Pair;
40 import org.wamblee.xml.DomUtils;
41 import org.xml.sax.SAXException;
44 * Parses a GPX file and prints out a data file with each trackpoints distance from the start of the
45 * track and its elevation, separated by a space.
47 public class GpxPlotter {
49 public static void main(String[] aArgs) throws ParserConfigurationException, FileNotFoundException, SAXException, IOException {
50 File file = new File(aArgs[0]);
51 Document doc = DomUtils.convert(DomUtils.read(new FileInputStream(file)));
53 Track track = parseTrack(doc);
55 List<Pair<Double,Double>> elevationProfile = computeElevationProfile(track);
56 printTrack(elevationProfile);
57 computeTotalClimb(elevationProfile);
58 plotElevationProfile(elevationProfile);
64 private static Track parseTrack(Document doc) {
65 Track track = new Track();
66 Element root = doc.getRootElement().element("trk").element("trkseg");
67 for ( Iterator i =root.elementIterator("trkpt"); i.hasNext(); ) {
68 Element trkpt = (Element)i.next();
69 track.addPoint(parseTrackPoint(trkpt));
77 private static TrackPoint parseTrackPoint(Element trkpt) {
78 //System.out.println(trkpt.asXML() + "|\n");
79 double latitude = new Double(trkpt.attributeValue("lat"));
80 double longitude = new Double(trkpt.attributeValue("lon"));
81 double elevation = new Double(trkpt.elementText("ele"));
82 //System.out.println(" lat = " + lat + " lon = " + lon + " ele = " + ele);
83 return new TrackPoint(latitude, longitude, elevation);
86 private static List<Pair<Double, Double>> computeElevationProfile(Track aTrack) {
87 List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
88 double distance = 0.0;
89 for (int i = 0; i < aTrack.size(); i++) {
90 Point point = aTrack.getPoint(i);
91 results.add(new Pair<Double,Double>(distance, point.getCoordinates().getX3()));
92 if ( i+1 < aTrack.size()) {
93 Point nextPoint = aTrack.getPoint(i+1);
94 distance += ReferenceCoordinateSystem.distance(point, nextPoint);
100 private static void printTrack(List<Pair<Double,Double>> aHeightProfile) {
101 for (Pair<Double,Double> point: aHeightProfile) {
102 System.out.println(point.getFirst() + " " + point.getSecond());
106 private static void computeTotalClimb(List<Pair<Double,Double>> aHeightProfile) {
109 double lastHeight = aHeightProfile.get(0).getSecond();
110 for ( int i = 1; i < aHeightProfile.size(); i++) {
111 double height = aHeightProfile.get(i).getSecond();
112 if ( height > lastHeight) {
113 result += (height-lastHeight);
117 System.out.println("Total climb: " + result);
120 private static void plotElevationProfile(List<Pair<Double,Double>> aHeightProfile) throws IOException {
121 XYSeries series = new XYSeries("height");
122 for (Pair<Double,Double> point: aHeightProfile) {
123 series.add(point.getFirst(), point.getSecond());
125 XYSeriesCollection dataset = new XYSeriesCollection(series);
126 JFreeChart chart = ChartFactory.createXYLineChart(
131 PlotOrientation.VERTICAL,
135 ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 600, 300);
136 ChartFrame frame = new ChartFrame("test", chart);
138 frame.setVisible(true);