source: josm/src/org/openstreetmap/josm/io/OsmReader.java@ 72

Last change on this file since 72 was 72, checked in by imi, 18 years ago

fixed incomplete line segment saving

File size: 4.2 KB
Line 
1package org.openstreetmap.josm.io;
2
3import java.io.IOException;
4import java.io.Reader;
5import java.util.HashMap;
6import java.util.Map;
7
8import org.openstreetmap.josm.data.coor.LatLon;
9import org.openstreetmap.josm.data.osm.DataSet;
10import org.openstreetmap.josm.data.osm.LineSegment;
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.Way;
14import org.openstreetmap.josm.data.osm.visitor.AddVisitor;
15import org.xml.sax.Attributes;
16import org.xml.sax.SAXException;
17
18import uk.co.wilson.xml.MinML2;
19
20/**
21 * Parser for the Osm Api. Read from an input stream and construct a dataset out of it.
22 *
23 * @author Imi
24 */
25public class OsmReader extends MinML2 {
26
27 /**
28 * The dataset to add parsed objects to.
29 */
30 private DataSet ds = new DataSet();
31
32 /**
33 * The visitor to use to add the data to the set.
34 */
35 private AddVisitor adder = new AddVisitor(ds);
36
37 /**
38 * The current processed primitive.
39 */
40 private OsmPrimitive current;
41
42 /**
43 * All read nodes so far.
44 */
45 private Map<Long, Node> nodes = new HashMap<Long, Node>();
46 /**
47 * All read segents so far.
48 */
49 private Map<Long, LineSegment> lineSegments = new HashMap<Long, LineSegment>();
50
51 /**
52 * Parse the given input source and return the dataset.
53 */
54 public static DataSet parseDataSet(Reader source) throws SAXException, IOException {
55 OsmReader osm = new OsmReader(source);
56
57 // clear all negative ids (new to this file)
58 for (OsmPrimitive o : osm.ds.allPrimitives())
59 if (o.id < 0)
60 o.id = 0;
61
62 return osm.ds;
63 }
64
65 private OsmReader(Reader source) throws SAXException, IOException {
66 parse(source);
67 }
68
69 @Override
70 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
71 try {
72 if (qName.equals("osm")) {
73 if (atts == null)
74 throw new SAXException("Unknown version.");
75 if (!"0.3".equals(atts.getValue("version")))
76 throw new SAXException("Unknown version: "+atts.getValue("version"));
77 } else if (qName.equals("node")) {
78 Node n = new Node(new LatLon(getDouble(atts, "lat"), getDouble(atts, "lon")));
79 current = n;
80 readCommon(atts);
81 current.id = getLong(atts, "id");
82 nodes.put(n.id, n);
83 } else if (qName.equals("segment")) {
84 current = new LineSegment(
85 nodes.get(getLong(atts, "from")),
86 nodes.get(getLong(atts, "to")));
87 readCommon(atts);
88 lineSegments.put(current.id, (LineSegment)current);
89 } else if (qName.equals("way")) {
90 current = new Way();
91 readCommon(atts);
92 } else if (qName.equals("seg")) {
93 if (current instanceof Way) {
94 long id = getLong(atts, "id");
95 LineSegment ls = lineSegments.get(id);
96 if (ls == null) {
97 ls = new LineSegment(id); // incomplete line segment
98 lineSegments.put(id, ls);
99 adder.visit(ls);
100 }
101 ((Way)current).segments.add(ls);
102 }
103 } else if (qName.equals("tag")) {
104 current.put(atts.getValue("k"), atts.getValue("v"));
105 }
106 } catch (NumberFormatException x) {
107 throw new SAXException(x.getMessage(), x);
108 } catch (NullPointerException x) {
109 throw new SAXException("NullPointerException. Possible some missing tags.", x);
110 }
111 }
112
113
114 @Override
115 public void endElement(String namespaceURI, String localName, String qName) {
116 if (qName.equals("node") || qName.equals("segment") || qName.equals("way") || qName.equals("area")) {
117 current.visit(adder);
118 }
119 }
120
121 /**
122 * Read out the common attributes from atts and put them into this.current.
123 */
124 private void readCommon(Attributes atts) {
125 current.id = getLong(atts, "id");
126 String action = atts.getValue("action");
127 if ("delete".equals(action))
128 current.setDeleted(true);
129 else if ("modify".equals(action)) {
130 current.modified = true;
131 current.modifiedProperties = true;
132 } else if ("modify/object".equals(action))
133 current.modified = true;
134 else if ("modify/property".equals(action))
135 current.modifiedProperties = true;
136 }
137
138 private double getDouble(Attributes atts, String value) {
139 return Double.parseDouble(atts.getValue(value));
140 }
141 private long getLong(Attributes atts, String value) {
142 return Long.parseLong(atts.getValue(value));
143 }
144}
Note: See TracBrowser for help on using the repository browser.