1 | // License: GPL. For details, see LICENSE file.
|
---|
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.HashSet;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.Set;
|
---|
12 | import java.util.stream.Collectors;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
15 | import org.openstreetmap.josm.data.osm.visitor.OsmPrimitiveVisitor;
|
---|
16 | import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
|
---|
17 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
18 | import org.openstreetmap.josm.tools.CopyList;
|
---|
19 | import org.openstreetmap.josm.tools.Geometry;
|
---|
20 | import org.openstreetmap.josm.tools.Pair;
|
---|
21 | import org.openstreetmap.josm.tools.Utils;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * One full way, consisting of a list of way {@link Node nodes}.
|
---|
25 | *
|
---|
26 | * @author imi
|
---|
27 | * @since 64
|
---|
28 | */
|
---|
29 | public final class Way extends OsmPrimitive implements IWay<Node> {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * All way nodes in this way
|
---|
33 | */
|
---|
34 | private Node[] nodes = new Node[0];
|
---|
35 | private BBox bbox;
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public List<Node> getNodes() {
|
---|
39 | return new CopyList<>(nodes);
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Set new list of nodes to way. This method is preferred to multiple calls to addNode/removeNode
|
---|
44 | * and similar methods because nodes are internally saved as array which means lower memory overhead
|
---|
45 | * but also slower modifying operations.
|
---|
46 | * @param nodes New way nodes. Can be null, in that case all way nodes are removed
|
---|
47 | * @since 1862
|
---|
48 | */
|
---|
49 | public void setNodes(List<Node> nodes) {
|
---|
50 | checkDatasetNotReadOnly();
|
---|
51 | boolean locked = writeLock();
|
---|
52 | try {
|
---|
53 | for (Node node:this.nodes) {
|
---|
54 | node.removeReferrer(this);
|
---|
55 | node.clearCachedStyle();
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (nodes == null) {
|
---|
59 | this.nodes = new Node[0];
|
---|
60 | } else {
|
---|
61 | this.nodes = nodes.toArray(new Node[0]);
|
---|
62 | }
|
---|
63 | for (Node node: this.nodes) {
|
---|
64 | node.addReferrer(this);
|
---|
65 | node.clearCachedStyle();
|
---|
66 | }
|
---|
67 |
|
---|
68 | clearCachedStyle();
|
---|
69 | fireNodesChanged();
|
---|
70 | } finally {
|
---|
71 | writeUnlock(locked);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Prevent directly following identical nodes in ways.
|
---|
77 | * @param nodes list of nodes
|
---|
78 | * @return {@code nodes} with consecutive identical nodes removed
|
---|
79 | */
|
---|
80 | private static List<Node> removeDouble(List<Node> nodes) {
|
---|
81 | Node last = null;
|
---|
82 | int count = nodes.size();
|
---|
83 | for (int i = 0; i < count && count > 2;) {
|
---|
84 | Node n = nodes.get(i);
|
---|
85 | if (last == n) {
|
---|
86 | nodes.remove(i);
|
---|
87 | --count;
|
---|
88 | } else {
|
---|
89 | last = n;
|
---|
90 | ++i;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | return nodes;
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public int getNodesCount() {
|
---|
98 | return nodes.length;
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public Node getNode(int index) {
|
---|
103 | return nodes[index];
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public long getNodeId(int idx) {
|
---|
108 | return nodes[idx].getUniqueId();
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public List<Long> getNodeIds() {
|
---|
113 | return Arrays.stream(nodes).map(Node::getId).collect(Collectors.toList());
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Replies true if this way contains the node <code>node</code>, false
|
---|
118 | * otherwise. Replies false if <code>node</code> is null.
|
---|
119 | *
|
---|
120 | * @param node the node. May be null.
|
---|
121 | * @return true if this way contains the node <code>node</code>, false
|
---|
122 | * otherwise
|
---|
123 | * @since 1911
|
---|
124 | */
|
---|
125 | public boolean containsNode(Node node) {
|
---|
126 | if (node == null) return false;
|
---|
127 |
|
---|
128 | Node[] nodes = this.nodes;
|
---|
129 | for (Node n : nodes) {
|
---|
130 | if (n.equals(node))
|
---|
131 | return true;
|
---|
132 | }
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Return nodes adjacent to <code>node</code>
|
---|
138 | *
|
---|
139 | * @param node the node. May be null.
|
---|
140 | * @return Set of nodes adjacent to <code>node</code>
|
---|
141 | * @since 4671
|
---|
142 | */
|
---|
143 | public Set<Node> getNeighbours(Node node) {
|
---|
144 | Set<Node> neigh = new HashSet<>();
|
---|
145 |
|
---|
146 | if (node == null) return neigh;
|
---|
147 |
|
---|
148 | Node[] nodes = this.nodes;
|
---|
149 | for (int i = 0; i < nodes.length; i++) {
|
---|
150 | if (nodes[i].equals(node)) {
|
---|
151 | if (i > 0)
|
---|
152 | neigh.add(nodes[i-1]);
|
---|
153 | if (i < nodes.length-1)
|
---|
154 | neigh.add(nodes[i+1]);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | return neigh;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Replies the ordered {@link List} of chunks of this way. Each chunk is replied as a {@link Pair} of {@link Node nodes}.
|
---|
162 | * @param sort If true, the nodes of each pair are sorted as defined by {@link Pair#sort}.
|
---|
163 | * If false, Pair.a and Pair.b are in the way order
|
---|
164 | * (i.e for a given Pair(n), Pair(n-1).b == Pair(n).a, Pair(n).b == Pair(n+1).a, etc.)
|
---|
165 | * @return The ordered list of chunks of this way.
|
---|
166 | * @since 3348
|
---|
167 | */
|
---|
168 | public List<Pair<Node, Node>> getNodePairs(boolean sort) {
|
---|
169 | List<Pair<Node, Node>> chunkSet = new ArrayList<>();
|
---|
170 | if (isIncomplete()) return chunkSet;
|
---|
171 | Node lastN = null;
|
---|
172 | Node[] nodes = this.nodes;
|
---|
173 | for (Node n : nodes) {
|
---|
174 | if (lastN == null) {
|
---|
175 | lastN = n;
|
---|
176 | continue;
|
---|
177 | }
|
---|
178 | Pair<Node, Node> np = new Pair<>(lastN, n);
|
---|
179 | if (sort) {
|
---|
180 | Pair.sort(np);
|
---|
181 | }
|
---|
182 | chunkSet.add(np);
|
---|
183 | lastN = n;
|
---|
184 | }
|
---|
185 | return chunkSet;
|
---|
186 | }
|
---|
187 |
|
---|
188 | @Override public void accept(OsmPrimitiveVisitor visitor) {
|
---|
189 | visitor.visit(this);
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Override public void accept(PrimitiveVisitor visitor) {
|
---|
193 | visitor.visit(this);
|
---|
194 | }
|
---|
195 |
|
---|
196 | protected Way(long id, boolean allowNegative) {
|
---|
197 | super(id, allowNegative);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Contructs a new {@code Way} with id 0.
|
---|
202 | * @since 86
|
---|
203 | */
|
---|
204 | public Way() {
|
---|
205 | super(0, false);
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Contructs a new {@code Way} from an existing {@code Way}.
|
---|
210 | * @param original The original {@code Way} to be identically cloned. Must not be null
|
---|
211 | * @param clearMetadata If {@code true}, clears the OSM id and other metadata as defined by {@link #clearOsmMetadata}.
|
---|
212 | * If {@code false}, does nothing
|
---|
213 | * @since 2410
|
---|
214 | */
|
---|
215 | public Way(Way original, boolean clearMetadata) {
|
---|
216 | super(original.getUniqueId(), true);
|
---|
217 | cloneFrom(original);
|
---|
218 | if (clearMetadata) {
|
---|
219 | clearOsmMetadata();
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Contructs a new {@code Way} from an existing {@code Way} (including its id).
|
---|
225 | * @param original The original {@code Way} to be identically cloned. Must not be null
|
---|
226 | * @since 86
|
---|
227 | */
|
---|
228 | public Way(Way original) {
|
---|
229 | this(original, false);
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Contructs a new {@code Way} for the given id. If the id > 0, the way is marked
|
---|
234 | * as incomplete. If id == 0 then way is marked as new
|
---|
235 | *
|
---|
236 | * @param id the id. >= 0 required
|
---|
237 | * @throws IllegalArgumentException if id < 0
|
---|
238 | * @since 343
|
---|
239 | */
|
---|
240 | public Way(long id) {
|
---|
241 | super(id, false);
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Contructs a new {@code Way} with given id and version.
|
---|
246 | * @param id the id. >= 0 required
|
---|
247 | * @param version the version
|
---|
248 | * @throws IllegalArgumentException if id < 0
|
---|
249 | * @since 2620
|
---|
250 | */
|
---|
251 | public Way(long id, int version) {
|
---|
252 | super(id, version, false);
|
---|
253 | }
|
---|
254 |
|
---|
255 | @Override
|
---|
256 | public void load(PrimitiveData data) {
|
---|
257 | if (!(data instanceof WayData))
|
---|
258 | throw new IllegalArgumentException("Not a way data: " + data);
|
---|
259 | boolean locked = writeLock();
|
---|
260 | try {
|
---|
261 | super.load(data);
|
---|
262 |
|
---|
263 | List<Long> nodeIds = ((WayData) data).getNodeIds();
|
---|
264 |
|
---|
265 | if (!nodeIds.isEmpty() && getDataSet() == null) {
|
---|
266 | throw new AssertionError("Data consistency problem - way without dataset detected");
|
---|
267 | }
|
---|
268 |
|
---|
269 | List<Node> newNodes = new ArrayList<>(nodeIds.size());
|
---|
270 | for (Long nodeId : nodeIds) {
|
---|
271 | Node node = (Node) getDataSet().getPrimitiveById(nodeId, OsmPrimitiveType.NODE);
|
---|
272 | if (node != null) {
|
---|
273 | newNodes.add(node);
|
---|
274 | } else {
|
---|
275 | throw new AssertionError("Data consistency problem - way with missing node detected");
|
---|
276 | }
|
---|
277 | }
|
---|
278 | setNodes(newNodes);
|
---|
279 | } finally {
|
---|
280 | writeUnlock(locked);
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | @Override
|
---|
285 | public WayData save() {
|
---|
286 | WayData data = new WayData();
|
---|
287 | saveCommonAttributes(data);
|
---|
288 | for (Node node:nodes) {
|
---|
289 | data.getNodeIds().add(node.getUniqueId());
|
---|
290 | }
|
---|
291 | return data;
|
---|
292 | }
|
---|
293 |
|
---|
294 | @Override
|
---|
295 | public void cloneFrom(OsmPrimitive osm) {
|
---|
296 | if (!(osm instanceof Way))
|
---|
297 | throw new IllegalArgumentException("Not a way: " + osm);
|
---|
298 | boolean locked = writeLock();
|
---|
299 | try {
|
---|
300 | super.cloneFrom(osm);
|
---|
301 | Way otherWay = (Way) osm;
|
---|
302 | setNodes(otherWay.getNodes());
|
---|
303 | } finally {
|
---|
304 | writeUnlock(locked);
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | @Override
|
---|
309 | public String toString() {
|
---|
310 | String nodesDesc = isIncomplete() ? "(incomplete)" : ("nodes=" + Arrays.toString(nodes));
|
---|
311 | return "{Way id=" + getUniqueId() + " version=" + getVersion()+ ' ' + getFlagsAsString() + ' ' + nodesDesc + '}';
|
---|
312 | }
|
---|
313 |
|
---|
314 | @Override
|
---|
315 | public boolean hasEqualSemanticAttributes(OsmPrimitive other, boolean testInterestingTagsOnly) {
|
---|
316 | if (!(other instanceof Way))
|
---|
317 | return false;
|
---|
318 | Way w = (Way) other;
|
---|
319 | if (getNodesCount() != w.getNodesCount()) return false;
|
---|
320 | if (!super.hasEqualSemanticAttributes(other, testInterestingTagsOnly))
|
---|
321 | return false;
|
---|
322 | for (int i = 0; i < getNodesCount(); i++) {
|
---|
323 | if (!getNode(i).hasEqualSemanticAttributes(w.getNode(i)))
|
---|
324 | return false;
|
---|
325 | }
|
---|
326 | return true;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Removes the given {@link Node} from this way. Ignored, if n is null.
|
---|
331 | * @param n The node to remove. Ignored, if null
|
---|
332 | * @since 1463
|
---|
333 | */
|
---|
334 | public void removeNode(Node n) {
|
---|
335 | checkDatasetNotReadOnly();
|
---|
336 | if (n == null || isIncomplete()) return;
|
---|
337 | boolean locked = writeLock();
|
---|
338 | try {
|
---|
339 | boolean closed = lastNode() == n && firstNode() == n;
|
---|
340 | int i;
|
---|
341 | List<Node> copy = getNodes();
|
---|
342 | while ((i = copy.indexOf(n)) >= 0) {
|
---|
343 | copy.remove(i);
|
---|
344 | }
|
---|
345 | i = copy.size();
|
---|
346 | if (closed && i > 2) {
|
---|
347 | copy.add(copy.get(0));
|
---|
348 | } else if (i >= 2 && i <= 3 && copy.get(0) == copy.get(i-1)) {
|
---|
349 | copy.remove(i-1);
|
---|
350 | }
|
---|
351 | setNodes(removeDouble(copy));
|
---|
352 | n.clearCachedStyle();
|
---|
353 | } finally {
|
---|
354 | writeUnlock(locked);
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * Removes the given set of {@link Node nodes} from this way. Ignored, if selection is null.
|
---|
360 | * @param selection The selection of nodes to remove. Ignored, if null
|
---|
361 | * @since 5408
|
---|
362 | */
|
---|
363 | public void removeNodes(Set<? extends Node> selection) {
|
---|
364 | checkDatasetNotReadOnly();
|
---|
365 | if (selection == null || isIncomplete()) return;
|
---|
366 | boolean locked = writeLock();
|
---|
367 | try {
|
---|
368 | boolean closed = isClosed() && selection.contains(lastNode());
|
---|
369 | List<Node> copy = new ArrayList<>();
|
---|
370 |
|
---|
371 | for (Node n: nodes) {
|
---|
372 | if (!selection.contains(n)) {
|
---|
373 | copy.add(n);
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | int i = copy.size();
|
---|
378 | if (closed && i > 2) {
|
---|
379 | copy.add(copy.get(0));
|
---|
380 | } else if (i >= 2 && i <= 3 && copy.get(0) == copy.get(i-1)) {
|
---|
381 | copy.remove(i-1);
|
---|
382 | }
|
---|
383 | setNodes(removeDouble(copy));
|
---|
384 | for (Node n : selection) {
|
---|
385 | n.clearCachedStyle();
|
---|
386 | }
|
---|
387 | } finally {
|
---|
388 | writeUnlock(locked);
|
---|
389 | }
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Adds a node to the end of the list of nodes. Ignored, if n is null.
|
---|
394 | *
|
---|
395 | * @param n the node. Ignored, if null
|
---|
396 | * @throws IllegalStateException if this way is marked as incomplete. We can't add a node
|
---|
397 | * to an incomplete way
|
---|
398 | * @since 1313
|
---|
399 | */
|
---|
400 | public void addNode(Node n) {
|
---|
401 | checkDatasetNotReadOnly();
|
---|
402 | if (n == null) return;
|
---|
403 |
|
---|
404 | boolean locked = writeLock();
|
---|
405 | try {
|
---|
406 | if (isIncomplete())
|
---|
407 | throw new IllegalStateException(tr("Cannot add node {0} to incomplete way {1}.", n.getId(), getId()));
|
---|
408 | clearCachedStyle();
|
---|
409 | n.addReferrer(this);
|
---|
410 | nodes = Utils.addInArrayCopy(nodes, n);
|
---|
411 | n.clearCachedStyle();
|
---|
412 | fireNodesChanged();
|
---|
413 | } finally {
|
---|
414 | writeUnlock(locked);
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Adds a node at position offs.
|
---|
420 | *
|
---|
421 | * @param offs the offset
|
---|
422 | * @param n the node. Ignored, if null.
|
---|
423 | * @throws IllegalStateException if this way is marked as incomplete. We can't add a node
|
---|
424 | * to an incomplete way
|
---|
425 | * @throws IndexOutOfBoundsException if offs is out of bounds
|
---|
426 | * @since 1313
|
---|
427 | */
|
---|
428 | public void addNode(int offs, Node n) {
|
---|
429 | checkDatasetNotReadOnly();
|
---|
430 | if (n == null) return;
|
---|
431 |
|
---|
432 | boolean locked = writeLock();
|
---|
433 | try {
|
---|
434 | if (isIncomplete())
|
---|
435 | throw new IllegalStateException(tr("Cannot add node {0} to incomplete way {1}.", n.getId(), getId()));
|
---|
436 |
|
---|
437 | clearCachedStyle();
|
---|
438 | n.addReferrer(this);
|
---|
439 | Node[] newNodes = new Node[nodes.length + 1];
|
---|
440 | System.arraycopy(nodes, 0, newNodes, 0, offs);
|
---|
441 | System.arraycopy(nodes, offs, newNodes, offs + 1, nodes.length - offs);
|
---|
442 | newNodes[offs] = n;
|
---|
443 | nodes = newNodes;
|
---|
444 | n.clearCachedStyle();
|
---|
445 | fireNodesChanged();
|
---|
446 | } finally {
|
---|
447 | writeUnlock(locked);
|
---|
448 | }
|
---|
449 | }
|
---|
450 |
|
---|
451 | @Override
|
---|
452 | public void setDeleted(boolean deleted) {
|
---|
453 | boolean locked = writeLock();
|
---|
454 | try {
|
---|
455 | for (Node n:nodes) {
|
---|
456 | if (deleted) {
|
---|
457 | n.removeReferrer(this);
|
---|
458 | } else {
|
---|
459 | n.addReferrer(this);
|
---|
460 | }
|
---|
461 | n.clearCachedStyle();
|
---|
462 | }
|
---|
463 | fireNodesChanged();
|
---|
464 | super.setDeleted(deleted);
|
---|
465 | } finally {
|
---|
466 | writeUnlock(locked);
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 | @Override
|
---|
471 | public boolean isClosed() {
|
---|
472 | if (isIncomplete()) return false;
|
---|
473 |
|
---|
474 | Node[] nodes = this.nodes;
|
---|
475 | return nodes.length >= 3 && nodes[nodes.length-1] == nodes[0];
|
---|
476 | }
|
---|
477 |
|
---|
478 | /**
|
---|
479 | * Determines if this way denotes an area (closed way with at least three distinct nodes).
|
---|
480 | * @return {@code true} if this way is closed and contains at least three distinct nodes
|
---|
481 | * @see #isClosed
|
---|
482 | * @since 5490
|
---|
483 | */
|
---|
484 | public boolean isArea() {
|
---|
485 | if (this.nodes.length >= 4 && isClosed()) {
|
---|
486 | Node distinctNode = null;
|
---|
487 | for (int i = 1; i < nodes.length-1; i++) {
|
---|
488 | if (distinctNode == null && nodes[i] != nodes[0]) {
|
---|
489 | distinctNode = nodes[i];
|
---|
490 | } else if (distinctNode != null && nodes[i] != nodes[0] && nodes[i] != distinctNode) {
|
---|
491 | return true;
|
---|
492 | }
|
---|
493 | }
|
---|
494 | }
|
---|
495 | return false;
|
---|
496 | }
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Returns the last node of this way.
|
---|
500 | * The result equals <code>{@link #getNode getNode}({@link #getNodesCount getNodesCount} - 1)</code>.
|
---|
501 | * @return the last node of this way
|
---|
502 | * @since 1400
|
---|
503 | */
|
---|
504 | public Node lastNode() {
|
---|
505 | Node[] nodes = this.nodes;
|
---|
506 | if (isIncomplete() || nodes.length == 0) return null;
|
---|
507 | return nodes[nodes.length-1];
|
---|
508 | }
|
---|
509 |
|
---|
510 | /**
|
---|
511 | * Returns the first node of this way.
|
---|
512 | * The result equals {@link #getNode getNode}{@code (0)}.
|
---|
513 | * @return the first node of this way
|
---|
514 | * @since 1400
|
---|
515 | */
|
---|
516 | public Node firstNode() {
|
---|
517 | Node[] nodes = this.nodes;
|
---|
518 | if (isIncomplete() || nodes.length == 0) return null;
|
---|
519 | return nodes[0];
|
---|
520 | }
|
---|
521 |
|
---|
522 | /**
|
---|
523 | * Replies true if the given node is the first or the last one of this way, false otherwise.
|
---|
524 | * @param n The node to test
|
---|
525 | * @return true if the {@code n} is the first or the last node, false otherwise.
|
---|
526 | * @since 1400
|
---|
527 | */
|
---|
528 | public boolean isFirstLastNode(Node n) {
|
---|
529 | Node[] nodes = this.nodes;
|
---|
530 | if (isIncomplete() || nodes.length == 0) return false;
|
---|
531 | return n == nodes[0] || n == nodes[nodes.length -1];
|
---|
532 | }
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * Replies true if the given node is an inner node of this way, false otherwise.
|
---|
536 | * @param n The node to test
|
---|
537 | * @return true if the {@code n} is an inner node, false otherwise.
|
---|
538 | * @since 3515
|
---|
539 | */
|
---|
540 | public boolean isInnerNode(Node n) {
|
---|
541 | Node[] nodes = this.nodes;
|
---|
542 | if (isIncomplete() || nodes.length <= 2) return false;
|
---|
543 | /* circular ways have only inner nodes, so return true for them! */
|
---|
544 | if (n == nodes[0] && n == nodes[nodes.length-1]) return true;
|
---|
545 | for (int i = 1; i < nodes.length - 1; ++i) {
|
---|
546 | if (nodes[i] == n) return true;
|
---|
547 | }
|
---|
548 | return false;
|
---|
549 | }
|
---|
550 |
|
---|
551 | @Override
|
---|
552 | public OsmPrimitiveType getType() {
|
---|
553 | return OsmPrimitiveType.WAY;
|
---|
554 | }
|
---|
555 |
|
---|
556 | @Override
|
---|
557 | public OsmPrimitiveType getDisplayType() {
|
---|
558 | return isClosed() ? OsmPrimitiveType.CLOSEDWAY : OsmPrimitiveType.WAY;
|
---|
559 | }
|
---|
560 |
|
---|
561 | private void checkNodes() {
|
---|
562 | DataSet dataSet = getDataSet();
|
---|
563 | if (dataSet != null) {
|
---|
564 | Node[] nodes = this.nodes;
|
---|
565 | for (Node n: nodes) {
|
---|
566 | if (n.getDataSet() != dataSet)
|
---|
567 | throw new DataIntegrityProblemException("Nodes in way must be in the same dataset",
|
---|
568 | tr("Nodes in way must be in the same dataset"));
|
---|
569 | if (n.isDeleted())
|
---|
570 | throw new DataIntegrityProblemException("Deleted node referenced: " + toString(),
|
---|
571 | "<html>" + tr("Deleted node referenced by {0}",
|
---|
572 | DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(this)) + "</html>");
|
---|
573 | }
|
---|
574 | if (Config.getPref().getBoolean("debug.checkNullCoor", true)) {
|
---|
575 | for (Node n: nodes) {
|
---|
576 | if (n.isVisible() && !n.isIncomplete() && !n.isLatLonKnown())
|
---|
577 | throw new DataIntegrityProblemException("Complete visible node with null coordinates: " + toString(),
|
---|
578 | "<html>" + tr("Complete node {0} with null coordinates in way {1}",
|
---|
579 | DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(n),
|
---|
580 | DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(this)) + "</html>");
|
---|
581 | }
|
---|
582 | }
|
---|
583 | }
|
---|
584 | }
|
---|
585 |
|
---|
586 | private void fireNodesChanged() {
|
---|
587 | checkNodes();
|
---|
588 | if (getDataSet() != null) {
|
---|
589 | getDataSet().fireWayNodesChanged(this);
|
---|
590 | }
|
---|
591 | }
|
---|
592 |
|
---|
593 | @Override
|
---|
594 | void setDataset(DataSet dataSet) {
|
---|
595 | super.setDataset(dataSet);
|
---|
596 | checkNodes();
|
---|
597 | }
|
---|
598 |
|
---|
599 | @Override
|
---|
600 | public BBox getBBox() {
|
---|
601 | if (getDataSet() == null)
|
---|
602 | return new BBox(this);
|
---|
603 | if (bbox == null) {
|
---|
604 | bbox = new BBox(this);
|
---|
605 | }
|
---|
606 | return new BBox(bbox);
|
---|
607 | }
|
---|
608 |
|
---|
609 | @Override
|
---|
610 | protected void addToBBox(BBox box, Set<PrimitiveId> visited) {
|
---|
611 | box.add(getBBox());
|
---|
612 | }
|
---|
613 |
|
---|
614 | @Override
|
---|
615 | public void updatePosition() {
|
---|
616 | bbox = new BBox(this);
|
---|
617 | }
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * Replies true if this way has incomplete nodes, false otherwise.
|
---|
621 | * @return true if this way has incomplete nodes, false otherwise.
|
---|
622 | * @since 2587
|
---|
623 | */
|
---|
624 | public boolean hasIncompleteNodes() {
|
---|
625 | Node[] nodes = this.nodes;
|
---|
626 | for (Node node : nodes) {
|
---|
627 | if (node.isIncomplete())
|
---|
628 | return true;
|
---|
629 | }
|
---|
630 | return false;
|
---|
631 | }
|
---|
632 |
|
---|
633 | /**
|
---|
634 | * Replies true if all nodes of the way have known lat/lon, false otherwise.
|
---|
635 | * @return true if all nodes of the way have known lat/lon, false otherwise
|
---|
636 | * @since 13033
|
---|
637 | */
|
---|
638 | public boolean hasOnlyLocatableNodes() {
|
---|
639 | Node[] nodes = this.nodes;
|
---|
640 | for (Node node : nodes) {
|
---|
641 | if (!node.isLatLonKnown())
|
---|
642 | return false;
|
---|
643 | }
|
---|
644 | return true;
|
---|
645 | }
|
---|
646 |
|
---|
647 | @Override
|
---|
648 | public boolean isUsable() {
|
---|
649 | return super.isUsable() && !hasIncompleteNodes();
|
---|
650 | }
|
---|
651 |
|
---|
652 | @Override
|
---|
653 | public boolean isDrawable() {
|
---|
654 | return super.isDrawable() && hasOnlyLocatableNodes();
|
---|
655 | }
|
---|
656 |
|
---|
657 | /**
|
---|
658 | * Replies the length of the way, in metres, as computed by {@link LatLon#greatCircleDistance}.
|
---|
659 | * @return The length of the way, in metres
|
---|
660 | * @since 4138
|
---|
661 | */
|
---|
662 | public double getLength() {
|
---|
663 | double length = 0;
|
---|
664 | Node lastN = null;
|
---|
665 | for (Node n:nodes) {
|
---|
666 | if (lastN != null) {
|
---|
667 | LatLon lastNcoor = lastN.getCoor();
|
---|
668 | LatLon coor = n.getCoor();
|
---|
669 | if (lastNcoor != null && coor != null) {
|
---|
670 | length += coor.greatCircleDistance(lastNcoor);
|
---|
671 | }
|
---|
672 | }
|
---|
673 | lastN = n;
|
---|
674 | }
|
---|
675 | return length;
|
---|
676 | }
|
---|
677 |
|
---|
678 | /**
|
---|
679 | * Replies the length of the longest segment of the way, in metres, as computed by {@link LatLon#greatCircleDistance}.
|
---|
680 | * @return The length of the segment, in metres
|
---|
681 | * @since 8320
|
---|
682 | */
|
---|
683 | public double getLongestSegmentLength() {
|
---|
684 | double length = 0;
|
---|
685 | Node lastN = null;
|
---|
686 | for (Node n:nodes) {
|
---|
687 | if (lastN != null) {
|
---|
688 | LatLon lastNcoor = lastN.getCoor();
|
---|
689 | LatLon coor = n.getCoor();
|
---|
690 | if (lastNcoor != null && coor != null) {
|
---|
691 | double l = coor.greatCircleDistance(lastNcoor);
|
---|
692 | if (l > length) {
|
---|
693 | length = l;
|
---|
694 | }
|
---|
695 | }
|
---|
696 | }
|
---|
697 | lastN = n;
|
---|
698 | }
|
---|
699 | return length;
|
---|
700 | }
|
---|
701 |
|
---|
702 | /**
|
---|
703 | * Tests if this way is a oneway.
|
---|
704 | * @return {@code 1} if the way is a oneway,
|
---|
705 | * {@code -1} if the way is a reversed oneway,
|
---|
706 | * {@code 0} otherwise.
|
---|
707 | * @since 5199
|
---|
708 | */
|
---|
709 | public int isOneway() {
|
---|
710 | String oneway = get("oneway");
|
---|
711 | if (oneway != null) {
|
---|
712 | if ("-1".equals(oneway)) {
|
---|
713 | return -1;
|
---|
714 | } else {
|
---|
715 | Boolean isOneway = OsmUtils.getOsmBoolean(oneway);
|
---|
716 | if (isOneway != null && isOneway) {
|
---|
717 | return 1;
|
---|
718 | }
|
---|
719 | }
|
---|
720 | }
|
---|
721 | return 0;
|
---|
722 | }
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Replies the first node of this way, respecting or not its oneway state.
|
---|
726 | * @param respectOneway If true and if this way is a reversed oneway, replies the last node. Otherwise, replies the first node.
|
---|
727 | * @return the first node of this way, according to {@code respectOneway} and its oneway state.
|
---|
728 | * @since 5199
|
---|
729 | */
|
---|
730 | public Node firstNode(boolean respectOneway) {
|
---|
731 | return !respectOneway || isOneway() != -1 ? firstNode() : lastNode();
|
---|
732 | }
|
---|
733 |
|
---|
734 | /**
|
---|
735 | * Replies the last node of this way, respecting or not its oneway state.
|
---|
736 | * @param respectOneway If true and if this way is a reversed oneway, replies the first node. Otherwise, replies the last node.
|
---|
737 | * @return the last node of this way, according to {@code respectOneway} and its oneway state.
|
---|
738 | * @since 5199
|
---|
739 | */
|
---|
740 | public Node lastNode(boolean respectOneway) {
|
---|
741 | return !respectOneway || isOneway() != -1 ? lastNode() : firstNode();
|
---|
742 | }
|
---|
743 |
|
---|
744 | @Override
|
---|
745 | public boolean concernsArea() {
|
---|
746 | return hasAreaTags();
|
---|
747 | }
|
---|
748 |
|
---|
749 | @Override
|
---|
750 | public boolean isOutsideDownloadArea() {
|
---|
751 | for (final Node n : nodes) {
|
---|
752 | if (n.isOutsideDownloadArea()) {
|
---|
753 | return true;
|
---|
754 | }
|
---|
755 | }
|
---|
756 | return false;
|
---|
757 | }
|
---|
758 |
|
---|
759 | @Override
|
---|
760 | protected void keysChangedImpl(Map<String, String> originalKeys) {
|
---|
761 | super.keysChangedImpl(originalKeys);
|
---|
762 | clearCachedNodeStyles();
|
---|
763 | }
|
---|
764 |
|
---|
765 | /**
|
---|
766 | * Clears all cached styles for all nodes of this way. This should not be called from outside.
|
---|
767 | * @see Node#clearCachedStyle()
|
---|
768 | */
|
---|
769 | public void clearCachedNodeStyles() {
|
---|
770 | for (final Node n : nodes) {
|
---|
771 | n.clearCachedStyle();
|
---|
772 | }
|
---|
773 | }
|
---|
774 |
|
---|
775 | /**
|
---|
776 | * Returns angles of vertices.
|
---|
777 | * @return angles of the way
|
---|
778 | * @since 13670
|
---|
779 | */
|
---|
780 | public synchronized List<Pair<Double, Node>> getAngles() {
|
---|
781 | List<Pair<Double, Node>> angles = new ArrayList<>();
|
---|
782 |
|
---|
783 | for (int i = 1; i < nodes.length - 1; i++) {
|
---|
784 | Node n0 = nodes[i - 1];
|
---|
785 | Node n1 = nodes[i];
|
---|
786 | Node n2 = nodes[i + 1];
|
---|
787 |
|
---|
788 | double angle = Geometry.getNormalizedAngleInDegrees(Geometry.getCornerAngle(
|
---|
789 | n0.getEastNorth(), n1.getEastNorth(), n2.getEastNorth()));
|
---|
790 | angles.add(new Pair<>(angle, n1));
|
---|
791 | }
|
---|
792 |
|
---|
793 | angles.add(new Pair<>(Geometry.getNormalizedAngleInDegrees(Geometry.getCornerAngle(
|
---|
794 | nodes[nodes.length - 2].getEastNorth(),
|
---|
795 | nodes[0].getEastNorth(),
|
---|
796 | nodes[1].getEastNorth())), nodes[0]));
|
---|
797 |
|
---|
798 | return angles;
|
---|
799 | }
|
---|
800 | }
|
---|