1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm.visitor;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.HashSet;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.osm.Node;
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
9 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
10 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
11 | import org.openstreetmap.josm.data.osm.Way;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Collect all nodes a specific osm primitive has.
|
---|
15 | *
|
---|
16 | * @author imi
|
---|
17 | */
|
---|
18 | public class AllNodesVisitor extends AbstractVisitor {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * The resulting nodes collected so far.
|
---|
22 | */
|
---|
23 | public Collection<Node> nodes = new HashSet<>();
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Nodes have only itself as nodes.
|
---|
27 | */
|
---|
28 | @Override
|
---|
29 | public void visit(Node n) {
|
---|
30 | nodes.add(n);
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Ways have their way nodes.
|
---|
35 | */
|
---|
36 | @Override
|
---|
37 | public void visit(Way w) {
|
---|
38 | if (w.isIncomplete()) return;
|
---|
39 | for (Node n : w.getNodes())
|
---|
40 | visit(n);
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Relations may have any number of nodes.
|
---|
45 | * FIXME: do we want to collect nodes from segs/ways that are relation members?
|
---|
46 | * if so, use AutomatchVisitor!
|
---|
47 | */
|
---|
48 | @Override
|
---|
49 | public void visit(Relation e) {
|
---|
50 | for (RelationMember m : e.getMembers())
|
---|
51 | if (m.isNode()) visit(m.getNode());
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Replies all nodes contained by the given primitives
|
---|
56 | * @param osms The OSM primitives to inspect
|
---|
57 | * @return All nodes the given primitives have.
|
---|
58 | */
|
---|
59 | public static Collection<Node> getAllNodes(Collection<? extends OsmPrimitive> osms) {
|
---|
60 | AllNodesVisitor v = new AllNodesVisitor();
|
---|
61 | for (OsmPrimitive osm : osms)
|
---|
62 | osm.accept(v);
|
---|
63 | return v.nodes;
|
---|
64 | }
|
---|
65 | }
|
---|