From 4a00c1546af1c16125b578c9b0e5b0624bae39d9 Mon Sep 17 00:00:00 2001 From: erik Date: Thu, 3 Aug 2006 18:45:44 +0000 Subject: [PATCH] --- gps/src/org/wamblee/gpx/GpxPlotter.java | 109 ++++++++++++++++-- .../wamblee/gpx/ZoomableBackgroundXYPlot.java | 94 +++++++++++++++ 2 files changed, 195 insertions(+), 8 deletions(-) create mode 100644 gps/src/org/wamblee/gpx/ZoomableBackgroundXYPlot.java diff --git a/gps/src/org/wamblee/gpx/GpxPlotter.java b/gps/src/org/wamblee/gpx/GpxPlotter.java index 19a145d1..92878d3f 100644 --- a/gps/src/org/wamblee/gpx/GpxPlotter.java +++ b/gps/src/org/wamblee/gpx/GpxPlotter.java @@ -16,6 +16,9 @@ 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; @@ -33,14 +36,23 @@ 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.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; @@ -50,7 +62,7 @@ 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))); @@ -60,6 +72,8 @@ public class GpxPlotter { printTrack(elevationProfile); computeTotalClimb(elevationProfile); plotElevationProfile(elevationProfile); + List> trackXy = computeTrackXY(track); + plotTrack(trackXy); } /** @@ -101,6 +115,21 @@ public class GpxPlotter { return results; } + private static List> computeTrackXY(Track aTrack) { + Point reference = aTrack.getPoint(0); + Plane plane = new Plane(reference, reference); // assume the earth is spherical. + List> results = new ArrayList>(); + for (int i = 0; i < aTrack.size(); i++) { + Point point = aTrack.getPoint(i); + Pair projection = plane.normalizedProjection(point); + results.add(projection); + System.out.println(point); + } + return results; + } + + + private static void printTrack(List> aHeightProfile) { for (Pair point: aHeightProfile) { System.out.println(point.getFirst() + " " + point.getSecond()); @@ -122,11 +151,7 @@ public class GpxPlotter { } private static void plotElevationProfile(List> aHeightProfile) throws IOException { - XYSeries series = new XYSeries("height"); - for (Pair 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)", @@ -141,6 +166,74 @@ public class GpxPlotter { frame.pack(); frame.setVisible(true); } + + private static void plotTrack(List> 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> aHeightProfile, String aName) { + XYSeries series = new XYSeries(aName, false); + for (Pair point: aHeightProfile) { + series.add(point.getFirst(), point.getSecond()); + } + XYSeriesCollection dataset = new XYSeriesCollection(series); + return dataset; + } } diff --git a/gps/src/org/wamblee/gpx/ZoomableBackgroundXYPlot.java b/gps/src/org/wamblee/gpx/ZoomableBackgroundXYPlot.java new file mode 100644 index 00000000..f3788baf --- /dev/null +++ b/gps/src/org/wamblee/gpx/ZoomableBackgroundXYPlot.java @@ -0,0 +1,94 @@ +/* + * 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("========"); + } + +} -- 2.31.1