8cb03a191d24f8a8e545cbcb36d7fbfdc9949e25
[utils] / gps / src / org / wamblee / gpx / GpxPlotter.java
1 /*
2  * Copyright 2006 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */ 
16
17 package org.wamblee.gpx;
18
19 import java.io.File;
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;
27
28 import javax.xml.parsers.ParserConfigurationException;
29
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;
42
43 /**
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. 
46  */
47 public class GpxPlotter {
48     
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)));
52         
53         Track track = parseTrack(doc);
54     
55         List<Pair<Double,Double>> elevationProfile = computeElevationProfile(track);
56         printTrack(elevationProfile); 
57         computeTotalClimb(elevationProfile);
58         plotElevationProfile(elevationProfile);
59     }
60
61     /**
62      * @param doc
63      */
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));
70         }
71         return track;
72     }
73
74     /**
75      * @param trkpt
76      */
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);
84     }
85     
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);
95             }
96         }
97         return results; 
98     }
99     
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());
103        }
104     }
105     
106     private static void computeTotalClimb(List<Pair<Double,Double>> aHeightProfile) {
107         double result = 0.0;
108         
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); 
114             }
115             lastHeight = height; 
116         }
117         System.out.println("Total climb: " + result);
118     }
119     
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());
124         }
125         XYSeriesCollection dataset = new XYSeriesCollection(series);
126         JFreeChart chart = ChartFactory.createXYLineChart(
127                 "Height Profile", 
128                 "Distance(m)",
129                 "Height(m)",
130                 dataset,
131                 PlotOrientation.VERTICAL,
132                 true,
133                 true,
134                 false);
135         ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 600, 300);
136         ChartFrame frame = new ChartFrame("test", chart);
137         frame.pack();
138         frame.setVisible(true);
139     }
140
141 }
142