From: erik Date: Sat, 12 Aug 2006 19:23:35 +0000 (+0000) Subject: Made the track serializable. X-Git-Tag: BEFORE_MAVEN_MIGRATION~49 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=9584bb45a4731bbfccd810b586441d54ea660871;p=utils Made the track serializable. --- diff --git a/gps/src/org/wamblee/gps/geometry/CoordinateSystem.java b/gps/src/org/wamblee/gps/geometry/CoordinateSystem.java index 9094336b..98fee484 100644 --- a/gps/src/org/wamblee/gps/geometry/CoordinateSystem.java +++ b/gps/src/org/wamblee/gps/geometry/CoordinateSystem.java @@ -16,11 +16,13 @@ package org.wamblee.gps.geometry; +import java.io.Serializable; + /** * Represents a coordinate system. */ -public interface CoordinateSystem { +public interface CoordinateSystem extends Serializable { /** * Conversion to a reference coordinate system. diff --git a/gps/src/org/wamblee/gps/geometry/Coordinates.java b/gps/src/org/wamblee/gps/geometry/Coordinates.java index 544c92a1..4a9b820d 100644 --- a/gps/src/org/wamblee/gps/geometry/Coordinates.java +++ b/gps/src/org/wamblee/gps/geometry/Coordinates.java @@ -16,10 +16,12 @@ package org.wamblee.gps.geometry; +import java.io.Serializable; + /** * Coordinates in some 3-dimensional coordinate system. */ -public class Coordinates { +public class Coordinates implements Serializable { private double _x1; private double _x2; @@ -49,6 +51,15 @@ public class Coordinates { return _x3; } + public double getX(int i) { + switch (i) { + case 1: return _x1; + case 2: return _x2; + case 3: return _x3; + } + throw new IllegalArgumentException("coordinate out of range " + i); + } + /* (non-Javadoc) * @see java.lang.Object#toString() */ diff --git a/gps/src/org/wamblee/gps/geometry/Point.java b/gps/src/org/wamblee/gps/geometry/Point.java index 175bd8e0..f3d614da 100644 --- a/gps/src/org/wamblee/gps/geometry/Point.java +++ b/gps/src/org/wamblee/gps/geometry/Point.java @@ -16,11 +16,13 @@ package org.wamblee.gps.geometry; +import java.io.Serializable; + /** * Represents a point in some coordinate system. */ -public class Point { +public class Point implements Serializable { private Coordinates _coordinates; private CoordinateSystem _system; diff --git a/gps/src/org/wamblee/gps/track/Track.java b/gps/src/org/wamblee/gps/track/Track.java index a281f2f8..30e49f01 100644 --- a/gps/src/org/wamblee/gps/track/Track.java +++ b/gps/src/org/wamblee/gps/track/Track.java @@ -16,6 +16,7 @@ package org.wamblee.gps.track; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; @@ -24,7 +25,7 @@ import org.wamblee.gps.geometry.Point; /** * Represents a GPS track. */ -public class Track { +public class Track implements Serializable { private List _points; @@ -51,6 +52,28 @@ public class Track { return _points.size(); } + public double getMinCoordinate(int i) { + if ( size() == 0 ) { + throw new IllegalArgumentException("empty track"); + } + double min = getPoint(0).getCoordinates().getX(i); + for (int j = 1; j < size(); j++) { + min = Math.min(min, getPoint(j).getCoordinates().getX(i)); + } + return min; + } + + public double getMaxCoordinate(int i) { + if ( size() == 0 ) { + throw new IllegalArgumentException("empty track"); + } + double max = getPoint(0).getCoordinates().getX(i); + for (int j = 1; j < size(); j++) { + max = Math.max(max, getPoint(j).getCoordinates().getX(i)); + } + return max; + } + /** * Gets the point at the given inded. * @param aIndex 0 <= aIndex < size() diff --git a/gps/src/org/wamblee/gpx/GpxPlotter.java b/gps/src/org/wamblee/gpx/GpxPlotter.java index 92878d3f..097dd251 100644 --- a/gps/src/org/wamblee/gpx/GpxPlotter.java +++ b/gps/src/org/wamblee/gpx/GpxPlotter.java @@ -17,19 +17,15 @@ 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 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; @@ -37,13 +33,10 @@ 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; @@ -54,51 +47,25 @@ 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; /** * 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 Exception { - File file = new File(aArgs[0]); - Document doc = DomUtils.convert(DomUtils.read(new FileInputStream(file))); - - Track track = parseTrack(doc); + File file = new File(aArgs[0]); + GpxParser parser = new GpxParser(); + Track track = parser.parse(new FileInputStream(file)); List> elevationProfile = computeElevationProfile(track); printTrack(elevationProfile); computeTotalClimb(elevationProfile); plotElevationProfile(elevationProfile); List> trackXy = computeTrackXY(track); - plotTrack(trackXy); - } - - /** - * @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> trackLatLon = computeTrackLatLon(track); + plotTrack(trackLatLon); } private static List> computeElevationProfile(Track aTrack) { @@ -128,6 +95,15 @@ public class GpxPlotter { return results; } + private static List> computeTrackLatLon(Track aTrack) { + List> results = new ArrayList>(); + for (int i = 0; i < aTrack.size(); i++) { + Point point = aTrack.getPoint(i); + results.add(new Pair(point.getCoordinates().getX1(), point.getCoordinates().getX2())); + } + return results; + } + private static void printTrack(List> aHeightProfile) { @@ -170,22 +146,24 @@ public class GpxPlotter { private static void plotTrack(List> aPoints) throws IOException, InterruptedException { XYSeriesCollection dataset = createDataset(aPoints, "track"); JFreeChart chart = createLineChart(dataset); - Range range = chart.getXYPlot().getRangeAxis().getRange(); + + Pair,Pair> 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); - - //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); + ChartUtilities.writeChartAsPNG(new FileOutputStream("test.png"), chart, 1280, 800); ChartFrame frame = new ChartFrame("test", chart); frame.pack(); frame.setVisible(true); @@ -198,7 +176,10 @@ public class GpxPlotter { 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); @@ -235,5 +216,33 @@ public class GpxPlotter { return dataset; } + private static Pair,Pair> getBounds(List> aList) { + Pair 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 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>( + new Pair( minx - paddingFactor*(maxx-minx), + maxx + paddingFactor*(maxx-minx)), + new Pair( miny - paddingFactor*(maxy-miny), + maxy + paddingFactor*(maxy-miny)) + ); + } } diff --git a/gps/src/org/wamblee/gpx/ZoomableBackgroundXYPlot.java b/gps/src/org/wamblee/gpx/ZoomableBackgroundXYPlot.java index f3788baf..8de07114 100644 --- a/gps/src/org/wamblee/gpx/ZoomableBackgroundXYPlot.java +++ b/gps/src/org/wamblee/gpx/ZoomableBackgroundXYPlot.java @@ -26,7 +26,8 @@ import org.jfree.chart.renderer.xy.XYItemRenderer; import org.jfree.data.xy.XYDataset; /** - * + * Extension of XY plot that provides automatic zooming of the background + * image. */ public class ZoomableBackgroundXYPlot extends XYPlot {