(no commit message)
[utils] / gps / src / org / wamblee / gpx / GpxPlotter.java
index 19a145d1679dfb69244f9f88a60a3daaebd94966..f239cabf4a3b7aadc01807f000f0ea0caf45b3dd 100644 (file)
 
 package org.wamblee.gpx;
 
+import java.awt.Color;
+import java.awt.Image;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 
-import javax.xml.parsers.ParserConfigurationException;
-
-import org.dom4j.Document;
-import org.dom4j.Element;
 import org.jfree.chart.ChartFactory;
 import org.jfree.chart.ChartFrame;
 import org.jfree.chart.ChartUtilities;
 import org.jfree.chart.JFreeChart;
+import org.jfree.chart.axis.NumberAxis;
 import org.jfree.chart.plot.PlotOrientation;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.renderer.xy.XYItemRenderer;
+import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
 import org.jfree.data.xy.XYSeries;
 import org.jfree.data.xy.XYSeriesCollection;
 import org.wamblee.general.Pair;
-import org.wamblee.gps.coordinates.Point;
-import org.wamblee.gps.coordinates.ReferenceCoordinateSystem;
+import org.wamblee.gps.geometry.Plane;
+import org.wamblee.gps.geometry.Point;
+import org.wamblee.gps.geometry.ReferenceCoordinateSystem;
 import org.wamblee.gps.track.Track;
