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.DataSet;
|
---|
8 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
9 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
10 | import org.openstreetmap.josm.data.osm.Node;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Helper that collect all ways a node is part of.
|
---|
16 | *
|
---|
17 | * Deleted objects are not collected.
|
---|
18 | *
|
---|
19 | * @author imi
|
---|
20 | */
|
---|
21 | public class CollectBackReferencesVisitor implements Visitor {
|
---|
22 |
|
---|
23 | private final DataSet ds;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * The result list of primitives stored here.
|
---|
27 | */
|
---|
28 | public final Collection<OsmPrimitive> data = new HashSet<OsmPrimitive>();
|
---|
29 |
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Construct a back reference counter.
|
---|
33 | * @param ds The dataset to operate on.
|
---|
34 | */
|
---|
35 | public CollectBackReferencesVisitor(DataSet ds) {
|
---|
36 | this.ds = ds;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public void visit(Node n) {
|
---|
40 | for (Way w : ds.ways) {
|
---|
41 | if (w.deleted)
|
---|
42 | continue;
|
---|
43 | for (Node n2 : w.nodes) {
|
---|
44 | if (n == n2) {
|
---|
45 | data.add(w);
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 | checkRelationMembership(n);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void visit(Way w) {
|
---|
53 | checkRelationMembership(w);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void visit(Relation r) {
|
---|
57 | checkRelationMembership(r);
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void checkRelationMembership(OsmPrimitive p) {
|
---|
61 | // FIXME - this might be a candidate for optimisation
|
---|
62 | // if OSM primitives are made to hold a list of back
|
---|
63 | // references.
|
---|
64 | for (Relation r : ds.relations) {
|
---|
65 | for (RelationMember m : r.members) {
|
---|
66 | if (m.member == p) {
|
---|
67 | data.add(r);
|
---|
68 | // move up the tree (there might be relations
|
---|
69 | // referring to this relation)
|
---|
70 | checkRelationMembership(r);
|
---|
71 | break;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|