19a145d1679dfb69244f9f88a60a3daaebd94966
[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.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;
46
47 /**
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. 
50  */
51 public class GpxPlotter {
52     
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)));
56         
57         Track track = parseTrack(doc);
58     
59         List<Pair<Double,Double>> elevationProfile = computeElevationProfile(track);
60         printTrack(elevationProfile); 
61         computeTotalClimb(elevationProfile);
62         plotElevationProfile(elevationProfile);
63     }
64
65     /**
66      * @param doc
67      */
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));
74         }
75         return track;
76     }
77
78     /**
79      * @param trkpt
80      */
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);
88     }
89     
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);
99             }
100         }
101         return results; 
102     }
103     
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());
107        }
108     }
109     
110     private static void computeTotalClimb(List<Pair<Double,Double>> aHeightProfile) {
111         double result = 0.0;
112         
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); 
118             }
119             lastHeight = height; 
120         }
121         System.out.println("Total climb: " + result);
122     }
123     
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());
128         }
129         XYSeriesCollection dataset = new XYSeriesCollection(series);
130         JFreeChart chart = ChartFactory.createXYLineChart(
131                 "Height Profile", 
132                 "Distance(m)",
133                 "Height(m)",
134                 dataset,
135                 PlotOrientation.VERTICAL,
136                 true,
137                 true,
138                 false);
139         ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 600, 300);
140         ChartFrame frame = new ChartFrame("test", chart);
141         frame.pack();
142         frame.setVisible(true);
143     }
144
145 }
146