X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=gps%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fgpx%2FGpxParser.java;h=043794146a12335665a232414417d4ce31300910;hb=34b0dc0bca5a2d99394b9692b4ea14ce9e2141b7;hp=30ced035712445f7e7cbeaafe02136db2a263d4e;hpb=bac714989e43848a344eaeb6263595f5b1d63227;p=utils diff --git a/gps/src/main/java/org/wamblee/gpx/GpxParser.java b/gps/src/main/java/org/wamblee/gpx/GpxParser.java index 30ced035..04379414 100644 --- a/gps/src/main/java/org/wamblee/gpx/GpxParser.java +++ b/gps/src/main/java/org/wamblee/gpx/GpxParser.java @@ -12,12 +12,13 @@ * 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.io.InputStream; import java.util.Iterator; +import java.util.List; import org.dom4j.Document; import org.dom4j.Element; @@ -27,50 +28,55 @@ import org.wamblee.xml.DomUtils; import org.wamblee.xml.XMLException; /** - * Parser for GPX tracks. - * + * Parser for GPX tracks. + * * @author Erik Brakkee */ public class GpxParser { - - private static final String SCHEMA_RESOURCE = "gpx.xsd"; - - public GpxParser() { - // Empty. - } - - 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(); - 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 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); - } + private static final String SCHEMA_RESOURCE = "gpx.xsd"; + + public GpxParser() { + // Empty. + } + + 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"); + + for (Element segment : segments) { + for (Iterator i = segment.elementIterator("trkpt"); i.hasNext();) { + Element trkpt = (Element) i.next(); + track.addPoint(parseTrackPoint(trkpt)); + } + } + return track; + } + + /** + * @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); + } }