Changeset 9 in josm for src/org/openstreetmap/josm/io
- Timestamp:
- 2005-10-04T00:09:32+02:00 (20 years ago)
- Location:
- src/org/openstreetmap/josm/io
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/io/GpxReader.java
r8 r9 3 3 import java.io.IOException; 4 4 import java.io.Reader; 5 import java.util.HashMap; 5 6 6 7 import org.jdom.Element; … … 10 11 import org.openstreetmap.josm.data.GeoPoint; 11 12 import org.openstreetmap.josm.data.osm.DataSet; 13 import org.openstreetmap.josm.data.osm.Key; 12 14 import org.openstreetmap.josm.data.osm.LineSegment; 13 15 import org.openstreetmap.josm.data.osm.Node; 16 import org.openstreetmap.josm.data.osm.OsmPrimitive; 14 17 import org.openstreetmap.josm.data.osm.Track; 15 18 import org.openstreetmap.josm.gui.Main; 16 19 17 20 /** 18 * Reads an gpx stream and construct a DataSet out of it. Some information may not be 19 * imported, since JOSM does not fully support GPX. 21 * Reads an gpx stream and construct a DataSet out of it. 22 * Some information may not be imported, but GpxReader tries its best to load 23 * all data possible in the key/value structure. 20 24 * 21 25 * @author imi 22 26 */ 23 public class GpxReader { 24 25 public static final Namespace XSD = Namespace.getNamespace("http://www.w3.org/2001/XMLSchema"); 26 public static final Namespace GPX = Namespace.getNamespace("http://www.topografix.com/GPX/1/0"); 27 public class GpxReader implements DataReader { 28 29 /** 30 * The GPX namespace used. 31 */ 32 private static final Namespace GPX = Namespace.getNamespace("http://www.topografix.com/GPX/1/0"); 33 /** 34 * The OSM namespace used (for extensions). 35 */ 36 private static final Namespace OSM = Namespace.getNamespace("osm"); 37 38 /** 39 * The data source from this reader. 40 */ 41 public Reader source; 27 42 28 43 /** 44 * Construct a parser from a specific data source. 45 * @param source The data source, as example a FileReader to read from a file. 46 */ 47 public GpxReader(Reader source) { 48 this.source = source; 49 } 50 51 /** 29 52 * Read the input stream and return a DataSet from the stream. 30 * 31 * @param in 32 * @throws IOException An error with the provided stream occoured. 33 * @throws JDOMException An parse error occoured. 34 */ 35 public DataSet parse(Reader in) throws JDOMException, IOException { 53 */ 54 public DataSet parse() throws ParseException, ConnectionException { 36 55 try { 37 56 final SAXBuilder builder = new SAXBuilder(); 38 Element root = builder.build( in).getRootElement();57 Element root = builder.build(source).getRootElement(); 39 58 return parseDataSet(root); 40 59 } catch (NullPointerException npe) { 41 throw new JDOMException("NullPointerException. Probably a tag name mismatch.", npe);60 throw new ParseException("NullPointerException. Probably a tag name mismatch.", npe); 42 61 } catch (ClassCastException cce) { 43 throw new JDOMException("ClassCastException. Probably a tag does not contain the correct type.", cce); 62 throw new ParseException("ClassCastException. Probably a tag does not contain the correct type.", cce); 63 } catch (JDOMException e) { 64 throw new ParseException("The data could not be parsed. Reason: "+e.getMessage(), e); 65 } catch (IOException e) { 66 throw new ConnectionException("The data could not be retrieved. Reason: "+e.getMessage(), e); 44 67 } 45 68 } … … 56 79 Float.parseFloat(e.getAttributeValue("lat")), 57 80 Float.parseFloat(e.getAttributeValue("lon"))); 81 for (Object o : e.getChildren()) { 82 Element child = (Element)o; 83 if (child.getName().equals("extensions")) 84 parseKeyValueExtensions(data, child); 85 else if (child.getName().equals("link")) 86 parseKeyValueLink(data, child); 87 else 88 parseKeyValueTag(data, child); 89 } 58 90 return data; 91 } 92 93 /** 94 * Parse the extensions tag and add all properties found as key/value. 95 * <code>osm.keys</code> may be <code>null</code>, in which case it is 96 * created first. If <code>e</code> is <code>null</code>, nothing 97 * happens. 98 * 99 * @param osm The primitive to store the properties. 100 * @param e The extensions element to read the properties from. 101 */ 102 private void parseKeyValueExtensions(OsmPrimitive osm, Element e) { 103 if (e != null) { 104 if (osm.keys == null) 105 osm.keys = new HashMap<Key, String>(); 106 for (Object o : e.getChildren("property", OSM)) { 107 Element child = (Element)o; 108 Key key = Key.get(child.getAttributeValue("name")); 109 osm.keys.put(key, child.getAttributeValue("value")); 110 } 111 } 112 } 113 114 /** 115 * If the element is not <code>null</code>, read the data from it and put 116 * it as the key with the name of the elements name in the given object. 117 * 118 * The <code>keys</code> - field of the element could be <code>null</code>, 119 * in which case it is created first. 120 * 121 * @param osm The osm primitive to put the key into. 122 * @param e The element to look for data. 123 */ 124 private void parseKeyValueTag(OsmPrimitive osm, Element e) { 125 if (e != null) { 126 if (osm.keys == null) 127 osm.keys = new HashMap<Key, String>(); 128 osm.keys.put(Key.get(e.getName()), e.getValue()); 129 } 130 } 131 132 /** 133 * Parse the GPX linkType data information and store it as value in the 134 * primitives <i>link</i> key. <code>osm.keys</code> may be 135 * <code>null</code>, in which case it is created first. If 136 * <code>e</code> is <code>null</code>, nothing happens. 137 * 138 * The format stored is: mimetype;url 139 * Example: text/html;http://www.openstreetmap.org 140 * @param osm The osm primitive to store the data in. 141 * @param e The element in gpx:linkType - format. 142 */ 143 private void parseKeyValueLink(Node osm, Element e) { 144 if (e != null) { 145 if (osm.keys == null) 146 osm.keys = new HashMap<Key, String>(); 147 String link = e.getChildText("type") + ";" + e.getChildText("text"); 148 osm.keys.put(Key.get("link"), link); 149 } 59 150 } 60 151 … … 105 196 if (Main.pref.mergeNodes) 106 197 for (Node n : data.nodes) 107 if (node. equals(n))198 if (node.coor.lat == n.coor.lat && node.coor.lon == n.coor.lon) 108 199 return n; 109 200 data.nodes.add(node);
Note:
See TracChangeset
for help on using the changeset viewer.