1 | package org.openstreetmap.josm.io;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.InputStream;
|
---|
5 | import java.io.InputStreamReader;
|
---|
6 | import java.text.DateFormat;
|
---|
7 | import java.text.ParseException;
|
---|
8 | import java.text.SimpleDateFormat;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Map.Entry;
|
---|
14 |
|
---|
15 | import javax.swing.BoundedRangeModel;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
19 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
20 | import org.openstreetmap.josm.data.osm.Node;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
22 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
23 | import org.openstreetmap.josm.data.osm.Way;
|
---|
24 | import org.openstreetmap.josm.data.osm.visitor.AddVisitor;
|
---|
25 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
26 | import org.xml.sax.Attributes;
|
---|
27 | import org.xml.sax.SAXException;
|
---|
28 |
|
---|
29 | import uk.co.wilson.xml.MinML2;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Parser for the Osm Api. Read from an input stream and construct a dataset out of it.
|
---|
33 | *
|
---|
34 | * Reading process takes place in three phases. During the first phase (including xml parse),
|
---|
35 | * all nodes are read and stored. Other information than nodes are stored in a raw list
|
---|
36 | *
|
---|
37 | * The second phase reads from the raw list all segments and create Segment objects.
|
---|
38 | *
|
---|
39 | * The third phase read all ways out of the remaining objects in the raw list.
|
---|
40 | *
|
---|
41 | * @author Imi
|
---|
42 | */
|
---|
43 | public class OsmReader {
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * The dataset to add parsed objects to.
|
---|
47 | */
|
---|
48 | private DataSet ds = new DataSet();
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * The visitor to use to add the data to the set.
|
---|
52 | */
|
---|
53 | private AddVisitor adder = new AddVisitor(ds);
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * All read nodes after phase 1.
|
---|
57 | */
|
---|
58 | private Map<Long, Node> nodes = new HashMap<Long, Node>();
|
---|
59 |
|
---|
60 | private static class OsmPrimitiveData extends OsmPrimitive {
|
---|
61 | @Override public void visit(Visitor visitor) {}
|
---|
62 | public int compareTo(OsmPrimitive o) {return 0;}
|
---|
63 |
|
---|
64 | public void copyTo(OsmPrimitive osm) {
|
---|
65 | osm.id = id;
|
---|
66 | osm.keys = keys;
|
---|
67 | osm.modified = modified;
|
---|
68 | osm.selected = selected;
|
---|
69 | osm.deleted = deleted;
|
---|
70 | osm.timestamp = timestamp;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Data structure for the remaining segment objects
|
---|
76 | * Maps the raw attributes to key/value pairs.
|
---|
77 | */
|
---|
78 | private Map<OsmPrimitiveData, long[]> segs = new HashMap<OsmPrimitiveData, long[]>();
|
---|
79 | /**
|
---|
80 | * Data structure for the remaining way objects
|
---|
81 | */
|
---|
82 | private Map<OsmPrimitiveData, Collection<Long>> ways = new HashMap<OsmPrimitiveData, Collection<Long>>();
|
---|
83 |
|
---|
84 |
|
---|
85 | private class Parser extends MinML2 {
|
---|
86 | /**
|
---|
87 | * The current osm primitive to be read.
|
---|
88 | */
|
---|
89 | private OsmPrimitive current;
|
---|
90 |
|
---|
91 | @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
92 | try {
|
---|
93 | if (qName.equals("osm")) {
|
---|
94 | if (atts == null)
|
---|
95 | throw new SAXException("Unknown version.");
|
---|
96 | if (!"0.3".equals(atts.getValue("version")))
|
---|
97 | throw new SAXException("Unknown version "+atts.getValue("version"));
|
---|
98 | } else if (qName.equals("node")) {
|
---|
99 | current = new Node(new LatLon(getDouble(atts, "lat"), getDouble(atts, "lon")));
|
---|
100 | readCommon(atts, current);
|
---|
101 | nodes.put(current.id, (Node)current);
|
---|
102 | } else if (qName.equals("segment")) {
|
---|
103 | current = new OsmPrimitiveData();
|
---|
104 | readCommon(atts, current);
|
---|
105 | segs.put((OsmPrimitiveData)current, new long[]{getLong(atts, "from"), getLong(atts, "to")});
|
---|
106 | } else if (qName.equals("way")) {
|
---|
107 | current = new OsmPrimitiveData();
|
---|
108 | readCommon(atts, current);
|
---|
109 | ways.put((OsmPrimitiveData)current, new LinkedList<Long>());
|
---|
110 | } else if (qName.equals("seg")) {
|
---|
111 | Collection<Long> list = ways.get(current);
|
---|
112 | if (list == null)
|
---|
113 | throw new SAXException("Found <seg> tag on non-way.");
|
---|
114 | long id = getLong(atts, "id");
|
---|
115 | if (id == 0)
|
---|
116 | throw new SAXException("Incomplete segment with id=0");
|
---|
117 | list.add(id);
|
---|
118 | } else if (qName.equals("tag"))
|
---|
119 | current.put(atts.getValue("k"), atts.getValue("v"));
|
---|
120 | } catch (NumberFormatException x) {
|
---|
121 | x.printStackTrace(); // SAXException does not chain correctly
|
---|
122 | throw new SAXException(x.getMessage(), x);
|
---|
123 | } catch (NullPointerException x) {
|
---|
124 | x.printStackTrace(); // SAXException does not chain correctly
|
---|
125 | throw new SAXException("NullPointerException. Possible some missing tags.", x);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | private double getDouble(Attributes atts, String value) {
|
---|
130 | return Double.parseDouble(atts.getValue(value));
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Read out the common attributes from atts and put them into this.current.
|
---|
136 | */
|
---|
137 | void readCommon(Attributes atts, OsmPrimitive current) throws SAXException {
|
---|
138 | current.id = getLong(atts, "id");
|
---|
139 | if (current.id == 0)
|
---|
140 | throw new SAXException("Illegal object with id=0");
|
---|
141 |
|
---|
142 | String time = atts.getValue("timestamp");
|
---|
143 | if (time != null && time.length() != 0) {
|
---|
144 | try {
|
---|
145 | DateFormat df = new SimpleDateFormat("y-M-d H:m:s");
|
---|
146 | current.timestamp = df.parse(time);
|
---|
147 | } catch (ParseException e) {
|
---|
148 | e.printStackTrace();
|
---|
149 | throw new SAXException("Couldn't read time format '"+time+"'.");
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | String action = atts.getValue("action");
|
---|
154 | if (action == null)
|
---|
155 | return;
|
---|
156 | if (action.equals("delete"))
|
---|
157 | current.delete(true);
|
---|
158 | else if (action.startsWith("modify"))
|
---|
159 | current.modified = true;
|
---|
160 | }
|
---|
161 | private long getLong(Attributes atts, String value) throws SAXException {
|
---|
162 | String s = atts.getValue(value);
|
---|
163 | if (s == null)
|
---|
164 | throw new SAXException("Missing required attirbute '"+value+"'");
|
---|
165 | return Long.parseLong(s);
|
---|
166 | }
|
---|
167 |
|
---|
168 | private void createSegments() {
|
---|
169 | for (Entry<OsmPrimitiveData, long[]> e : segs.entrySet()) {
|
---|
170 | Node from = nodes.get(e.getValue()[0]);
|
---|
171 | Node to = nodes.get(e.getValue()[1]);
|
---|
172 | if (from == null || to == null)
|
---|
173 | continue; //TODO: implement support for incomplete nodes.
|
---|
174 | Segment s = new Segment(from, to);
|
---|
175 | e.getKey().copyTo(s);
|
---|
176 | segments.put(s.id, s);
|
---|
177 | adder.visit(s);
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | private void createWays() {
|
---|
182 | for (Entry<OsmPrimitiveData, Collection<Long>> e : ways.entrySet()) {
|
---|
183 | Way w = new Way();
|
---|
184 | for (long id : e.getValue()) {
|
---|
185 | Segment s = segments.get(id);
|
---|
186 | if (s == null)
|
---|
187 | s = new Segment(id); // incomplete line segment
|
---|
188 | w.segments.add(s);
|
---|
189 | }
|
---|
190 | e.getKey().copyTo(w);
|
---|
191 | adder.visit(w);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * All read segments after phase 2.
|
---|
197 | */
|
---|
198 | private Map<Long, Segment> segments = new HashMap<Long, Segment>();
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Parse the given input source and return the dataset.
|
---|
202 | */
|
---|
203 | public static DataSet parseDataSet(InputStream source, JLabel currentAction, BoundedRangeModel progress) throws SAXException, IOException {
|
---|
204 | OsmReader osm = new OsmReader();
|
---|
205 |
|
---|
206 | // phase 1: Parse nodes and read in raw segments and ways
|
---|
207 | osm.new Parser().parse(new InputStreamReader(source, "UTF-8"));
|
---|
208 | if (progress != null)
|
---|
209 | progress.setValue(0);
|
---|
210 | if (currentAction != null)
|
---|
211 | currentAction.setText("Preparing data...");
|
---|
212 | for (Node n : osm.nodes.values())
|
---|
213 | osm.adder.visit(n);
|
---|
214 |
|
---|
215 | try {
|
---|
216 | osm.createSegments();
|
---|
217 | osm.createWays();
|
---|
218 | } catch (NumberFormatException e) {
|
---|
219 | e.printStackTrace();
|
---|
220 | throw new SAXException("Illformed Node id");
|
---|
221 | }
|
---|
222 |
|
---|
223 | // clear all negative ids (new to this file)
|
---|
224 | for (OsmPrimitive o : osm.ds.allPrimitives())
|
---|
225 | if (o.id < 0)
|
---|
226 | o.id = 0;
|
---|
227 |
|
---|
228 | return osm.ds;
|
---|
229 | }
|
---|
230 | }
|
---|