(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.Image;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.jfree.chart.ChartFactory;
29 import org.jfree.chart.ChartFrame;
30 import org.jfree.chart.ChartUtilities;
31 import org.jfree.chart.JFreeChart;
32 import org.jfree.chart.axis.NumberAxis;
33 import org.jfree.chart.plot.PlotOrientation;
34 import org.jfree.chart.plot.XYPlot;
35 import org.jfree.chart.renderer.xy.XYItemRenderer;
36 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
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.geometry.Plane;
41 import org.wamblee.gps.geometry.Point;
42 import org.wamblee.gps.geometry.ReferenceCoordinateSystem;
43 import org.wamblee.gps.track.Track;
44 import org.wamblee.utils.JpegUtils;
45
46 /**
47  * Parses a GPX file and prints out a data file with each trackpoints distance from the start of the 
48  * track and its elevation, separated 0by a space. 
49  */
50 public class GpxPlotter {
51     
52     public static void main(String[] aArgs) throws Exception { 
53         File file = new File(aArgs[0]);
54         GpxParser parser = new GpxParser(); 
55         Track track = parser.parse(new FileInputStream(file));
56     
57         List<Pair<Double,Double>> elevationProfile = computeElevationProfile(track);
58         printTrack(elevationProfile); 
59         computeTotalClimb(elevationProfile);
60         plotElevationProfile(elevationProfile);
61         List<Pair<Double,Double>> trackXy = computeTrackXY(track);
62         List<Pair<Double,Double>> trackLatLon = computeTrackLatLon(track);
63         plotTrack(trackLatLon);
64     }
65     
66     private static List<Pair<Double, Double>> computeElevationProfile(Track aTrack) {
67         List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
68         double distance = 0.0; 
69         for (int i = 0; i < aTrack.size(); i++) { 
70             Point point = aTrack.getPoint(i);
71             results.add(new Pair<Double,Double>(distance, point.getCoordinates().getX3()));
72             if ( i+1 < aTrack.size()) { 
73                 Point nextPoint = aTrack.getPoint(i+1); 
74                 distance += ReferenceCoordinateSystem.distance(point, nextPoint);
75             }
76         }
77         return results; 
78     }
79     
80     private static List<Pair<Double, Double>> computeTrackXY(Track aTrack) {
81         Point reference = aTrack.getPoint(0);
82         Plane plane = new Plane(reference, reference); // assume the earth is spherical.
83         List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
84         for (int i = 0; i < aTrack.size(); i++) { 
85             Point point = aTrack.getPoint(i);
86             Pair<Double,Double> projection = plane.normalizedProjection(point);
87             results.add(projection);
88             System.out.println(point);
89         }
90         return results; 
91     }
92     
93     private static List<Pair<Double, Double>> computeTrackLatLon(Track aTrack) {
94         List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
95         for (int i = 0; i < aTrack.size(); i++) { 
96             Point point = aTrack.getPoint(i);
97             results.add(new Pair<Double,Double>(point.getCoordinates().getX1(), point.getCoordinates().getX2()));
98         }
99         return results; 
100     }
101     
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         XYSeriesCollection dataset = createDataset(aHeightProfile, "height");
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("height.png"), chart, 600, 300);
136         ChartFrame frame = new ChartFrame("test", chart);
137         frame.pack();
138         frame.setVisible(true);
139     }
140     
141     private static void plotTrack(List<Pair<Double,Double>> aPoints) throws IOException, InterruptedException {
142         XYSeriesCollection dataset = createDataset(aPoints, "track");
143         JFreeChart chart = createLineChart(dataset);
144         
145         Pair<Pair<Double,Double>,Pair<Double,Double>> bounds = getBounds(aPoints); 
146         
147         chart.getXYPlot().getDomainAxis().setLowerBound(bounds.getFirst().getFirst());
148         chart.getXYPlot().getDomainAxis().setUpperBound(bounds.getFirst().getSecond());
149         
150         chart.getXYPlot().getRangeAxis().setLowerBound(bounds.getSecond().getFirst());
151         chart.getXYPlot().getRangeAxis().setUpperBound(bounds.getSecond().getSecond());
152         
153         Image background = JpegUtils.loadJpegImage(new FileInputStream("/home/erik/vakantie.jpg"));
154         chart.getPlot().setBackgroundImage(background);
155        
156         XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
157         renderer.setShapesVisible(true);
158         renderer.setShapesFilled(true);
159         renderer.setPaint(Color.BLACK);
160      
161         ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 1280, 800);
162         ChartFrame frame = new ChartFrame("test", chart);
163         frame.pack();
164         frame.setVisible(true);
165     }
166
167     /**
168      * @param dataset
169      * @return
170      */
171     private static JFreeChart createLineChart(XYSeriesCollection dataset) {
172         NumberAxis xAxis = new NumberAxis("S->N");
173         xAxis.setAutoRangeIncludesZero(false);
174         
175         NumberAxis yAxis = new NumberAxis("W->E");
176         yAxis.setAutoRangeIncludesZero(false);
177        
178         XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
179         XYPlot plot = new ZoomableBackgroundXYPlot(dataset, xAxis, yAxis, renderer);
180         plot.setOrientation(PlotOrientation.HORIZONTAL);
181       
182         JFreeChart chart = new JFreeChart(
183             "Track", JFreeChart.DEFAULT_TITLE_FONT, plot, true
184         );
185
186         return chart;
187         /*
188         JFreeChart chart = ChartFactory.createXYLineChart(
189                 "Track", 
190                 "S->N",
191                 "W->E",
192                 dataset,
193                 PlotOrientation.HORIZONTAL,
194                 true,
195                 true,
196                 false);
197         return chart;
198         */
199     }
200
201     /**
202      * @param aHeightProfile
203      * @return
204      */
205     private static XYSeriesCollection createDataset(List<Pair<Double, Double>> aHeightProfile, String aName) {
206         XYSeries series = new XYSeries(aName, false);
207         for (Pair<Double,Double> point: aHeightProfile) { 
208             series.add(point.getFirst(), point.getSecond());
209         }
210         XYSeriesCollection dataset = new XYSeriesCollection(series);
211         return dataset;
212     }
213
214     private static Pair<Pair<Double,Double>,Pair<Double,Double>> getBounds(List<Pair<Double,Double>> aList) { 
215         Pair<Double,Double> first = aList.get(0); 
216         double minx= first.getFirst();
217         double maxx = minx;
218         double miny = first.getSecond();
219         double maxy = miny;
220         
221         for (int i = 0; i < aList.size(); i++) { 
222             Pair<Double,Double> value = aList.get(i);
223             minx = Math.min(minx, value.getFirst());
224             maxx = Math.max(maxx, value.getFirst());
225             miny = Math.min(miny, value.getSecond());
226             maxy = Math.max(maxy, value.getSecond());
227         }
228         if ( maxx == minx ) { 
229             maxx += 1.0; // to avoid problems. 
230         }
231         if ( maxy == miny ) { 
232             maxy += 1.0; // to avoid problems.
233         }
234         final double paddingFactor = 0.3; // allow some space around min and max
235         return new Pair<Pair<Double,Double>,Pair<Double,Double>>(
236                 new Pair<Double,Double>( minx - paddingFactor*(maxx-minx), 
237                                          maxx + paddingFactor*(maxx-minx)),
238                 new Pair<Double,Double>( miny - paddingFactor*(maxy-miny), 
239                                          maxy + paddingFactor*(maxy-miny))
240                 );
241     }
242 }
243