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.gps.coordinates.Point;
41 import org.wamblee.gps.coordinates.ReferenceCoordinateSystem;
42 import org.wamblee.gps.track.Track;
43 import org.wamblee.gps.track.TrackPoint;
44 import org.wamblee.xml.DomUtils;
45 import org.xml.sax.SAXException;
48 * Parses a GPX file and prints out a data file with each trackpoints distance from the start of the
49 * track and its elevation, separated by a space.
51 public class GpxPlotter {
53 public static void main(String[] aArgs) throws ParserConfigurationException, FileNotFoundException, SAXException, IOException {
54 File file = new File(aArgs[0]);
55 Document doc = DomUtils.convert(DomUtils.read(new FileInputStream(file)));
57 Track track = parseTrack(doc);
59 List<Pair<Double,Double>> elevationProfile = computeElevationProfile(track);
60 printTrack(elevationProfile);
61 computeTotalClimb(elevationProfile);
62 plotElevationProfile(elevationProfile);
68 private static Track parseTrack(Document doc) {
69 Track track = new Track();
70 Element root = doc.getRootElement().element("trk").element("trkseg");
71 for ( Iterator i =root.elementIterator("trkpt"); i.hasNext(); ) {
72 Element trkpt = (Element)i.next();
73 track.addPoint(parseTrackPoint(trkpt));
81 private static TrackPoint parseTrackPoint(Element trkpt) {
82 //System.out.println(trkpt.asXML() + "|\n");
83 double latitude = new Double(trkpt.attributeValue("lat"));
84 double longitude = new Double(trkpt.attributeValue("lon"));
85 double elevation = new Double(trkpt.elementText("ele"));
86 //System.out.println(" lat = " + lat + " lon = " + lon + " ele = " + ele);
87 return new TrackPoint(latitude, longitude, elevation);
90 private static List<Pair<Double, Double>> computeElevationProfile(Track aTrack) {
91 List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
92 double distance = 0.0;
93 for (int i = 0; i < aTrack.size(); i++) {
94 Point point = aTrack.getPoint(i);
95 results.add(new Pair<Double,Double>(distance, point.getCoordinates().getX3()));
96 if ( i+1 < aTrack.size()) {
97 Point nextPoint = aTrack.getPoint(i+1);
98 distance += ReferenceCoordinateSystem.distance(point, nextPoint);
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 XYSeries series = new XYSeries("height");
126 for (Pair<Double,Double> point: aHeightProfile) {
127 series.add(point.getFirst(), point.getSecond());
129 XYSeriesCollection dataset = new XYSeriesCollection(series);
130 JFreeChart chart = ChartFactory.createXYLineChart(
135 PlotOrientation.VERTICAL,
139 ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 600, 300);
140 ChartFrame frame = new ChartFrame("test", chart);
142 frame.setVisible(true);