| 1 | package org.openstreetmap.josm.io;
|
|---|
| 2 |
|
|---|
| 3 | import java.io.IOException;
|
|---|
| 4 | import java.io.Reader;
|
|---|
| 5 | import java.util.ArrayList;
|
|---|
| 6 | import java.util.LinkedList;
|
|---|
| 7 |
|
|---|
| 8 | import org.jdom.Element;
|
|---|
| 9 | import org.jdom.JDOMException;
|
|---|
| 10 | import org.jdom.Namespace;
|
|---|
| 11 | import org.jdom.input.SAXBuilder;
|
|---|
| 12 | import org.openstreetmap.josm.data.GeoPoint;
|
|---|
| 13 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.Track;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.LineSegment;
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Reads an gpx stream and construct a DataSet out of it. Some information may not be
|
|---|
| 20 | * imported, since JOSM does not fully support GPX.
|
|---|
| 21 | *
|
|---|
| 22 | * @author imi
|
|---|
| 23 | */
|
|---|
| 24 | public class GpxReader {
|
|---|
| 25 |
|
|---|
| 26 | public static final Namespace XSD = Namespace.getNamespace("http://www.w3.org/2001/XMLSchema");
|
|---|
| 27 | public static final Namespace GPX = Namespace.getNamespace("http://www.topografix.com/GPX/1/0");
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Read the input stream and return a DataSet from the stream.
|
|---|
| 31 | *
|
|---|
| 32 | * @param in
|
|---|
| 33 | * @throws IOException An error with the provided stream occoured.
|
|---|
| 34 | * @throws JDOMException An parse error occoured.
|
|---|
| 35 | */
|
|---|
| 36 | public DataSet parse(Reader in) throws JDOMException, IOException {
|
|---|
| 37 | try {
|
|---|
| 38 | final SAXBuilder builder = new SAXBuilder();
|
|---|
| 39 | Element root = builder.build(in).getRootElement();
|
|---|
| 40 | return parseDataSet(root);
|
|---|
| 41 | } catch (NullPointerException npe) {
|
|---|
| 42 | throw new JDOMException("NullPointerException. Probably a tag name mismatch.", npe);
|
|---|
| 43 | } catch (ClassCastException cce) {
|
|---|
| 44 | throw new JDOMException("ClassCastException. Probably a tag does not contain the correct type.", cce);
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Read one node (waypoint).
|
|---|
| 51 | * @param e The element to parse
|
|---|
| 52 | * @return The Waypoint read from the element
|
|---|
| 53 | */
|
|---|
| 54 | private Node parseWaypoint(Element e) {
|
|---|
| 55 | Node data = new Node();
|
|---|
| 56 | data.coor = new GeoPoint(
|
|---|
| 57 | Float.parseFloat(e.getAttributeValue("lat")),
|
|---|
| 58 | Float.parseFloat(e.getAttributeValue("lon")));
|
|---|
| 59 | return data;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Read a data set from the element.
|
|---|
| 64 | * @param e The element to parse
|
|---|
| 65 | * @return The DataSet read from the element
|
|---|
| 66 | */
|
|---|
| 67 | private DataSet parseDataSet(Element e) {
|
|---|
| 68 | DataSet data = new DataSet();
|
|---|
| 69 | // read global information
|
|---|
| 70 | data.author = e.getAttributeValue("creator");
|
|---|
| 71 | Element metadata = e.getChild("metadata");
|
|---|
| 72 | if (metadata != null)
|
|---|
| 73 | {
|
|---|
| 74 | data.desc = metadata.getChildText("desc", XSD);
|
|---|
| 75 | data.name = metadata.getChildText("name", XSD);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | // read waypoints not contained in tracks or areas
|
|---|
| 79 | data.allNodes = new LinkedList<Node>();
|
|---|
| 80 | for (Object o : e.getChildren("wpt", GPX))
|
|---|
| 81 | data.allNodes.add(parseWaypoint((Element)o));
|
|---|
| 82 |
|
|---|
| 83 | // read tracks
|
|---|
| 84 | for (Object trackElement : e.getChildren("trk", GPX)) {
|
|---|
| 85 | Track track = new Track();
|
|---|
| 86 | for (Object trackSegmentElement : ((Element)trackElement).getChildren("trkseg", GPX)) {
|
|---|
| 87 | Node start = null;
|
|---|
| 88 | for (Object w : ((Element)trackSegmentElement).getChildren("trkpt", GPX)) {
|
|---|
| 89 | Node node = parseWaypoint((Element)w);
|
|---|
| 90 | data.allNodes.add(node);
|
|---|
| 91 | if (start == null)
|
|---|
| 92 | start = node;
|
|---|
| 93 | else {
|
|---|
| 94 | LineSegment lineSegment = new LineSegment(start, node);
|
|---|
| 95 | track.segments.add(lineSegment);
|
|---|
| 96 | start = null;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 | if (data.tracks == null)
|
|---|
| 101 | data.tracks = new ArrayList<Track>();
|
|---|
| 102 | data.tracks.add(track);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | return data;
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|