From fa34d476d1ea565c12595b920b561e512801ff4a Mon Sep 17 00:00:00 2001 From: erik Date: Sun, 20 Sep 2009 15:30:55 +0000 Subject: [PATCH] now supporting all gpx files with both tracks and routes. Still Track is jsut a list of points. --- .../main/java/org/wamblee/gpx/GpxParser.java | 97 +++++++++++-------- .../java/org/wamblee/gpx/TrackStatistics.java | 3 +- .../java/org/wamblee/gpx/GpxParserTest.java | 6 ++ 3 files changed, 66 insertions(+), 40 deletions(-) diff --git a/gps/src/main/java/org/wamblee/gpx/GpxParser.java b/gps/src/main/java/org/wamblee/gpx/GpxParser.java index 04379414..9f544a9a 100644 --- a/gps/src/main/java/org/wamblee/gpx/GpxParser.java +++ b/gps/src/main/java/org/wamblee/gpx/GpxParser.java @@ -34,49 +34,68 @@ import org.wamblee.xml.XMLException; */ public class GpxParser { - private static final String SCHEMA_RESOURCE = "gpx.xsd"; + private static final String SCHEMA_RESOURCE = "gpx.xsd"; - public GpxParser() { - // Empty. - } + public GpxParser() { + // Empty. + } - public Track parse(InputStream aIs) throws XMLException { - Document doc = DomUtils.convert(DomUtils.read(aIs)); - return parse(doc); - } + public Track parse(InputStream aIs) throws XMLException { + Document doc = DomUtils.convert(DomUtils.read(aIs)); + return parse(doc); + } - /** - * @param doc - */ - public Track parse(Document doc) { - Track track = new Track(); - List segments = doc.getRootElement().element("trk").elements( - "trkseg"); + /** + * @param doc + */ + public Track parse(Document doc) { + Track result = new Track(); + List tracks = doc.getRootElement().elements("trk"); + if ( tracks != null ) { + for (Element track: tracks) { + List segments = track.elements("trkseg"); + if ( segments != null ) { + for (Element segment: segments) { + parseTrackPoints(result, segment.elementIterator("trkpt")); + } + } + } + } - for (Element segment : segments) { - for (Iterator i = segment.elementIterator("trkpt"); i.hasNext();) { - Element trkpt = (Element) i.next(); - track.addPoint(parseTrackPoint(trkpt)); - } - } - return track; - } + List routes = doc.getRootElement().elements("rte"); + if ( routes != null ) { + for (Element route: routes) { + parseTrackPoints(result, route.elementIterator("rtept")); + } + } + + return result; + } + + + private void parseTrackPoints(Track aTrack, Iterator aPoints) { + while (aPoints.hasNext()) { + Element point = aPoints.next(); + aTrack.addPoint(parseTrackPoint(point)); + } + } + - /** - * @param trkpt - */ - private TrackPoint parseTrackPoint(Element trkpt) { - // System.out.println(trkpt.asXML() + "|\n"); - double latitude = new Double(trkpt.attributeValue("lat")); - double longitude = new Double(trkpt.attributeValue("lon")); - Element ele = trkpt.element("ele"); - double elevation = 0.0; - if (ele != null) { - elevation = new Double(ele.getText()); - } - // System.out.println(" lat = " + lat + " lon = " + lon + " ele = " + - // ele); - return new TrackPoint(latitude, longitude, elevation); - } + /** + * @param trkpt + */ + private TrackPoint parseTrackPoint(Element trkpt) { + // System.out.println(trkpt.asXML() + "|\n"); + double latitude = new Double(trkpt.attributeValue("lat")); + double longitude = new Double(trkpt.attributeValue("lon")); + Element ele = trkpt.element("ele"); + double elevation = 0.0; + if (ele != null) { + elevation = new Double(ele.getText()); + } + // System.out.println(" lat = " + lat + " lon = " + lon + " ele = " + + // ele); + return new TrackPoint(latitude, longitude, elevation); + } } diff --git a/gps/src/main/java/org/wamblee/gpx/TrackStatistics.java b/gps/src/main/java/org/wamblee/gpx/TrackStatistics.java index ce43a79d..42c2039e 100644 --- a/gps/src/main/java/org/wamblee/gpx/TrackStatistics.java +++ b/gps/src/main/java/org/wamblee/gpx/TrackStatistics.java @@ -3,6 +3,7 @@ package org.wamblee.gpx; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; @@ -17,7 +18,7 @@ import org.wamblee.gps.geometry.Point; import org.wamblee.gps.geometry.ReferenceCoordinateSystem; import org.wamblee.gps.track.Track; -public class TrackStatistics { +public class TrackStatistics implements Serializable { private Track _track; diff --git a/gps/src/test/java/org/wamblee/gpx/GpxParserTest.java b/gps/src/test/java/org/wamblee/gpx/GpxParserTest.java index 132b37bd..1439e85b 100644 --- a/gps/src/test/java/org/wamblee/gpx/GpxParserTest.java +++ b/gps/src/test/java/org/wamblee/gpx/GpxParserTest.java @@ -18,4 +18,10 @@ public class GpxParserTest extends TestCase { Track track = parser.parse(new ClassPathResource("nojoy.gpx").getInputStream()); assertEquals(468, track.size()); } + + public void testParseRoute() throws Exception { + GpxParser parser = new GpxParser(); + Track track = parser.parse(new ClassPathResource("mh2d.route.gpx").getInputStream()); + assertEquals(250, track.size()); + } } -- 2.31.1