1 | package org.openstreetmap.josm.io;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.io.InputStreamReader;
|
---|
7 | import java.text.MessageFormat;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.logging.Level;
|
---|
15 | import java.util.logging.Logger;
|
---|
16 |
|
---|
17 | import javax.xml.parsers.ParserConfigurationException;
|
---|
18 | import javax.xml.parsers.SAXParserFactory;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.data.Bounds;
|
---|
21 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
22 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
23 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
25 | import org.openstreetmap.josm.data.osm.NodeData;
|
---|
26 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
27 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
28 | import org.openstreetmap.josm.data.osm.PrimitiveData;
|
---|
29 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
30 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
31 | import org.openstreetmap.josm.data.osm.RelationData;
|
---|
32 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
33 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
34 | import org.openstreetmap.josm.data.osm.Storage;
|
---|
35 | import org.openstreetmap.josm.data.osm.User;
|
---|
36 | import org.openstreetmap.josm.data.osm.Way;
|
---|
37 | import org.openstreetmap.josm.data.osm.WayData;
|
---|
38 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
39 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
40 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
41 | import org.openstreetmap.josm.tools.DateUtils;
|
---|
42 | import org.xml.sax.Attributes;
|
---|
43 | import org.xml.sax.InputSource;
|
---|
44 | import org.xml.sax.Locator;
|
---|
45 | import org.xml.sax.SAXException;
|
---|
46 | import org.xml.sax.helpers.DefaultHandler;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Parser for the Osm Api. Read from an input stream and construct a dataset out of it.
|
---|
50 | *
|
---|
51 | */
|
---|
52 | public class OsmReader {
|
---|
53 | static private final Logger logger = Logger.getLogger(OsmReader.class.getName());
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * The dataset to add parsed objects to.
|
---|
57 | */
|
---|
58 | private DataSet ds = new DataSet();
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Replies the parsed data set
|
---|
62 | *
|
---|
63 | * @return the parsed data set
|
---|
64 | */
|
---|
65 | public DataSet getDataSet() {
|
---|
66 | return ds;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** the map from external ids to read OsmPrimitives. External ids are
|
---|
70 | * longs too, but in contrast to internal ids negative values are used
|
---|
71 | * to identify primitives unknown to the OSM server
|
---|
72 | */
|
---|
73 | private Map<PrimitiveId, OsmPrimitive> externalIdMap = new HashMap<PrimitiveId, OsmPrimitive>();
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * constructor (for private use only)
|
---|
77 | *
|
---|
78 | * @see #parseDataSet(InputStream, DataSet, ProgressMonitor)
|
---|
79 | * @see #parseDataSetOsm(InputStream, DataSet, ProgressMonitor)
|
---|
80 | */
|
---|
81 | private OsmReader() {
|
---|
82 | externalIdMap = new HashMap<PrimitiveId, OsmPrimitive>();
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Used as a temporary storage for relation members, before they
|
---|
87 | * are resolved into pointers to real objects.
|
---|
88 | */
|
---|
89 | private static class RelationMemberData {
|
---|
90 | public OsmPrimitiveType type;
|
---|
91 | public long id;
|
---|
92 | public String role;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Data structure for the remaining way objects
|
---|
97 | */
|
---|
98 | private Map<Long, Collection<Long>> ways = new HashMap<Long, Collection<Long>>();
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Data structure for relation objects
|
---|
102 | */
|
---|
103 | private Map<Long, Collection<RelationMemberData>> relations = new HashMap<Long, Collection<RelationMemberData>>();
|
---|
104 |
|
---|
105 | private class Parser extends DefaultHandler {
|
---|
106 | private Locator locator;
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public void setDocumentLocator(Locator locator) {
|
---|
110 | this.locator = locator;
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected void throwException(String msg) throws OsmDataParsingException{
|
---|
114 | throw new OsmDataParsingException(msg).rememberLocation(locator);
|
---|
115 | }
|
---|
116 | /**
|
---|
117 | * The current osm primitive to be read.
|
---|
118 | */
|
---|
119 | private OsmPrimitive currentPrimitive;
|
---|
120 | private long currentExternalId;
|
---|
121 | private String generator;
|
---|
122 | private Storage<String> internedStrings = new Storage<String>();
|
---|
123 |
|
---|
124 | // Memory optimization - see #2312
|
---|
125 | private String intern(String s) {
|
---|
126 | String result = internedStrings.get(s);
|
---|
127 | if (result == null) {
|
---|
128 | internedStrings.put(s);
|
---|
129 | return s;
|
---|
130 | } else
|
---|
131 | return result;
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
135 |
|
---|
136 | if (qName.equals("osm")) {
|
---|
137 | if (atts == null) {
|
---|
138 | throwException(tr("Missing mandatory attribute ''{0}'' of XML element {1}.", "version", "osm"));
|
---|
139 | }
|
---|
140 | String v = atts.getValue("version");
|
---|
141 | if (v == null) {
|
---|
142 | throwException(tr("Missing mandatory attribute ''{0}''.", "version"));
|
---|
143 | }
|
---|
144 | if (!(v.equals("0.5") || v.equals("0.6"))) {
|
---|
145 | throwException(tr("Unsupported version: {0}", v));
|
---|
146 | }
|
---|
147 | // save generator attribute for later use when creating DataSource objects
|
---|
148 | generator = atts.getValue("generator");
|
---|
149 | ds.setVersion(v);
|
---|
150 |
|
---|
151 | } else if (qName.equals("bounds")) {
|
---|
152 | // new style bounds.
|
---|
153 | String minlon = atts.getValue("minlon");
|
---|
154 | String minlat = atts.getValue("minlat");
|
---|
155 | String maxlon = atts.getValue("maxlon");
|
---|
156 | String maxlat = atts.getValue("maxlat");
|
---|
157 | String origin = atts.getValue("origin");
|
---|
158 | if (minlon != null && maxlon != null && minlat != null && maxlat != null) {
|
---|
159 | if (origin == null) {
|
---|
160 | origin = generator;
|
---|
161 | }
|
---|
162 | Bounds bounds = new Bounds(
|
---|
163 | new LatLon(Double.parseDouble(minlat), Double.parseDouble(minlon)),
|
---|
164 | new LatLon(Double.parseDouble(maxlat), Double.parseDouble(maxlon)));
|
---|
165 | DataSource src = new DataSource(bounds, origin);
|
---|
166 | ds.dataSources.add(src);
|
---|
167 | } else {
|
---|
168 | throwException(tr(
|
---|
169 | "Missing manadatory attributes on element ''bounds''. Got minlon=''{0}'',minlat=''{1}'',maxlon=''{3}'',maxlat=''{4}'', origin=''{5}''.",
|
---|
170 | minlon, minlat, maxlon, maxlat, origin
|
---|
171 | ));
|
---|
172 | }
|
---|
173 |
|
---|
174 | // ---- PARSING NODES AND WAYS ----
|
---|
175 |
|
---|
176 | } else if (qName.equals("node")) {
|
---|
177 | NodeData nd = new NodeData();
|
---|
178 | nd.setCoor(new LatLon(getDouble(atts, "lat"), getDouble(atts, "lon")));
|
---|
179 | readCommon(atts, nd);
|
---|
180 | Node n = new Node(nd.getId(), nd.getVersion());
|
---|
181 | n.load(nd);
|
---|
182 | externalIdMap.put(nd.getPrimitiveId(), n);
|
---|
183 | currentPrimitive = n;
|
---|
184 | currentExternalId = nd.getUniqueId();
|
---|
185 | } else if (qName.equals("way")) {
|
---|
186 | WayData wd = new WayData();
|
---|
187 | readCommon(atts, wd);
|
---|
188 | Way w = new Way(wd.getId(), wd.getVersion());
|
---|
189 | w.load(wd);
|
---|
190 | externalIdMap.put(wd.getPrimitiveId(), w);
|
---|
191 | ways.put(wd.getUniqueId(), new ArrayList<Long>());
|
---|
192 | currentPrimitive = w;
|
---|
193 | currentExternalId = wd.getUniqueId();
|
---|
194 | } else if (qName.equals("nd")) {
|
---|
195 | Collection<Long> list = ways.get(currentExternalId);
|
---|
196 | if (list == null) {
|
---|
197 | throwException(
|
---|
198 | tr("Found XML element <nd> not as direct child of element <way>.")
|
---|
199 | );
|
---|
200 | }
|
---|
201 | if (atts.getValue("ref") == null) {
|
---|
202 | throwException(
|
---|
203 | tr("Missing mandatory attribute ''{0}'' on <nd> of way {1}.", "ref", currentPrimitive.getUniqueId())
|
---|
204 | );
|
---|
205 | }
|
---|
206 | long id = getLong(atts, "ref");
|
---|
207 | if (id == 0) {
|
---|
208 | throwException(
|
---|
209 | tr("Illegal value of attribute ''ref'' of element <nd>. Got {0}.", id)
|
---|
210 | );
|
---|
211 | }
|
---|
212 | if (currentPrimitive.isDeleted()) {
|
---|
213 | logger.info(tr("Deleted way {0} contains nodes", currentPrimitive.getUniqueId()));
|
---|
214 | } else {
|
---|
215 | list.add(id);
|
---|
216 | }
|
---|
217 |
|
---|
218 | // ---- PARSING RELATIONS ----
|
---|
219 |
|
---|
220 | } else if (qName.equals("relation")) {
|
---|
221 | RelationData rd = new RelationData();
|
---|
222 | readCommon(atts, rd);
|
---|
223 | Relation r = new Relation(rd.getId(), rd.getVersion());
|
---|
224 | r.load(rd);
|
---|
225 | externalIdMap.put(rd.getPrimitiveId(), r);
|
---|
226 | relations.put(rd.getUniqueId(), new LinkedList<RelationMemberData>());
|
---|
227 | currentPrimitive = r;
|
---|
228 | currentExternalId = rd.getUniqueId();
|
---|
229 | } else if (qName.equals("member")) {
|
---|
230 | Collection<RelationMemberData> list = relations.get(currentExternalId);
|
---|
231 | if (list == null) {
|
---|
232 | throwException(
|
---|
233 | tr("Found XML element <member> not as direct child of element <relation>.")
|
---|
234 | );
|
---|
235 | }
|
---|
236 | RelationMemberData emd = new RelationMemberData();
|
---|
237 | String value = atts.getValue("ref");
|
---|
238 | if (value == null) {
|
---|
239 | throwException(tr("Missing attribute ''ref'' on member in relation {0}.",currentPrimitive.getUniqueId()));
|
---|
240 | }
|
---|
241 | try {
|
---|
242 | emd.id = Long.parseLong(value);
|
---|
243 | } catch(NumberFormatException e) {
|
---|
244 | throwException(tr("Illegal value for attribute ''ref'' on member in relation {0}. Got {1}", Long.toString(currentPrimitive.getUniqueId()),value));
|
---|
245 | }
|
---|
246 | value = atts.getValue("type");
|
---|
247 | if (value == null) {
|
---|
248 | throwException(tr("Missing attribute ''type'' on member {0} in relation {1}.", Long.toString(emd.id), Long.toString(currentPrimitive.getUniqueId())));
|
---|
249 | }
|
---|
250 | try {
|
---|
251 | emd.type = OsmPrimitiveType.fromApiTypeName(value);
|
---|
252 | } catch(IllegalArgumentException e) {
|
---|
253 | throwException(tr("Illegal value for attribute ''type'' on member {0} in relation {1}. Got {2}.", Long.toString(emd.id), Long.toString(currentPrimitive.getUniqueId()), value));
|
---|
254 | }
|
---|
255 | value = atts.getValue("role");
|
---|
256 | emd.role = value;
|
---|
257 |
|
---|
258 | if (emd.id == 0) {
|
---|
259 | throwException(tr("Incomplete <member> specification with ref=0"));
|
---|
260 | }
|
---|
261 |
|
---|
262 | if (currentPrimitive.isDeleted()) {
|
---|
263 | logger.info(tr("Deleted relation {0} contains members", currentPrimitive.getUniqueId()));
|
---|
264 | } else {
|
---|
265 | list.add(emd);
|
---|
266 | }
|
---|
267 |
|
---|
268 | // ---- PARSING TAGS (applicable to all objects) ----
|
---|
269 |
|
---|
270 | } else if (qName.equals("tag")) {
|
---|
271 | String key = atts.getValue("k");
|
---|
272 | String value = atts.getValue("v");
|
---|
273 | currentPrimitive.put(intern(key), intern(value));
|
---|
274 |
|
---|
275 | } else {
|
---|
276 | System.out.println(tr("Undefined element ''{0}'' found in input stream. Skipping.", qName));
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | private double getDouble(Attributes atts, String value) {
|
---|
281 | return Double.parseDouble(atts.getValue(value));
|
---|
282 | }
|
---|
283 |
|
---|
284 | private User createUser(String uid, String name) throws SAXException {
|
---|
285 | if (uid == null) {
|
---|
286 | if (name == null)
|
---|
287 | return null;
|
---|
288 | return User.createLocalUser(name);
|
---|
289 | }
|
---|
290 | try {
|
---|
291 | long id = Long.parseLong(uid);
|
---|
292 | return User.createOsmUser(id, name);
|
---|
293 | } catch(NumberFormatException e) {
|
---|
294 | throwException(MessageFormat.format("Illegal value for attribute ''uid''. Got ''{0}''.", uid));
|
---|
295 | }
|
---|
296 | return null;
|
---|
297 | }
|
---|
298 | /**
|
---|
299 | * Read out the common attributes from atts and put them into this.current.
|
---|
300 | */
|
---|
301 | void readCommon(Attributes atts, PrimitiveData current) throws SAXException {
|
---|
302 | current.setId(getLong(atts, "id"));
|
---|
303 | if (current.getUniqueId() == 0) {
|
---|
304 | throwException(tr("Illegal object with ID=0."));
|
---|
305 | }
|
---|
306 |
|
---|
307 | String time = atts.getValue("timestamp");
|
---|
308 | if (time != null && time.length() != 0) {
|
---|
309 | current.setTimestamp(DateUtils.fromString(time));
|
---|
310 | }
|
---|
311 |
|
---|
312 | // user attribute added in 0.4 API
|
---|
313 | String user = atts.getValue("user");
|
---|
314 | // uid attribute added in 0.6 API
|
---|
315 | String uid = atts.getValue("uid");
|
---|
316 | current.setUser(createUser(uid, user));
|
---|
317 |
|
---|
318 | // visible attribute added in 0.4 API
|
---|
319 | String visible = atts.getValue("visible");
|
---|
320 | if (visible != null) {
|
---|
321 | current.setVisible(Boolean.parseBoolean(visible));
|
---|
322 | }
|
---|
323 |
|
---|
324 | String versionString = atts.getValue("version");
|
---|
325 | int version = 0;
|
---|
326 | if (versionString != null) {
|
---|
327 | try {
|
---|
328 | version = Integer.parseInt(versionString);
|
---|
329 | } catch(NumberFormatException e) {
|
---|
330 | throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.getUniqueId()), versionString));
|
---|
331 | }
|
---|
332 | if (ds.getVersion().equals("0.6")){
|
---|
333 | if (version <= 0 && current.getUniqueId() > 0) {
|
---|
334 | throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.getUniqueId()), versionString));
|
---|
335 | } else if (version < 0 && current.getUniqueId() <= 0) {
|
---|
336 | System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 0, "0.6"));
|
---|
337 | version = 0;
|
---|
338 | }
|
---|
339 | } else if (ds.getVersion().equals("0.5")) {
|
---|
340 | if (version <= 0 && current.getUniqueId() > 0) {
|
---|
341 | System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 1, "0.5"));
|
---|
342 | version = 1;
|
---|
343 | } else if (version < 0 && current.getUniqueId() <= 0) {
|
---|
344 | System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 0, "0.5"));
|
---|
345 | version = 0;
|
---|
346 | }
|
---|
347 | } else {
|
---|
348 | // should not happen. API version has been checked before
|
---|
349 | throwException(tr("Unknown or unsupported API version. Got {0}.", ds.getVersion()));
|
---|
350 | }
|
---|
351 | } else {
|
---|
352 | // version expected for OSM primitives with an id assigned by the server (id > 0), since API 0.6
|
---|
353 | //
|
---|
354 | if (current.getUniqueId() > 0 && ds.getVersion() != null && ds.getVersion().equals("0.6")) {
|
---|
355 | throwException(tr("Missing attribute ''version'' on OSM primitive with ID {0}.", Long.toString(current.getUniqueId())));
|
---|
356 | } else if (current.getUniqueId() > 0 && ds.getVersion() != null && ds.getVersion().equals("0.5")) {
|
---|
357 | // default version in 0.5 files for existing primitives
|
---|
358 | System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 1, "0.5"));
|
---|
359 | version= 1;
|
---|
360 | } else if (current.getUniqueId() <= 0 && ds.getVersion() != null && ds.getVersion().equals("0.5")) {
|
---|
361 | // default version in 0.5 files for new primitives, no warning necessary. This is
|
---|
362 | // (was) legal in API 0.5
|
---|
363 | version= 0;
|
---|
364 | }
|
---|
365 | }
|
---|
366 | current.setVersion(version);
|
---|
367 |
|
---|
368 | String action = atts.getValue("action");
|
---|
369 | if (action == null) {
|
---|
370 | // do nothing
|
---|
371 | } else if (action.equals("delete")) {
|
---|
372 | current.setDeleted(true);
|
---|
373 | current.setModified(true);
|
---|
374 | } else if (action.equals("modify")) {
|
---|
375 | current.setModified(true);
|
---|
376 | }
|
---|
377 |
|
---|
378 | String v = atts.getValue("changeset");
|
---|
379 | if (v == null) {
|
---|
380 | current.setChangesetId(0);
|
---|
381 | } else {
|
---|
382 | try {
|
---|
383 | current.setChangesetId(Integer.parseInt(v));
|
---|
384 | } catch(NumberFormatException e) {
|
---|
385 | if (current.getUniqueId() <= 0) {
|
---|
386 | // for a new primitive we just log a warning
|
---|
387 | System.out.println(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.getUniqueId()));
|
---|
388 | current.setChangesetId(0);
|
---|
389 | } else {
|
---|
390 | // for an existing primitive this is a problem
|
---|
391 | throwException(tr("Illegal value for attribute ''changeset''. Got {0}.", v));
|
---|
392 | }
|
---|
393 | }
|
---|
394 | if (current.getChangesetId() <=0) {
|
---|
395 | if (current.getUniqueId() <= 0) {
|
---|
396 | // for a new primitive we just log a warning
|
---|
397 | System.out.println(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.getUniqueId()));
|
---|
398 | current.setChangesetId(0);
|
---|
399 | } else {
|
---|
400 | // for an existing primitive this is a problem
|
---|
401 | throwException(tr("Illegal value for attribute ''changeset''. Got {0}.", v));
|
---|
402 | }
|
---|
403 | }
|
---|
404 | }
|
---|
405 | }
|
---|
406 |
|
---|
407 | private long getLong(Attributes atts, String name) throws SAXException {
|
---|
408 | String value = atts.getValue(name);
|
---|
409 | if (value == null) {
|
---|
410 | throwException(tr("Missing required attribute ''{0}''.",name));
|
---|
411 | }
|
---|
412 | try {
|
---|
413 | return Long.parseLong(value);
|
---|
414 | } catch(NumberFormatException e) {
|
---|
415 | throwException(tr("Illegal long value for attribute ''{0}''. Got ''{1}''.",name, value));
|
---|
416 | }
|
---|
417 | return 0; // should not happen
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * Processes the ways after parsing. Rebuilds the list of nodes of each way and
|
---|
423 | * adds the way to the dataset
|
---|
424 | *
|
---|
425 | * @throws IllegalDataException thrown if a data integrity problem is detected
|
---|
426 | */
|
---|
427 | protected void processWaysAfterParsing() throws IllegalDataException{
|
---|
428 | for (Long externalWayId: ways.keySet()) {
|
---|
429 | Way w = (Way)externalIdMap.get(new SimplePrimitiveId(externalWayId, OsmPrimitiveType.WAY));
|
---|
430 | List<Node> wayNodes = new ArrayList<Node>();
|
---|
431 | for (long id : ways.get(externalWayId)) {
|
---|
432 | Node n = (Node)externalIdMap.get(new SimplePrimitiveId(id, OsmPrimitiveType.NODE));
|
---|
433 | if (n == null) {
|
---|
434 | if (id <= 0)
|
---|
435 | throw new IllegalDataException (
|
---|
436 | tr("Way with external ID ''{0}'' includes missing node with external ID ''{1}''.",
|
---|
437 | externalWayId,
|
---|
438 | id));
|
---|
439 | // create an incomplete node if necessary
|
---|
440 | //
|
---|
441 | n = (Node)ds.getPrimitiveById(id,OsmPrimitiveType.NODE);
|
---|
442 | if (n == null) {
|
---|
443 | n = new Node(id);
|
---|
444 | ds.addPrimitive(n);
|
---|
445 | }
|
---|
446 | }
|
---|
447 | if (n.isDeleted()) {
|
---|
448 | logger.warning(tr("Deleted node {0} is part of way {1}", id, w.getId()));
|
---|
449 | } else {
|
---|
450 | wayNodes.add(n);
|
---|
451 | }
|
---|
452 | }
|
---|
453 | w.setNodes(wayNodes);
|
---|
454 | if (w.hasIncompleteNodes()) {
|
---|
455 | if (logger.isLoggable(Level.FINE)) {
|
---|
456 | logger.fine(tr("Way {0} with {1} nodes has incomplete nodes because at least one node was missing in the loaded data.",
|
---|
457 | externalWayId, w.getNodesCount()));
|
---|
458 | }
|
---|
459 | }
|
---|
460 | ds.addPrimitive(w);
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * Processes the parsed nodes after parsing. Just adds them to
|
---|
466 | * the dataset
|
---|
467 | *
|
---|
468 | */
|
---|
469 | protected void processNodesAfterParsing() {
|
---|
470 | for (OsmPrimitive primitive: externalIdMap.values()) {
|
---|
471 | if (primitive instanceof Node) {
|
---|
472 | this.ds.addPrimitive(primitive);
|
---|
473 | }
|
---|
474 | }
|
---|
475 | }
|
---|
476 |
|
---|
477 | /**
|
---|
478 | * Completes the parsed relations with its members.
|
---|
479 | *
|
---|
480 | * @throws IllegalDataException thrown if a data integrity problem is detected, i.e. if a
|
---|
481 | * relation member refers to a local primitive which wasn't available in the data
|
---|
482 | *
|
---|
483 | */
|
---|
484 | private void processRelationsAfterParsing() throws IllegalDataException {
|
---|
485 |
|
---|
486 | // First add all relations to make sure that when relation reference other relation, the referenced will be already in dataset
|
---|
487 | for (Long externalRelationId : relations.keySet()) {
|
---|
488 | Relation relation = (Relation) externalIdMap.get(
|
---|
489 | new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION)
|
---|
490 | );
|
---|
491 | ds.addPrimitive(relation);
|
---|
492 | }
|
---|
493 |
|
---|
494 | for (Long externalRelationId : relations.keySet()) {
|
---|
495 | Relation relation = (Relation) externalIdMap.get(
|
---|
496 | new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION)
|
---|
497 | );
|
---|
498 | List<RelationMember> relationMembers = new ArrayList<RelationMember>();
|
---|
499 | for (RelationMemberData rm : relations.get(externalRelationId)) {
|
---|
500 | OsmPrimitive primitive = null;
|
---|
501 |
|
---|
502 | // lookup the member from the map of already created primitives
|
---|
503 | primitive = externalIdMap.get(new SimplePrimitiveId(rm.id, rm.type));
|
---|
504 |
|
---|
505 | if (primitive == null) {
|
---|
506 | if (rm.id <= 0)
|
---|
507 | // relation member refers to a primitive with a negative id which was not
|
---|
508 | // found in the data. This is always a data integrity problem and we abort
|
---|
509 | // with an exception
|
---|
510 | //
|
---|
511 | throw new IllegalDataException(
|
---|
512 | tr("Relation with external id ''{0}'' refers to a missing primitive with external id ''{1}''.",
|
---|
513 | externalRelationId,
|
---|
514 | rm.id));
|
---|
515 |
|
---|
516 | // member refers to OSM primitive which was not present in the parsed data
|
---|
517 | // -> create a new incomplete primitive and add it to the dataset
|
---|
518 | //
|
---|
519 | primitive = ds.getPrimitiveById(rm.id, rm.type);
|
---|
520 | if (primitive == null) {
|
---|
521 | switch (rm.type) {
|
---|
522 | case NODE:
|
---|
523 | primitive = new Node(rm.id); break;
|
---|
524 | case WAY:
|
---|
525 | primitive = new Way(rm.id); break;
|
---|
526 | case RELATION:
|
---|
527 | primitive = new Relation(rm.id); break;
|
---|
528 | default: throw new AssertionError(); // can't happen
|
---|
529 | }
|
---|
530 |
|
---|
531 | ds.addPrimitive(primitive);
|
---|
532 | externalIdMap.put(new SimplePrimitiveId(rm.id, rm.type), primitive);
|
---|
533 | }
|
---|
534 | }
|
---|
535 | if (primitive.isDeleted()) {
|
---|
536 | logger.warning(tr("Deleted member {0} is used by relation {1}", primitive.getId(), relation.getId()));
|
---|
537 | } else {
|
---|
538 | relationMembers.add(new RelationMember(rm.role, primitive));
|
---|
539 | }
|
---|
540 | }
|
---|
541 | relation.setMembers(relationMembers);
|
---|
542 | }
|
---|
543 | }
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * Parse the given input source and return the dataset.
|
---|
547 | *
|
---|
548 | * @param source the source input stream. Must not be null.
|
---|
549 | * @param progressMonitor the progress monitor. If null, {@see NullProgressMonitor#INSTANCE} is assumed
|
---|
550 | *
|
---|
551 | * @return the dataset with the parsed data
|
---|
552 | * @throws IllegalDataException thrown if the an error was found while parsing the data from the source
|
---|
553 | * @throws IllegalArgumentException thrown if source is null
|
---|
554 | */
|
---|
555 | public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
|
---|
556 | if (progressMonitor == null) {
|
---|
557 | progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
558 | }
|
---|
559 | CheckParameterUtil.ensureParameterNotNull(source, "source");
|
---|
560 | OsmReader reader = new OsmReader();
|
---|
561 | try {
|
---|
562 | progressMonitor.beginTask(tr("Prepare OSM data...", 2));
|
---|
563 | progressMonitor.indeterminateSubTask(tr("Parsing OSM data..."));
|
---|
564 | InputSource inputSource = new InputSource(new InputStreamReader(source, "UTF-8"));
|
---|
565 | SAXParserFactory.newInstance().newSAXParser().parse(inputSource, reader.new Parser());
|
---|
566 | progressMonitor.worked(1);
|
---|
567 |
|
---|
568 | progressMonitor.indeterminateSubTask(tr("Preparing data set..."));
|
---|
569 | reader.ds.beginUpdate();
|
---|
570 | try {
|
---|
571 | reader.processNodesAfterParsing();
|
---|
572 | reader.processWaysAfterParsing();
|
---|
573 | reader.processRelationsAfterParsing();
|
---|
574 | } finally {
|
---|
575 | reader.ds.endUpdate();
|
---|
576 | }
|
---|
577 | progressMonitor.worked(1);
|
---|
578 | return reader.getDataSet();
|
---|
579 | } catch(IllegalDataException e) {
|
---|
580 | throw e;
|
---|
581 | } catch(ParserConfigurationException e) {
|
---|
582 | throw new IllegalDataException(e.getMessage(), e);
|
---|
583 | } catch(SAXException e) {
|
---|
584 | throw new IllegalDataException(e.getMessage(), e);
|
---|
585 | } catch(Exception e) {
|
---|
586 | throw new IllegalDataException(e);
|
---|
587 | } finally {
|
---|
588 | progressMonitor.finishTask();
|
---|
589 | }
|
---|
590 | }
|
---|
591 | }
|
---|