1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
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.Relation;
|
---|
8 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
9 | import org.openstreetmap.josm.data.osm.Node;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
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 implements Visitor {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * The resulting nodes collected so far.
|
---|
22 | */
|
---|
23 | public Collection<Node> nodes = new HashSet<Node>();
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Nodes have only itself as nodes.
|
---|
27 | */
|
---|
28 | public void visit(Node n) {
|
---|
29 | nodes.add(n);
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Ways have their way nodes.
|
---|
34 | */
|
---|
35 | public void visit(Way w) {
|
---|
36 | for (Node n : w.nodes)
|
---|
37 | visit(n);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Relations may have any number of nodes.
|
---|
42 | * FIXME: do we want to collect nodes from segs/ways that are relation members?
|
---|
43 | * if so, use AutomatchVisitor!
|
---|
44 | */
|
---|
45 | public void visit(Relation e) {
|
---|
46 | for (RelationMember m : e.members)
|
---|
47 | if (m.member instanceof Node) visit((Node)m.member);
|
---|
48 | }
|
---|
49 | /**
|
---|
50 | * @return All nodes the given primitive has.
|
---|
51 | */
|
---|
52 | public static Collection<Node> getAllNodes(Collection<? extends OsmPrimitive> osms) {
|
---|
53 | AllNodesVisitor v = new AllNodesVisitor();
|
---|
54 | for (OsmPrimitive osm : osms)
|
---|
55 | osm.visit(v);
|
---|
56 | return v.nodes;
|
---|
57 | }
|
---|
58 | }
|
---|