-import org.wamblee.gps.track.TrackPoint;
-import org.wamblee.xml.DomUtils;
-import org.xml.sax.SAXException;
+import org.wamblee.utils.JpegUtils;
 
 /**
  * Parses a GPX file and prints out a data file with each trackpoints distance from the start of the 
- * track and its elevation, separated by a space. 
+ * track and its elevation, separated 0by a space. 
  */
 public class GpxPlotter {
     
-    public static void main(String[] aArgs) throws ParserConfigurationException, FileNotFoundException, SAXException, IOException { 
-        File file = new File(aArgs[0]); 
-        Document doc = DomUtils.convert(DomUtils.read(new FileInputStream(file)));
-        
-        Track track = parseTrack(doc);
+    public static void main(String[] aArgs) throws Exception { 
+        File file = new File(aArgs[0]);
+        GpxParser parser = new GpxParser(); 
+        Track track = parser.parse(new FileInputStream(file));
     
         List<Pair<Double,Double>> elevationProfile = computeElevationProfile(track);
         printTrack(elevationProfile); 
         computeTotalClimb(elevationProfile);
         plotElevationProfile(elevationProfile);
-    }
-
-    /**
-     * @param doc
-     */
-    private static Track parseTrack(Document doc) {
-        Track track = new Track(); 
-        Element root = doc.getRootElement().element("trk").element("trkseg");
-        for ( Iterator i =root.elementIterator("trkpt"); i.hasNext(); ) {
-            Element trkpt = (Element)i.next();
-            track.addPoint(parseTrackPoint(trkpt));
-        }
-        return track;
-    }
-
-    /**
-     * @param trkpt
-     */
-    private static TrackPoint parseTrackPoint(Element trkpt) {
-        //System.out.println(trkpt.asXML() + "|\n"); 
-        double latitude = new Double(trkpt.attributeValue("lat"));
-        double longitude = new Double(trkpt.attributeValue("lon"));
-        double elevation = new Double(trkpt.elementText("ele"));
-        //System.out.println("  lat = " + lat + " lon = " + lon + " ele = " + ele);
-        return new TrackPoint(latitude, longitude, elevation);
+        List<Pair<Double,Double>> trackXy = computeTrackXY(track);
+        List<Pair<Double,Double>> trackLatLon = computeTrackLatLon(track);
+        plotTrack(trackLatLon);
     }
     
     private static List<Pair<Double, Double>> computeElevationProfile(Track aTrack) {
@@ -101,6 +77,30 @@ public class GpxPlotter {
         return results; 
     }
     
+    private static List<Pair<Double, Double>> computeTrackXY(Track aTrack) {
+        Point reference = aTrack.getPoint(0);
+        Plane plane = new Plane(reference, reference); // assume the earth is spherical.
+        List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
+        for (int i = 0; i < aTrack.size(); i++) { 
+            Point point = aTrack.getPoint(i);
+            Pair<Double,Double> projection = plane.normalizedProjection(point);
+            results.add(projection);
+            System.out.println(point);
+        }
+        return results; 
+    }
+    
+    private static List<Pair<Double, Double>> computeTrackLatLon(Track aTrack) {
+        List<Pair<Double,Double>> results = new ArrayList<Pair<Double,Double>>();
+        for (int i = 0; i < aTrack.size(); i++) { 
+            Point point = aTrack.getPoint(i);
+            results.add(new Pair<Double,Double>(point.getCoordinates().getX1(), point.getCoordinates().getX2()));
+        }
+        return results; 
+    }
+    
+    
+    
     private static void printTrack(List<Pair<Double,Double>> aHeightProfile) { 
        for (Pair<Double,Double> point: aHeightProfile) { 
            System.out.println(point.getFirst() + " " + point.getSecond());
@@ -122,11 +122,7 @@ public class GpxPlotter {
     }
     
     private static void plotElevationProfile(List<Pair<Double,Double>> aHeightProfile) throws IOException {
-        XYSeries series = new XYSeries("height");
-        for (Pair<Double,Double> point: aHeightProfile) { 
-            series.add(point.getFirst(), point.getSecond());
-        }
-        XYSeriesCollection dataset = new XYSeriesCollection(series);
+        XYSeriesCollection dataset = createDataset(aHeightProfile, "height");
         JFreeChart chart = ChartFactory.createXYLineChart(
                 "Height Profile", 
                 "Distance(m)",
@@ -136,11 +132,112 @@ public class GpxPlotter {
                 true,
                 true,
                 false);
-        ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 600, 300);
+        ChartUtilities.writeChartAsPNG(new FileOutputStream("height.png"), chart, 600, 300);
+        ChartFrame frame = new ChartFrame("test", chart);
+        frame.pack();
+        frame.setVisible(true);
+    }
+    
+    private static void plotTrack(List<Pair<Double,Double>> aPoints) throws IOException, InterruptedException {
+        XYSeriesCollection dataset = createDataset(aPoints, "track");
+        JFreeChart chart = createLineChart(dataset);
+        
+        Pair<Pair<Double,Double>,Pair<Double,Double>> bounds = getBounds(aPoints); 
+        
+        chart.getXYPlot().getDomainAxis().setLowerBound(bounds.getFirst().getFirst());
+        chart.getXYPlot().getDomainAxis().setUpperBound(bounds.getFirst().getSecond());
+        
+        chart.getXYPlot().getRangeAxis().setLowerBound(bounds.getSecond().getFirst());
+        chart.getXYPlot().getRangeAxis().setUpperBound(bounds.getSecond().getSecond());
+        
+        Image background = JpegUtils.loadJpegImage(new FileInputStream("/home/erik/vakantie.jpg"));
+        chart.getPlot().setBackgroundImage(background);
+       
+        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
+        renderer.setShapesVisible(true);
+        renderer.setShapesFilled(true);
+        renderer.setPaint(Color.BLACK);
+     
+        ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 1280, 800);
         ChartFrame frame = new ChartFrame("test", chart);
         frame.pack();
         frame.setVisible(true);
     }
 
+    /**
+     * @param dataset
+     * @return
+     */
+    private static JFreeChart createLineChart(XYSeriesCollection dataset) {
+        NumberAxis xAxis = new NumberAxis("S->N");
+        xAxis.setAutoRangeIncludesZero(false);
+        
+        NumberAxis yAxis = new NumberAxis("W->E");
+        yAxis.setAutoRangeIncludesZero(false);
+       
+        XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
+        XYPlot plot = new ZoomableBackgroundXYPlot(dataset, xAxis, yAxis, renderer);
+        plot.setOrientation(PlotOrientation.HORIZONTAL);
+      
+        JFreeChart chart = new JFreeChart(
+            "Track", JFreeChart.DEFAULT_TITLE_FONT, plot, true
+        );
+
+        return chart;
+        /*
+        JFreeChart chart = ChartFactory.createXYLineChart(
+                "Track", 
+                "S->N",
+                "W->E",
+                dataset,
+                PlotOrientation.HORIZONTAL,
+                true,
+                true,
+                false);
+        return chart;
+        */
+    }
+
+    /**
+     * @param aHeightProfile
+     * @return
+     */
+    private static XYSeriesCollection createDataset(List<Pair<Double, Double>> aHeightProfile, String aName) {
+        XYSeries series = new XYSeries(aName, false);
+        for (Pair<Double,Double> point: aHeightProfile) { 
+            series.add(point.getFirst(), point.getSecond());
+        }
+        XYSeriesCollection dataset = new XYSeriesCollection(series);
+        return dataset;
+    }
+
+    private static Pair<Pair<Double,Double>,Pair<Double,Double>> getBounds(List<Pair<Double,Double>> aList) { 
+        Pair<Double,Double> first = aList.get(0); 
+        double minx= first.getFirst();
+        double maxx = minx;
+        double miny = first.getSecond();
+        double maxy = miny;
+        
+        for (int i = 0; i < aList.size(); i++) { 
+            Pair<Double,Double> value = aList.get(i);
+            minx = Math.min(minx, value.getFirst());
+            maxx = Math.max(maxx, value.getFirst());
+            miny = Math.min(miny, value.getSecond());
+            maxy = Math.max(maxy, value.getSecond());
+        }
+        if ( maxx == minx ) { 
+            maxx += 1.0; // to avoid problems. 
+        }
+        if ( maxy == miny ) { 
+            maxy += 1.0; // to avoid problems.
+        }
+        final double paddingFactor = 0.3; // allow some space around min and max
+        return new Pair<Pair<Double,Double>,Pair<Double,Double>>(
+                new Pair<Double,Double>( minx - paddingFactor*(maxx-minx), 
+                                         maxx + paddingFactor*(maxx-minx)),
+                new Pair<Double,Double>( miny - paddingFactor*(maxy-miny), 
+                                         maxy + paddingFactor*(maxy-miny))
+                );
+    }
 }