Changeset 33415 in osm for applications/editors/josm/plugins
- Timestamp:
- 2017-06-28T14:42:16+02:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/pt_assistant
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/actions/SplitRoundaboutAction.java
r33414 r33415 24 24 import org.openstreetmap.josm.actions.SplitWayAction.SplitWayResult; 25 25 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask; 26 import org.openstreetmap.josm.command.ChangeCommand; 27 import org.openstreetmap.josm.command.Command; 28 import org.openstreetmap.josm.command.SequenceCommand; 26 29 import org.openstreetmap.josm.data.Bounds; 27 30 import org.openstreetmap.josm.data.osm.BBox; … … 47 50 public class SplitRoundaboutAction extends JosmAction { 48 51 49 private static final String actionName= "Split Roundabout";52 private static final String ACTION_NAME = "Split Roundabout"; 50 53 private static final long serialVersionUID = 8912249304286025356L; 51 54 … … 54 57 */ 55 58 public SplitRoundaboutAction() { 56 super( actionName, "icons/splitroundabout",actionName, null, true);59 super(ACTION_NAME, "icons/splitroundabout", ACTION_NAME, null, true); 57 60 } 58 61 … … 99 102 Map<Relation, List<Integer>> savedPositions = getSavedPositions(roundabout); 100 103 104 //remove the roundabout from each relation 105 Main.main.undoRedo.add(getRemoveRoundaboutFromRelationsCommand(roundabout)); 106 101 107 //split the roundabout on the designed nodes 102 108 List<Node> splitNodes = getSplitNodes(roundabout); 103 109 SplitWayResult result = SplitWayAction.split(getLayerManager().getEditLayer(), 104 110 roundabout, splitNodes, Collections.emptyList()); 105 result.getCommand() .executeCommand();111 Main.main.undoRedo.add(result.getCommand()); 106 112 Collection<Way> splitWays = result.getNewWays(); 107 113 splitWays.add(result.getOriginalWay()); 108 114 109 115 //update the relations. 110 updateRelations(savedPositions, splitNodes, splitWays); 111 } 112 113 public void updateRelations(Map<Relation, List<Integer>> savedPositions, 116 Main.main.undoRedo.add(getUpdateRelationsCommand(savedPositions, splitNodes, splitWays)); 117 } 118 119 public Command getUpdateRelationsCommand(Map<Relation, 120 List<Integer>> savedPositions, 114 121 List<Node> splitNodes, Collection<Way> splitWays) { 122 123 Map<Relation, Relation> changingRelations = 124 updateRelations(savedPositions, splitNodes, splitWays); 125 126 List<Command> commands = new ArrayList<>(); 127 changingRelations.forEach((oldR, newR) -> 128 commands.add(new ChangeCommand(oldR, newR))); 129 130 return new SequenceCommand("Updating Relations for SplitRoundabout", commands); 131 } 132 133 private Map<Relation, Relation> updateRelations(Map<Relation, 134 List<Integer>> savedPositions, 135 List<Node> splitNodes, Collection<Way> splitWays) { 136 Map<Relation, Relation> changingRelation = new HashMap<>(); 115 137 Map<Relation, Integer> memberOffset = new HashMap<>(); 116 138 savedPositions.forEach((r, positions) -> 117 139 positions.forEach(i -> { 118 140 141 if(!changingRelation.containsKey(r)) 142 changingRelation.put(r, new Relation(r)); 143 144 Relation c = changingRelation.get(r); 145 119 146 if(!memberOffset.containsKey(r)) 120 147 memberOffset.put(r, 0); 121 148 int offset = memberOffset.get(r); 122 149 123 Pair<Way, Way> entryExitWays= getEntryExitWays( r, i + offset);150 Pair<Way, Way> entryExitWays= getEntryExitWays(c, i + offset); 124 151 Way entryWay = entryExitWays.a; 125 152 Way exitWay = entryExitWays.b; … … 141 168 142 169 while(!curr.lastNode().equals(exitNode)) { 143 r.addMember(i + offset++, new RelationMember(null, curr));170 c.addMember(i + offset++, new RelationMember(null, curr)); 144 171 parents = curr.lastNode().getParentWays(); 145 172 parents.remove(curr); … … 147 174 curr = parents.get(0); 148 175 } 149 r.addMember(i + offset++, new RelationMember(null, curr));176 c.addMember(i + offset++, new RelationMember(null, curr)); 150 177 memberOffset.put(r, offset); 151 178 })); 179 return changingRelation; 152 180 } 153 181 … … 184 212 parents.remove(roundabout); 185 213 for(Way parent: parents) { 186 for(OsmPrimitive prim : parent.getReferrers()) {187 if(prim.getType() == OsmPrimitiveType.RELATION &&188 (Relation) prim))214 for(Relation r : OsmPrimitive.getFilteredList( 215 parent.getReferrers(), Relation.class)) { 216 if(RouteUtils.isPTRoute(r)) 189 217 return false; 190 218 } … … 196 224 } 197 225 226 public Command getRemoveRoundaboutFromRelationsCommand(Way roundabout) { 227 List <Relation> referrers = OsmPrimitive.getFilteredList( 228 roundabout.getReferrers(), Relation.class); 229 referrers.removeIf(r -> !RouteUtils.isPTRoute(r)); 230 231 List<Command> commands = new ArrayList<>(); 232 referrers.forEach(r -> { 233 Relation c = new Relation(r); 234 c.removeMembersFor(roundabout); 235 commands.add(new ChangeCommand(r, c)); 236 }); 237 238 return new SequenceCommand("Remove roundabout from relations", commands); 239 } 240 198 241 //save the position of the roundabout inside each public transport route 199 242 //it is contained in … … 201 244 202 245 Map<Relation, List<Integer>> savedPositions = new HashMap<>(); 203 List <OsmPrimitive> referrers = roundabout.getReferrers(); 204 referrers.removeIf(r -> r.getType() != OsmPrimitiveType.RELATION 205 || !RouteUtils.isPTRoute((Relation) r)); 206 207 for(OsmPrimitive currPrim : referrers) { 208 Relation curr = (Relation) currPrim; 246 List <Relation> referrers = OsmPrimitive.getFilteredList( 247 roundabout.getReferrers(), Relation.class); 248 referrers.removeIf(r -> !RouteUtils.isPTRoute(r)); 249 250 for(Relation curr : referrers) { 209 251 for(int j = 0; j < curr.getMembersCount(); j++) { 210 252 if(curr.getMember(j).getUniqueId() == roundabout.getUniqueId()) { … … 215 257 } 216 258 } 217 218 if(savedPositions.containsKey(curr))219 curr.removeMembersFor(roundabout);220 259 } 221 260 -
applications/editors/josm/plugins/pt_assistant/test/unit/org/openstreetmap/josm/plugins/pt_assistant/actions/SplitRoundaboutTest.java
r33413 r33415 59 59 private Collection<Way> splitWay(Way w) { 60 60 Map<Relation, List<Integer>> savedPositions = action.getSavedPositions(w); 61 action.getRemoveRoundaboutFromRelationsCommand(w).executeCommand(); 61 62 List<Node> splitNodes = action.getSplitNodes(w); 62 63 SplitWayResult result = SplitWayAction.split(layer, w, splitNodes, Collections.emptyList()); … … 64 65 Collection<Way> splitWays = result.getNewWays(); 65 66 splitWays.add(result.getOriginalWay()); 66 action. updateRelations(savedPositions, splitNodes, splitWays);67 action.getUpdateRelationsCommand(savedPositions, splitNodes, splitWays).executeCommand(); 67 68 return splitWays; 68 69 } … … 74 75 sw1.forEach(w -> { 75 76 if (w.firstNode().getUniqueId() == 267843779L && w.lastNode().getUniqueId() == 2968718407L) 76 assertEquals(w.getReferrers().size() , 5);77 assertEquals(5, w.getReferrers().size()); 77 78 else if (w.firstNode().getUniqueId() == 2968718407L && w.lastNode().getUniqueId() == 2383688231L) 78 assertEquals(w.getReferrers().size() , 0);79 assertEquals(0, w.getReferrers().size()); 79 80 else if (w.firstNode().getUniqueId() == 2383688231L && w.lastNode().getUniqueId() == 267843741L) 80 assertEquals(w.getReferrers().size() , 5);81 assertEquals(5, w.getReferrers().size()); 81 82 else if (w.firstNode().getUniqueId() == 267843741L && w.lastNode().getUniqueId() == 267843779L) 82 assertEquals(w.getReferrers().size() , 0);83 assertEquals(0, w.getReferrers().size()); 83 84 else 84 85 fail(); … … 92 93 sw2.forEach(w -> { 93 94 if(w.firstNode().getUniqueId() == 2158181809L && w.lastNode().getUniqueId() == 2158181798L) 94 assertEquals(w.getReferrers().size() , 8);95 assertEquals(8, w.getReferrers().size()); 95 96 else if (w.firstNode().getUniqueId() == 2158181798L && w.lastNode().getUniqueId() == 2158181789L) 96 assertEquals(w.getReferrers().size() , 0);97 assertEquals(0, w.getReferrers().size()); 97 98 else if (w.firstNode().getUniqueId() == 2158181789L && w.lastNode().getUniqueId() == 2158181803L) 98 assertEquals(w.getReferrers().size() , 8);99 assertEquals(8, w.getReferrers().size()); 99 100 else if (w.firstNode().getUniqueId() == 2158181803L && w.lastNode().getUniqueId() == 2158181809L) 100 assertEquals(w.getReferrers().size() , 0);101 assertEquals(0, w.getReferrers().size()); 101 102 else 102 103 fail(); … … 110 111 sw3.forEach(w -> { 111 112 if(w.firstNode().getUniqueId() == 280697532L && w.lastNode().getUniqueId() == 280697452L) 112 assertEquals(w.getReferrers().size() , 0);113 assertEquals(0, w.getReferrers().size()); 113 114 else if (w.firstNode().getUniqueId() == 280697452L && w.lastNode().getUniqueId() == 280697591L) 114 assertEquals(w.getReferrers().size() , 2);115 assertEquals(2, w.getReferrers().size()); 115 116 else if (w.firstNode().getUniqueId() == 280697591L && w.lastNode().getUniqueId() == 280697534L) 116 assertEquals(w.getReferrers().size() , 0);117 assertEquals(0, w.getReferrers().size()); 117 118 else if (w.firstNode().getUniqueId() == 280697534L && w.lastNode().getUniqueId() == 280697532L) 118 assertEquals(w.getReferrers().size() , 1);119 assertEquals(1, w.getReferrers().size()); 119 120 else 120 121 fail();
Note:
See TracChangeset
for help on using the changeset viewer.