Changeset 57 in josm
- Timestamp:
- 2006-02-21T13:39:40+01:00 (19 years ago)
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/data/osm/visitor/MergeVisitor.java
r52 r57 1 1 package org.openstreetmap.josm.data.osm.visitor; 2 2 3 import java.util.HashMap; 3 4 import java.util.Iterator; 5 import java.util.LinkedList; 6 import java.util.Map; 4 7 5 8 import org.openstreetmap.josm.data.osm.DataSet; … … 20 23 private final DataSet ds; 21 24 25 /** 26 * A list of all nodes that got replaced with other nodes. 27 * Key is the node in the other's dataset and the value is the one that is now 28 * in ds.nodes instead. 29 */ 30 private final Map<Node, Node> mergedNodes = new HashMap<Node, Node>(); 31 /** 32 * A list of all line segments that got replaced with others. 33 * Key is the segment in the other's dataset and the value is the one that is now 34 * in ds.lineSegments. 35 */ 36 private final Map<LineSegment, LineSegment> mergedLineSegments = new HashMap<LineSegment, LineSegment>(); 37 22 38 public MergeVisitor(DataSet ds) { 23 39 this.ds = ds; … … 39 55 ds.nodes.add(otherNode); 40 56 else { 57 mergedNodes.put(otherNode, myNode); 41 58 mergeCommon(myNode, otherNode); 42 59 if (myNode.modified && !otherNode.modified) … … 64 81 ds.lineSegments.add(otherLs); 65 82 else { 83 mergedLineSegments.put(otherLs, myLs); 66 84 mergeCommon(myLs, otherLs); 67 85 if (myLs.modified && !otherLs.modified) … … 99 117 Iterator<LineSegment> it = otherTrack.segments.iterator(); 100 118 for (LineSegment ls : myTrack.segments) { 101 if (!match(ls, it.next())) {119 if (!match(ls, it.next())) 102 120 same = false; 103 }104 121 } 105 122 if (!same) { … … 113 130 public void visit(Key k) { 114 131 //TODO: Key doesn't really fit the OsmPrimitive concept! 132 } 133 134 /** 135 * Postprocess the dataset and fix all merged references to point to the actual 136 * data. 137 */ 138 public void fixReferences() { 139 for (LineSegment ls : ds.lineSegments) { 140 if (mergedNodes.containsKey(ls.start)) 141 ls.start = mergedNodes.get(ls.start); 142 if (mergedNodes.containsKey(ls.end)) 143 ls.end = mergedNodes.get(ls.end); 144 } 145 for (Track t : ds.tracks) { 146 boolean replacedSomething = false; 147 LinkedList<LineSegment> newSegments = new LinkedList<LineSegment>(); 148 for (LineSegment ls : t.segments) { 149 LineSegment otherLs = mergedLineSegments.get(ls); 150 newSegments.add(otherLs == null ? ls : otherLs); 151 if (otherLs != null) 152 replacedSomething = true; 153 } 154 if (replacedSomething) { 155 t.segments.clear(); 156 t.segments.addAll(newSegments); 157 } 158 for (LineSegment ls : t.segments) { 159 if (mergedNodes.containsKey(ls.start)) 160 ls.start = mergedNodes.get(ls.start); 161 if (mergedNodes.containsKey(ls.end)) 162 ls.end = mergedNodes.get(ls.end); 163 } 164 } 115 165 } 116 166 -
src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java
r52 r57 136 136 for (OsmPrimitive osm : ((OsmDataLayer)from).data.allPrimitives()) 137 137 osm.visit(visitor); 138 visitor.fixReferences(); 138 139 } 139 140 -
test/org/openstreetmap/josm/test/MergeVisitorTest.java
r52 r57 7 7 import org.openstreetmap.josm.data.osm.LineSegment; 8 8 import org.openstreetmap.josm.data.osm.Node; 9 import org.openstreetmap.josm.data.osm.OsmPrimitive; 9 10 import org.openstreetmap.josm.data.osm.visitor.MergeVisitor; 11 import org.openstreetmap.josm.test.framework.Bug; 10 12 import org.openstreetmap.josm.test.framework.DataSetTestCaseHelper; 11 13 … … 19 21 public void testMergeOldLineSegmentsWithNew() { 20 22 DataSet ds = new DataSet(); 21 Node n1 = DataSetTestCaseHelper.createNode(ds); 22 n1.id = 1; 23 Node n2 = DataSetTestCaseHelper.createNode(ds); 24 n2.id = 2; 25 LineSegment ls1 = DataSetTestCaseHelper.createLineSegment(ds, n1, n2); 23 Node[] n = createNodes(ds, 2); 24 LineSegment ls1 = DataSetTestCaseHelper.createLineSegment(ds, n[0], n[1]); 26 25 ls1.id = 3; 27 26 28 27 Node newnode = new Node(); 29 newnode.coor = new GeoPoint(n 2.coor.lat, n2.coor.lon);30 LineSegment newls = new LineSegment(n 1, newnode);28 newnode.coor = new GeoPoint(n[1].coor.lat, n[1].coor.lon); 29 LineSegment newls = new LineSegment(n[0], newnode); 31 30 32 31 MergeVisitor v = new MergeVisitor(ds); … … 34 33 assertEquals("line segment should have been merged.", 1, ds.lineSegments.size()); 35 34 } 35 36 /** 37 * Nodes beeing merged are equal but not the same. 38 */ 39 @Bug(54) 40 public void testEqualNotSame() { 41 // create a dataset with line segment a-b 42 DataSet ds = new DataSet(); 43 Node n[] = createNodes(ds, 2); 44 LineSegment ls1 = DataSetTestCaseHelper.createLineSegment(ds, n[0], n[1]); 45 ls1.id = 1; 46 47 // create an other dataset with line segment a'-c (a' is equal, but not same to a) 48 DataSet ds2 = new DataSet(); 49 Node n2[] = createNodes(ds2, 2); 50 n2[0].coor = new GeoPoint(n[0].coor.lat, n[0].coor.lon); 51 n2[1].id = 42; 52 LineSegment ls2 = DataSetTestCaseHelper.createLineSegment(ds, n2[0], n2[1]); 53 54 MergeVisitor v = new MergeVisitor(ds); 55 for (OsmPrimitive osm : ds2.allPrimitives()) 56 osm.visit(v); 57 v.fixReferences(); 58 59 assertSame(ls1.start, ls2.start); 60 } 61 62 63 /** 64 * Create that amount of nodes and add them to the dataset. The id will be 1,2,3,4... 65 * @param amount Number of nodes to create. 66 * @return The created nodes. 67 */ 68 private Node[] createNodes(DataSet ds, int amount) { 69 Node[] nodes = new Node[amount]; 70 for (int i = 0; i < amount; ++i) { 71 nodes[i] = DataSetTestCaseHelper.createNode(ds); 72 nodes[i].id = i+1; 73 } 74 return nodes; 75 } 36 76 }
Note:
See TracChangeset
for help on using the changeset viewer.