now supporting all gpx files with both tracks and routes.
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 20 Sep 2009 15:30:55 +0000 (15:30 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 20 Sep 2009 15:30:55 +0000 (15:30 +0000)
Still Track is jsut a list of points.

trunk/gps/src/main/java/org/wamblee/gpx/GpxParser.java
trunk/gps/src/main/java/org/wamblee/gpx/TrackStatistics.java
trunk/gps/src/test/java/org/wamblee/gpx/GpxParserTest.java

index 043794146a12335665a232414417d4ce31300910..9f544a9a48c8fcb0bf98fd9bfdc2b32253c4eb78 100644 (file)
@@ -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<Element> segments = doc.getRootElement().element("trk").elements(
-                               "trkseg");
+    /**
+     * @param doc
+     */
+    public Track parse(Document doc) {
+        Track result = new Track();
+        List<Element> tracks = doc.getRootElement().elements("trk");
+        if ( tracks != null ) { 
+            for (Element track: tracks) { 
+                List<Element> 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<Element> 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<Element> 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);
+    }
 
 }
index ce43a79d28741dd01636d0a13f508101a04634ca..42c2039e3fad8411b95db686f0ef07c2234131eb 100644 (file)
@@ -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; 
     
index 132b37bdc0e4f7949b5889ff9fd6282afbe0ab11..1439e85b5bff70a109a02eb99350b274b9eea32f 100644 (file)
@@ -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());
+       }
 }