source: josm/trunk/src/org/openstreetmap/josm/io/AbstractReader.java@ 6990

Last change on this file since 6990 was 6248, checked in by Don-vip, 11 years ago

Rework console output:

  • new log level "error"
  • Replace nearly all calls to system.out and system.err to Main.(error|warn|info|debug)
  • Remove some unnecessary debug output
  • Some messages are modified (removal of "Info", "Warning", "Error" from the message itself -> notable i18n impact but limited to console error messages not seen by the majority of users, so that's ok)
File size: 8.3 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.osm.Changeset;
14import org.openstreetmap.josm.data.osm.DataSet;
15import org.openstreetmap.josm.data.osm.Node;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
18import org.openstreetmap.josm.data.osm.PrimitiveId;
19import org.openstreetmap.josm.data.osm.Relation;
20import org.openstreetmap.josm.data.osm.RelationMember;
21import org.openstreetmap.josm.data.osm.RelationMemberData;
22import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
23import org.openstreetmap.josm.data.osm.Way;
24
25/**
26 * Abstract Reader, allowing other implementations than OsmReader (PbfReader in PBF plugin for example)
27 * @author Vincent
28 *
29 */
30public abstract class AbstractReader {
31
32 /**
33 * The dataset to add parsed objects to.
34 */
35 protected DataSet ds = new DataSet();
36
37 protected Changeset uploadChangeset;
38
39 /** the map from external ids to read OsmPrimitives. External ids are
40 * longs too, but in contrast to internal ids negative values are used
41 * to identify primitives unknown to the OSM server
42 */
43 protected final Map<PrimitiveId, OsmPrimitive> externalIdMap = new HashMap<PrimitiveId, OsmPrimitive>();
44
45 /**
46 * Data structure for the remaining way objects
47 */
48 protected final Map<Long, Collection<Long>> ways = new HashMap<Long, Collection<Long>>();
49
50 /**
51 * Data structure for relation objects
52 */
53 protected final Map<Long, Collection<RelationMemberData>> relations = new HashMap<Long, Collection<RelationMemberData>>();
54
55 /**
56 * Replies the parsed data set
57 *
58 * @return the parsed data set
59 */
60 public DataSet getDataSet() {
61 return ds;
62 }
63
64 /**
65 * Processes the parsed nodes after parsing. Just adds them to
66 * the dataset
67 *
68 */
69 protected void processNodesAfterParsing() {
70 for (OsmPrimitive primitive: externalIdMap.values()) {
71 if (primitive instanceof Node) {
72 this.ds.addPrimitive(primitive);
73 }
74 }
75 }
76
77 /**
78 * Processes the ways after parsing. Rebuilds the list of nodes of each way and
79 * adds the way to the dataset
80 *
81 * @throws IllegalDataException thrown if a data integrity problem is detected
82 */
83 protected void processWaysAfterParsing() throws IllegalDataException{
84 for (Long externalWayId: ways.keySet()) {
85 Way w = (Way)externalIdMap.get(new SimplePrimitiveId(externalWayId, OsmPrimitiveType.WAY));
86 List<Node> wayNodes = new ArrayList<Node>();
87 for (long id : ways.get(externalWayId)) {
88 Node n = (Node)externalIdMap.get(new SimplePrimitiveId(id, OsmPrimitiveType.NODE));
89 if (n == null) {
90 if (id <= 0)
91 throw new IllegalDataException (
92 tr("Way with external ID ''{0}'' includes missing node with external ID ''{1}''.",
93 externalWayId,
94 id));
95 // create an incomplete node if necessary
96 //
97 n = (Node)ds.getPrimitiveById(id,OsmPrimitiveType.NODE);
98 if (n == null) {
99 n = new Node(id);
100 ds.addPrimitive(n);
101 }
102 }
103 if (n.isDeleted()) {
104 Main.info(tr("Deleted node {0} is part of way {1}", id, w.getId()));
105 } else {
106 wayNodes.add(n);
107 }
108 }
109 w.setNodes(wayNodes);
110 if (w.hasIncompleteNodes()) {
111 Main.info(tr("Way {0} with {1} nodes has incomplete nodes because at least one node was missing in the loaded data.",
112 externalWayId, w.getNodesCount()));
113 }
114 ds.addPrimitive(w);
115 }
116 }
117
118 /**
119 * Completes the parsed relations with its members.
120 *
121 * @throws IllegalDataException thrown if a data integrity problem is detected, i.e. if a
122 * relation member refers to a local primitive which wasn't available in the data
123 *
124 */
125 protected void processRelationsAfterParsing() throws IllegalDataException {
126
127 // First add all relations to make sure that when relation reference other relation, the referenced will be already in dataset
128 for (Long externalRelationId : relations.keySet()) {
129 Relation relation = (Relation) externalIdMap.get(
130 new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION)
131 );
132 ds.addPrimitive(relation);
133 }
134
135 for (Long externalRelationId : relations.keySet()) {
136 Relation relation = (Relation) externalIdMap.get(
137 new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION)
138 );
139 List<RelationMember> relationMembers = new ArrayList<RelationMember>();
140 for (RelationMemberData rm : relations.get(externalRelationId)) {
141 OsmPrimitive primitive = null;
142
143 // lookup the member from the map of already created primitives
144 primitive = externalIdMap.get(new SimplePrimitiveId(rm.getMemberId(), rm.getMemberType()));
145
146 if (primitive == null) {
147 if (rm.getMemberId() <= 0)
148 // relation member refers to a primitive with a negative id which was not
149 // found in the data. This is always a data integrity problem and we abort
150 // with an exception
151 //
152 throw new IllegalDataException(
153 tr("Relation with external id ''{0}'' refers to a missing primitive with external id ''{1}''.",
154 externalRelationId,
155 rm.getMemberId()));
156
157 // member refers to OSM primitive which was not present in the parsed data
158 // -> create a new incomplete primitive and add it to the dataset
159 //
160 primitive = ds.getPrimitiveById(rm.getMemberId(), rm.getMemberType());
161 if (primitive == null) {
162 switch (rm.getMemberType()) {
163 case NODE:
164 primitive = new Node(rm.getMemberId()); break;
165 case WAY:
166 primitive = new Way(rm.getMemberId()); break;
167 case RELATION:
168 primitive = new Relation(rm.getMemberId()); break;
169 default: throw new AssertionError(); // can't happen
170 }
171
172 ds.addPrimitive(primitive);
173 externalIdMap.put(new SimplePrimitiveId(rm.getMemberId(), rm.getMemberType()), primitive);
174 }
175 }
176 if (primitive.isDeleted()) {
177 Main.info(tr("Deleted member {0} is used by relation {1}", primitive.getId(), relation.getId()));
178 } else {
179 relationMembers.add(new RelationMember(rm.getRole(), primitive));
180 }
181 }
182 relation.setMembers(relationMembers);
183 }
184 }
185
186 protected void processChangesetAfterParsing() {
187 if (uploadChangeset != null) {
188 for (Map.Entry<String, String> e : uploadChangeset.getKeys().entrySet()) {
189 ds.addChangeSetTag(e.getKey(), e.getValue());
190 }
191 }
192 }
193
194 protected final void prepareDataSet() throws IllegalDataException {
195 try {
196 ds.beginUpdate();
197 processNodesAfterParsing();
198 processWaysAfterParsing();
199 processRelationsAfterParsing();
200 processChangesetAfterParsing();
201 } finally {
202 ds.endUpdate();
203 }
204 }
205}
Note: See TracBrowser for help on using the repository browser.