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 | private final boolean indirectRefs;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * The result list of primitives stored here.
|
---|
28 | */
|
---|
29 | public final Collection<OsmPrimitive> data = new HashSet<OsmPrimitive>();
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Construct a back reference counter.
|
---|
34 | * @param ds The dataset to operate on.
|
---|
35 | */
|
---|
36 | public CollectBackReferencesVisitor(DataSet ds) {
|
---|
37 | this.ds = ds;
|
---|
38 | this.indirectRefs = true;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public CollectBackReferencesVisitor(DataSet ds, boolean indirectRefs) {
|
---|
42 | this.ds = ds;
|
---|
43 | this.indirectRefs = indirectRefs;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void visit(Node n) {
|
---|
47 | for (Way w : ds.ways) {
|
---|
48 | if (w.deleted || w.incomplete) continue;
|
---|
49 | for (Node n2 : w.nodes) {
|
---|
50 | if (n == n2) {
|
---|
51 | data.add(w);
|
---|
52 | if (indirectRefs) {
|
---|
53 | visit(w);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 | checkRelationMembership(n);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void visit(Way w) {
|
---|
62 | checkRelationMembership(w);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void visit(Relation r) {
|
---|
66 | checkRelationMembership(r);
|
---|
67 | }
|
---|
68 |
|
---|
69 | private void checkRelationMembership(OsmPrimitive p) {
|
---|
70 | // FIXME - this might be a candidate for optimisation
|
---|
71 | // if OSM primitives are made to hold a list of back
|
---|
72 | // references.
|
---|
73 | for (Relation r : ds.relations) {
|
---|
74 | if (r.incomplete || r.deleted) continue;
|
---|
75 | for (RelationMember m : r.members) {
|
---|
76 | if (m.member == p) {
|
---|
77 | if (!data.contains(r)) {
|
---|
78 | data.add(r);
|
---|
79 | if (indirectRefs) {
|
---|
80 | // move up the tree (there might be relations
|
---|
81 | // referring to this relation)
|
---|
82 | checkRelationMembership(r);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | break;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|