[josm-dev] [PATCH 06/26] Update visitors for reverse lookup

Dave Hansen dave at sr71.net
Tue Apr 29 03:02:48 BST 2008


This makes sure to keep the reverse lookup code in sync
when objects are added or deleted from data sets.


---

 core-dave/./src/org/openstreetmap/josm/data/osm/visitor/AddVisitor.java    |   12 +++++-
 core-dave/./src/org/openstreetmap/josm/data/osm/visitor/DeleteVisitor.java |   11 +++++
 core-dave/./src/org/openstreetmap/josm/data/osm/visitor/MergeVisitor.java  |   19 ++++------
 3 files changed, 31 insertions(+), 11 deletions(-)

diff -puN ./src/org/openstreetmap/josm/data/osm/visitor/AddVisitor.java~rl-visitors ./src/org/openstreetmap/josm/data/osm/visitor/AddVisitor.java
--- core/./src/org/openstreetmap/josm/data/osm/visitor/AddVisitor.java~rl-visitors	2008-04-28 18:59:25.000000000 -0700
+++ core-dave/./src/org/openstreetmap/josm/data/osm/visitor/AddVisitor.java	2008-04-28 18:59:25.000000000 -0700
@@ -2,6 +2,7 @@
 package org.openstreetmap.josm.data.osm.visitor;
 
 import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
 import org.openstreetmap.josm.data.osm.Relation;
 import org.openstreetmap.josm.data.osm.Node;
 import org.openstreetmap.josm.data.osm.Way;
@@ -20,12 +21,21 @@ public class AddVisitor implements Visit
 	public AddVisitor(DataSet ds) {
 		this.ds = ds;
 	}
-	
+	public void fixId(OsmPrimitive osm)
+	{
+		if (osm.id < 0)
+			osm.id = 0;
+	}
+	boolean debugme = false;
 	public void visit(Node n) {
+		fixId(n);
 		ds.nodes.add(n);
 	}
 	public void visit(Way w) {
+		fixId(w);
 		ds.ways.add(w);
+		for (Node n : w.nodes)
+			ds.rl.addWayToNodeMap(n, w);
 	}
 	public void visit(Relation e) {
 		ds.relations.add(e);
diff -puN ./src/org/openstreetmap/josm/data/osm/visitor/DeleteVisitor.java~rl-visitors ./src/org/openstreetmap/josm/data/osm/visitor/DeleteVisitor.java
--- core/./src/org/openstreetmap/josm/data/osm/visitor/DeleteVisitor.java~rl-visitors	2008-04-28 18:59:25.000000000 -0700
+++ core-dave/./src/org/openstreetmap/josm/data/osm/visitor/DeleteVisitor.java	2008-04-28 18:59:25.000000000 -0700
@@ -1,11 +1,14 @@
 // License: GPL. Copyright 2007 by Immanuel Scholz and others
 package org.openstreetmap.josm.data.osm.visitor;
 
+import org.openstreetmap.josm.Main;
 import org.openstreetmap.josm.data.osm.DataSet;
 import org.openstreetmap.josm.data.osm.Relation;
 import org.openstreetmap.josm.data.osm.Node;
 import org.openstreetmap.josm.data.osm.Way;
 
+import java.util.*;
+
 /**
  * Visitor, that adds the visited object to the dataset given at constructor.
  * 
@@ -22,10 +25,18 @@ public class DeleteVisitor implements Vi
 	}
 	
 	public void visit(Node n) {
+		List<Way> ways = ds.rl.waysUsingNode(n);
+		if (ways.size() != 0) {
+			Main.debug("error: removed node from data set while still in use by ways:");
+			for (Way w : ways)
+				Main.debug("   way: " + w.id);
+		}
 		ds.nodes.remove(n);
 	}
 	public void visit(Way w) {
 		ds.ways.remove(w);
+		ds.rl.removeWay(w);
+		Main.debug("DeleteVisitor() Way: " + w.id);
 	}
 	public void visit(Relation e) {
 		ds.relations.remove(e);
diff -puN ./src/org/openstreetmap/josm/data/osm/visitor/MergeVisitor.java~rl-visitors ./src/org/openstreetmap/josm/data/osm/visitor/MergeVisitor.java
--- core/./src/org/openstreetmap/josm/data/osm/visitor/MergeVisitor.java~rl-visitors	2008-04-28 18:59:25.000000000 -0700
+++ core-dave/./src/org/openstreetmap/josm/data/osm/visitor/MergeVisitor.java	2008-04-28 18:59:25.000000000 -0700
@@ -8,6 +8,9 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.Map;
 
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.command.Command;
+import org.openstreetmap.josm.command.ReplaceNodeInWayCommand;
 import org.openstreetmap.josm.data.osm.DataSet;
 import org.openstreetmap.josm.data.osm.Relation;
 import org.openstreetmap.josm.data.osm.RelationMember;
@@ -104,18 +107,14 @@ public class MergeVisitor implements Vis
 	}
 
 	private void fixWay(Way w) {
-	    boolean replacedSomething = false;
-	    LinkedList<Node> newNodes = new LinkedList<Node>();
-	    for (Node n : w.nodes) {
+	    for (int i = 0; i < w.nodes.size(); i++) {
+	    	Node n = w.nodes.get(i);
 	    	Node otherN = (Node) merged.get(n);
-	    	newNodes.add(otherN == null ? n : otherN);
-	    	if (otherN != null)
-	    		replacedSomething = true;
+	    	if (otherN == null)
+	    		continue;
+	    	Command c = new ReplaceNodeInWayCommand(w, n, otherN, i);
+	    	c.executeCommand();
 	    }
-	    if (replacedSomething) {
-	    	w.nodes.clear();
-	    	w.nodes.addAll(newNodes);
-		}
     }
 
 	private void fixRelation(Relation r) {
_




More information about the josm-dev mailing list