source: josm/trunk/src/org/openstreetmap/josm/data/osm/visitor/CollectBackReferencesVisitor.java@ 407

Last change on this file since 407 was 407, checked in by gebner, 17 years ago

Add incompleteness checks.

File size: 2.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.osm.visitor;
3
4import java.util.Collection;
5import java.util.HashSet;
6
7import org.openstreetmap.josm.data.osm.DataSet;
8import org.openstreetmap.josm.data.osm.Relation;
9import org.openstreetmap.josm.data.osm.RelationMember;
10import org.openstreetmap.josm.data.osm.Node;
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12import 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 */
21public 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 for (RelationMember m : r.members) {
75 if (m.member == p) {
76 if (!data.contains(r)) {
77 data.add(r);
78 if (indirectRefs) {
79 // move up the tree (there might be relations
80 // referring to this relation)
81 checkRelationMembership(r);
82 }
83 }
84 break;
85 }
86 }
87 }
88 }
89}
Note: See TracBrowser for help on using the repository browser.