(no commit message)
[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.awt.Color;
20 import java.awt.Graphics2D;
21 import java.awt.Image;
22 import java.io.File;
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;
30
31 import javax.xml.parsers.ParserConfigurationException;
32
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;
58
59 /**
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. 
62  */
63 public class GpxPlotter {
64     
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)));
68         
69         Track track = parseTrack(doc);
70     
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);
76         plotTrack(trackXy);
77     }
78
79     /**
80      * @param doc
81      */
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));
88         }
89         return track;
90     }
91
92     /**
93      * @param trkpt
94      */
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);
102     }
103     
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);
113             }
114         }
115         return results; 
116     }
117     
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);
127         }
128         return results; 
129     }
130     
131     
132     
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());
136        }
137     }
138     
139     private static void computeTotalClimb(List<Pair<Double,Double>> aHeightProfile) {
140         double result = 0.0;
141         
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); 
147             }
148             lastHeight = height; 
149         }
150         System.out.println("Total climb: " + result);
151     }
152     
153     private static void plotElevationProfile(List<Pair<Double,Double>> aHeightProfile) throws IOException {
154         XYSeriesCollection dataset = createDataset(aHeightProfile, "height");
155         JFreeChart chart = ChartFactory.createXYLineChart(
156                 "Height Profile", 
157                 "Distance(m)",
158                 "Height(m)",
159                 dataset,
160                 PlotOrientation.VERTICAL,
161                 true,
162                 true,
163                 false);
164         ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 600, 300);
165         ChartFrame frame = new ChartFrame("test", chart);
166         frame.pack();
167         frame.setVisible(true);
168     }
169     
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();
174         
175         Image background = JpegUtils.loadJpegImage(new FileInputStream("/home/erik/vakantie.jpg"));
176         chart.getPlot().setBackgroundImage(background);
177        
178         
179         XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
180         renderer.setShapesVisible(true);
181         renderer.setShapesFilled(true);
182         renderer.setPaint(Color.BLACK);
183         
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());
187      
188         ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 600, 300);
189         ChartFrame frame = new ChartFrame("test", chart);
190         frame.pack();
191         frame.setVisible(true);
192     }
193
194     /**
195      * @param dataset
196      * @return
197      */
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);
205       
206         JFreeChart chart = new JFreeChart(
207             "Track", JFreeChart.DEFAULT_TITLE_FONT, plot, true
208         );
209
210         return chart;
211         /*
212         JFreeChart chart = ChartFactory.createXYLineChart(
213                 "Track", 
214                 "S->N",
215                 "W->E",
216                 dataset,
217                 PlotOrientation.HORIZONTAL,
218                 true,
219                 true,
220                 false);
221         return chart;
222         */
223     }
224
225     /**
226      * @param aHeightProfile
227      * @return
228      */
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());
233         }
234         XYSeriesCollection dataset = new XYSeriesCollection(series);
235         return dataset;
236     }
237
238 }
239