1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
12 | import org.openstreetmap.josm.tools.CopyList;
|
---|
13 | import org.openstreetmap.josm.tools.Pair;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * One full way, consisting of a list of way nodes.
|
---|
17 | *
|
---|
18 | * @author imi
|
---|
19 | */
|
---|
20 | public final class Way extends OsmPrimitive {
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * All way nodes in this way
|
---|
24 | *
|
---|
25 | */
|
---|
26 | private Node[] nodes = new Node[0];
|
---|
27 | private BBox bbox;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | *
|
---|
31 | * You can modify returned list but changes will not be propagated back
|
---|
32 | * to the Way. Use {@link #setNodes(List)} to update this way
|
---|
33 | * @return Nodes composing the way
|
---|
34 | * @since 1862
|
---|
35 | */
|
---|
36 | public List<Node> getNodes() {
|
---|
37 | return new CopyList<Node>(nodes);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Set new list of nodes to way. This method is preferred to multiple calls to addNode/removeNode
|
---|
42 | * and similar methods because nodes are internally saved as array which means lower memory overhead
|
---|
43 | * but also slower modifying operations.
|
---|
44 | * @param nodes New way nodes. Can be null, in that case all way nodes are removed
|
---|
45 | * @since 1862
|
---|
46 | */
|
---|
47 | public void setNodes(List<Node> nodes) {
|
---|
48 | for (Node node:this.nodes) {
|
---|
49 | node.removeReferrer(this);
|
---|
50 | }
|
---|
51 |
|
---|
52 | if (nodes == null) {
|
---|
53 | this.nodes = new Node[0];
|
---|
54 | } else {
|
---|
55 | this.nodes = nodes.toArray(new Node[nodes.size()]);
|
---|
56 | }
|
---|
57 | for (Node node:this.nodes) {
|
---|
58 | node.addReferrer(this);
|
---|
59 | }
|
---|
60 |
|
---|
61 | clearCached();
|
---|
62 | fireNodesChanged();
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Replies the number of nodes in this ways.
|
---|
67 | *
|
---|
68 | * @return the number of nodes in this ways.
|
---|
69 | * @since 1862
|
---|
70 | */
|
---|
71 | public int getNodesCount() {
|
---|
72 | return nodes.length;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Replies the node at position <code>index</code>.
|
---|
77 | *
|
---|
78 | * @param index the position
|
---|
79 | * @return the node at position <code>index</code>
|
---|
80 | * @exception IndexOutOfBoundsException thrown if <code>index</code> < 0
|
---|
81 | * or <code>index</code> >= {@see #getNodesCount()}
|
---|
82 | * @since 1862
|
---|
83 | */
|
---|
84 | public Node getNode(int index) {
|
---|
85 | return nodes[index];
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Replies true if this way contains the node <code>node</code>, false
|
---|
90 | * otherwise. Replies false if <code>node</code> is null.
|
---|
91 | *
|
---|
92 | * @param node the node. May be null.
|
---|
93 | * @return true if this way contains the node <code>node</code>, false
|
---|
94 | * otherwise
|
---|
95 | * @since 1909
|
---|
96 | */
|
---|
97 | public boolean containsNode(Node node) {
|
---|
98 | if (node == null) return false;
|
---|
99 | for (int i=0; i<nodes.length; i++) {
|
---|
100 | if (nodes[i].equals(node))
|
---|
101 | return true;
|
---|
102 | }
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /* mappaint data */
|
---|
107 | public boolean isMappaintArea = false;
|
---|
108 | public Integer mappaintDrawnAreaCode = 0;
|
---|
109 | /* end of mappaint data */
|
---|
110 | @Override protected void clearCached() {
|
---|
111 | super.clearCached();
|
---|
112 | isMappaintArea = false;
|
---|
113 | mappaintDrawnAreaCode = 0;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public ArrayList<Pair<Node,Node>> getNodePairs(boolean sort) {
|
---|
117 | ArrayList<Pair<Node,Node>> chunkSet = new ArrayList<Pair<Node,Node>>();
|
---|
118 | if (isIncomplete()) return chunkSet;
|
---|
119 | Node lastN = null;
|
---|
120 | for (Node n : this.nodes) {
|
---|
121 | if (lastN == null) {
|
---|
122 | lastN = n;
|
---|
123 | continue;
|
---|
124 | }
|
---|
125 | Pair<Node,Node> np = new Pair<Node,Node>(lastN, n);
|
---|
126 | if (sort) {
|
---|
127 | Pair.sort(np);
|
---|
128 | }
|
---|
129 | chunkSet.add(np);
|
---|
130 | lastN = n;
|
---|
131 | }
|
---|
132 | return chunkSet;
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override public void visit(Visitor visitor) {
|
---|
136 | visitor.visit(this);
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected Way(long id, boolean allowNegative) {
|
---|
140 | super(id, allowNegative);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Creates a new way with id 0.
|
---|
145 | *
|
---|
146 | */
|
---|
147 | public Way(){
|
---|
148 | super(0, false);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | *
|
---|
153 | * @param original
|
---|
154 | * @param clearId
|
---|
155 | */
|
---|
156 | public Way(Way original, boolean clearId) {
|
---|
157 | super(original.getUniqueId(), true);
|
---|
158 | cloneFrom(original);
|
---|
159 | if (clearId) {
|
---|
160 | clearOsmId();
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Create an identical clone of the argument (including the id).
|
---|
166 | *
|
---|
167 | * @param original the original way. Must not be null.
|
---|
168 | */
|
---|
169 | public Way(Way original) {
|
---|
170 | this(original, false);
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Creates a new way for the given id. If the id > 0, the way is marked
|
---|
175 | * as incomplete.
|
---|
176 | *
|
---|
177 | * @param id the id. > 0 required
|
---|
178 | * @throws IllegalArgumentException thrown if id < 0
|
---|
179 | */
|
---|
180 | public Way(long id) throws IllegalArgumentException {
|
---|
181 | super(id, false);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | *
|
---|
186 | * @param data
|
---|
187 | */
|
---|
188 | @Override
|
---|
189 | public void load(PrimitiveData data) {
|
---|
190 | super.load(data);
|
---|
191 |
|
---|
192 | WayData wayData = (WayData) data;
|
---|
193 |
|
---|
194 | List<Node> newNodes = new ArrayList<Node>(wayData.getNodes().size());
|
---|
195 | for (Long nodeId : wayData.getNodes()) {
|
---|
196 | Node node = (Node)getDataSet().getPrimitiveById(nodeId, OsmPrimitiveType.NODE);
|
---|
197 | if (node != null) {
|
---|
198 | newNodes.add(node);
|
---|
199 | } else
|
---|
200 | throw new AssertionError("Data consistency problem - way with missing node detected");
|
---|
201 | }
|
---|
202 | setNodes(newNodes);
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override public WayData save() {
|
---|
206 | WayData data = new WayData();
|
---|
207 | saveCommonAttributes(data);
|
---|
208 | for (Node node:getNodes()) {
|
---|
209 | data.getNodes().add(node.getUniqueId());
|
---|
210 | }
|
---|
211 | return data;
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Override public void cloneFrom(OsmPrimitive osm) {
|
---|
215 | super.cloneFrom(osm);
|
---|
216 | Way otherWay = (Way)osm;
|
---|
217 | setNodes(otherWay.getNodes());
|
---|
218 | }
|
---|
219 |
|
---|
220 | @Override public String toString() {
|
---|
221 | String nodesDesc = isIncomplete()?"(incomplete)":"nodes=" + Arrays.toString(nodes);
|
---|
222 | return "{Way id=" + getUniqueId() + " version=" + getVersion()+ " " + getFlagsAsString() + " " + nodesDesc + "}";
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | public boolean hasEqualSemanticAttributes(OsmPrimitive other) {
|
---|
227 | if (other == null || ! (other instanceof Way) )
|
---|
228 | return false;
|
---|
229 | if (! super.hasEqualSemanticAttributes(other))
|
---|
230 | return false;
|
---|
231 | Way w = (Way)other;
|
---|
232 | if (getNodesCount() != w.getNodesCount()) return false;
|
---|
233 | for (int i=0;i<getNodesCount();i++) {
|
---|
234 | if (! getNode(i).hasEqualSemanticAttributes(w.getNode(i)))
|
---|
235 | return false;
|
---|
236 | }
|
---|
237 | return true;
|
---|
238 | }
|
---|
239 |
|
---|
240 | public int compareTo(OsmPrimitive o) {
|
---|
241 | if (o instanceof Relation)
|
---|
242 | return 1;
|
---|
243 | return o instanceof Way ? Long.valueOf(getUniqueId()).compareTo(o.getUniqueId()) : -1;
|
---|
244 | }
|
---|
245 |
|
---|
246 | public void removeNode(Node n) {
|
---|
247 | if (isIncomplete()) return;
|
---|
248 | boolean closed = (lastNode() == n && firstNode() == n);
|
---|
249 | int i;
|
---|
250 | List<Node> copy = getNodes();
|
---|
251 | while ((i = copy.indexOf(n)) >= 0) {
|
---|
252 | copy.remove(i);
|
---|
253 | }
|
---|
254 | i = copy.size();
|
---|
255 | if (closed && i > 2) {
|
---|
256 | copy.add(copy.get(0));
|
---|
257 | } else if (i >= 2 && i <= 3 && copy.get(0) == copy.get(i-1)) {
|
---|
258 | copy.remove(i-1);
|
---|
259 | }
|
---|
260 | setNodes(copy);
|
---|
261 | }
|
---|
262 |
|
---|
263 | public void removeNodes(Collection<? extends OsmPrimitive> selection) {
|
---|
264 | if (isIncomplete()) return;
|
---|
265 | for(OsmPrimitive p : selection) {
|
---|
266 | if (p instanceof Node) {
|
---|
267 | removeNode((Node)p);
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Adds a node to the end of the list of nodes. Ignored, if n is null.
|
---|
274 | *
|
---|
275 | * @param n the node. Ignored, if null.
|
---|
276 | * @throws IllegalStateException thrown, if this way is marked as incomplete. We can't add a node
|
---|
277 | * to an incomplete way
|
---|
278 | */
|
---|
279 | public void addNode(Node n) throws IllegalStateException {
|
---|
280 | if (n==null) return;
|
---|
281 | if (isIncomplete())
|
---|
282 | throw new IllegalStateException(tr("Cannot add node {0} to incomplete way {1}.", n.getId(), getId()));
|
---|
283 | clearCached();
|
---|
284 | n.addReferrer(this);
|
---|
285 | Node[] newNodes = new Node[nodes.length + 1];
|
---|
286 | System.arraycopy(nodes, 0, newNodes, 0, nodes.length);
|
---|
287 | newNodes[nodes.length] = n;
|
---|
288 | nodes = newNodes;
|
---|
289 | fireNodesChanged();
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Adds a node at position offs.
|
---|
294 | *
|
---|
295 | * @param int offs the offset
|
---|
296 | * @param n the node. Ignored, if null.
|
---|
297 | * @throws IllegalStateException thrown, if this way is marked as incomplete. We can't add a node
|
---|
298 | * to an incomplete way
|
---|
299 | * @throws IndexOutOfBoundsException thrown if offs is out of bounds
|
---|
300 | */
|
---|
301 | public void addNode(int offs, Node n) throws IllegalStateException, IndexOutOfBoundsException {
|
---|
302 | if (n==null) return;
|
---|
303 | if (isIncomplete())
|
---|
304 | throw new IllegalStateException(tr("Cannot add node {0} to incomplete way {1}.", n.getId(), getId()));
|
---|
305 | clearCached();
|
---|
306 | n.addReferrer(this);
|
---|
307 | Node[] newNodes = new Node[nodes.length + 1];
|
---|
308 | System.arraycopy(nodes, 0, newNodes, 0, offs);
|
---|
309 | System.arraycopy(nodes, offs, newNodes, offs + 1, nodes.length - offs);
|
---|
310 | newNodes[offs] = n;
|
---|
311 | nodes = newNodes;
|
---|
312 | fireNodesChanged();
|
---|
313 | }
|
---|
314 |
|
---|
315 | @Override
|
---|
316 | public void setDeleted(boolean deleted) {
|
---|
317 | for (Node n:nodes) {
|
---|
318 | if (deleted) {
|
---|
319 | n.removeReferrer(this);
|
---|
320 | } else {
|
---|
321 | n.addReferrer(this);
|
---|
322 | }
|
---|
323 | }
|
---|
324 | fireNodesChanged();
|
---|
325 | super.setDeleted(deleted);
|
---|
326 | }
|
---|
327 |
|
---|
328 | public boolean isClosed() {
|
---|
329 | if (isIncomplete()) return false;
|
---|
330 | return nodes.length >= 3 && lastNode() == firstNode();
|
---|
331 | }
|
---|
332 |
|
---|
333 | public Node lastNode() {
|
---|
334 | if (isIncomplete() || nodes.length == 0) return null;
|
---|
335 | return nodes[nodes.length-1];
|
---|
336 | }
|
---|
337 |
|
---|
338 | public Node firstNode() {
|
---|
339 | if (isIncomplete() || nodes.length == 0) return null;
|
---|
340 | return nodes[0];
|
---|
341 | }
|
---|
342 |
|
---|
343 | public boolean isFirstLastNode(Node n) {
|
---|
344 | if (isIncomplete() || nodes.length == 0) return false;
|
---|
345 | return n == firstNode() || n == lastNode();
|
---|
346 | }
|
---|
347 |
|
---|
348 | @Override
|
---|
349 | public String getDisplayName(NameFormatter formatter) {
|
---|
350 | return formatter.format(this);
|
---|
351 | }
|
---|
352 |
|
---|
353 | public OsmPrimitiveType getType() {
|
---|
354 | return OsmPrimitiveType.WAY;
|
---|
355 | }
|
---|
356 |
|
---|
357 | private void fireNodesChanged() {
|
---|
358 | if (getDataSet() != null) {
|
---|
359 | getDataSet().fireWayNodesChanged(this);
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | @Override
|
---|
364 | public BBox getBBox() {
|
---|
365 | if (getDataSet() == null)
|
---|
366 | return new BBox(this);
|
---|
367 | if (bbox == null) {
|
---|
368 | bbox = new BBox(this);
|
---|
369 | }
|
---|
370 | return bbox;
|
---|
371 | }
|
---|
372 |
|
---|
373 | @Override
|
---|
374 | public void updatePosition() {
|
---|
375 | bbox = new BBox(this);
|
---|
376 | }
|
---|
377 | }
|
---|