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

Last change on this file since 2070 was 2070, checked in by Gubaer, 15 years ago

new: rewrite of CombineWay action
new: conflict resolution dialog for CombineWay, including conflicts for different relation memberships
cleanup: cleanup in OsmReader, reduces memory footprint and reduces parsing time
cleanup: made most of the public fields in OsmPrimitive @deprecated, added accessors and changed the code
cleanup: replaced usages of @deprecated constructors for ExtendedDialog
fixed #3208: Combine ways brokes relation order

WARNING: this changeset touches a lot of code all over the code base. "latest" might become slightly unstable in the next days. Also experience incompatibility issues with plugins in the next few days.

  • Property svn:eol-style set to native
File size: 2.6 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.Node;
9import org.openstreetmap.josm.data.osm.OsmPrimitive;
10import org.openstreetmap.josm.data.osm.Relation;
11import org.openstreetmap.josm.data.osm.RelationMember;
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 extends AbstractVisitor {
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.isDeleted() || w.incomplete) {
49 continue;
50 }
51 for (Node n2 : w.getNodes()) {
52 if (n == n2) {
53 data.add(w);
54 if (indirectRefs) {
55 visit(w);
56 }
57 }
58 }
59 }
60 checkRelationMembership(n);
61 }
62
63 public void visit(Way w) {
64 checkRelationMembership(w);
65 }
66
67 public void visit(Relation r) {
68 checkRelationMembership(r);
69 }
70
71 private void checkRelationMembership(OsmPrimitive p) {
72 // FIXME - this might be a candidate for optimisation
73 // if OSM primitives are made to hold a list of back
74 // references.
75 for (Relation r : ds.relations) {
76 if (r.incomplete || r.isDeleted()) {
77 continue;
78 }
79 for (RelationMember m : r.getMembers()) {
80 if (m.getMember() == p) {
81 if (!data.contains(r)) {
82 data.add(r);
83 if (indirectRefs) {
84 // move up the tree (there might be relations
85 // referring to this relation)
86 checkRelationMembership(r);
87 }
88 }
89 break;
90 }
91 }
92 }
93 }
94}
Note: See TracBrowser for help on using the repository browser.