From: erik Date: Tue, 15 Aug 2006 20:16:44 +0000 (+0000) Subject: (no commit message) X-Git-Tag: BEFORE_MAVEN_MIGRATION~46 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=a34a0f3627a7ff783d4de64c59de941c52dee2c7;p=utils --- diff --git a/gps/src/org/wamblee/gpx/GpxParser.java b/gps/src/org/wamblee/gpx/GpxParser.java new file mode 100644 index 00000000..500091b3 --- /dev/null +++ b/gps/src/org/wamblee/gpx/GpxParser.java @@ -0,0 +1,71 @@ +/* + * 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.io.IOException; +import java.io.InputStream; +import java.util.Iterator; + +import javax.xml.parsers.ParserConfigurationException; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.wamblee.gps.track.Track; +import org.wamblee.gps.track.TrackPoint; +import org.wamblee.xml.DomUtils; +import org.xml.sax.SAXException; + +/** + * Parser for GPX tracks. + */ +public class GpxParser { + + public GpxParser() { + // Empty. + } + + public Track parse(InputStream aIs) throws SAXException, ParserConfigurationException, IOException { + 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")); + double elevation = new Double(trkpt.elementText("ele")); + //System.out.println(" lat = " + lat + " lon = " + lon + " ele = " + ele); + return new TrackPoint(latitude, longitude, elevation); + } + +}