package org.wamblee.gpx;
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.labels.StandardXYToolTipGenerator;
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.chart.urls.StandardXYURLGenerator;
+import org.jfree.data.Range;
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.utils.JpegUtils;
import org.wamblee.xml.DomUtils;
import org.xml.sax.SAXException;
*/
public class GpxPlotter {
- public static void main(String[] aArgs) throws ParserConfigurationException, FileNotFoundException, SAXException, IOException {
+ public static void main(String[] aArgs) throws Exception {
File file = new File(aArgs[0]);
Document doc = DomUtils.convert(DomUtils.read(new FileInputStream(file)));
printTrack(elevationProfile);
computeTotalClimb(elevationProfile);
plotElevationProfile(elevationProfile);
+ List<Pair<Double,Double>> trackXy = computeTrackXY(track);
+ plotTrack(trackXy);
}
/**
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 void printTrack(List<Pair<Double,Double>> aHeightProfile) {
for (Pair<Double,Double> point: aHeightProfile) {
System.out.println(point.getFirst() + " " + point.getSecond());
}
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)",
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);
+ Range range = chart.getXYPlot().getRangeAxis().getRange();
+
+ 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);
+
+ //renderer.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
+ // 1.0f, new float[] { 6.0f, 6.0f}, 0.0f));
+ System.out.println("Range: " + range.getLowerBound() + " " + range.getUpperBound());
+
+ ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 600, 300);
+ 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");
+ 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;
+ }
}
--- /dev/null
+/*
+ * Copyright 2006 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wamblee.gpx;
+
+import java.awt.Graphics2D;
+import java.awt.Image;
+import java.awt.geom.Rectangle2D;
+
+import org.jfree.chart.axis.ValueAxis;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.renderer.xy.XYItemRenderer;
+import org.jfree.data.xy.XYDataset;
+
+/**
+ *
+ */
+public class ZoomableBackgroundXYPlot extends XYPlot {
+
+ /*
+ * Initial domain axis.
+ */
+ private double _x1 = 1.0;
+ private double _x2 = -1.0; // _x2 < _x1 initially to make signify uninitialized values.
+
+ /*
+ * Initial range axis.
+ */
+ private double _y1 = 1.0;
+ private double _y2 = -1.0; // _y2 < _y1 initially to make signify uninitialized values.
+
+ public ZoomableBackgroundXYPlot(XYDataset aDataset,
+ ValueAxis aDomainAxis, ValueAxis aRangeAxis, XYItemRenderer aRenderer) {
+ super(aDataset, aDomainAxis, aRangeAxis, aRenderer);
+ }
+
+ /* (non-Javadoc)
+ * @see org.jfree.chart.plot.Plot#drawBackgroundImage(java.awt.Graphics2D, java.awt.geom.Rectangle2D)
+ */
+ @Override
+ protected void drawBackgroundImage(Graphics2D g2, Rectangle2D area) {
+ //System.out.println("--------");
+ //System.out.println("Area: " + area);
+ //System.out.println("Graphics clip: " + g2.getClipBounds());
+ //System.out.println("Domain axis: " + getDomainAxis().getLowerBound() + " " +
+ // getDomainAxis().getUpperBound());
+
+ // Get the current domain axis bounds
+ double y1 = getDomainAxis().getLowerBound();
+ double y2 = getDomainAxis().getUpperBound();
+ double x1 = getRangeAxis().getLowerBound();
+ double x2 = getRangeAxis().getUpperBound();
+
+ if ( _x2 < _x1 ) {
+ // initial domain axis bounds
+ _y1 = y1;
+ _y2 = y2;
+ _x1 = x1;
+ _x2 = x2;
+ }
+
+ Image background = getBackgroundImage();
+ int width = background.getWidth(null);
+ int height = background.getHeight(null);
+
+ // Determine the part of the image to be drawn on the screen based on the scaling
+ // of the domain axes.
+ int imageX1 = (int)Math.round(1 + (x1 - _x1)*(width-1)/(_x2 - _x1));
+ int imageX2 = (int)Math.round(1 + (x2 - _x1)*(width-1)/(_x2 - _x1));
+ // Note: y-axis of image goes from bottom to top.
+ int imageY2 = (int)Math.round(height + (y1 - _y1)*(1-height)/(_y2 - _y1));
+ int imageY1 = (int)Math.round(height + (y2 - _y1)*(1-height)/(_y2 - _y1));
+
+ // Draw the correct part of the image on the screen.
+ g2.drawImage(background, (int)area.getMinX(), (int)area.getMinY(), (int)area.getMaxX(), (int)area.getMaxY(),
+ imageX1, imageY1, imageX2, imageY2, null);
+
+ // System.out.println("========");
+ }
+
+}