Changeset 12828 in josm
- Timestamp:
- 2017-09-12T16:50:56+02:00 (7 years ago)
- Location:
- trunk
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java
r12749 r12828 25 25 import org.openstreetmap.josm.Main; 26 26 import org.openstreetmap.josm.actions.ReverseWayAction.ReverseWayResult; 27 import org.openstreetmap.josm.actions.SplitWayAction.SplitWayResult;28 27 import org.openstreetmap.josm.command.AddCommand; 29 28 import org.openstreetmap.josm.command.ChangeCommand; … … 31 30 import org.openstreetmap.josm.command.DeleteCommand; 32 31 import org.openstreetmap.josm.command.SequenceCommand; 32 import org.openstreetmap.josm.command.SplitWayCommand; 33 33 import org.openstreetmap.josm.data.UndoRedoHandler; 34 34 import org.openstreetmap.josm.data.coor.EastNorth; … … 1019 1019 1020 1020 if (chunks.size() > 1) { 1021 SplitWay Result split = SplitWayAction.splitWay(way, chunks,1022 Collections.<OsmPrimitive>emptyList(), SplitWay Action.Strategy.keepFirstChunk());1021 SplitWayCommand split = SplitWayCommand.splitWay(way, chunks, 1022 Collections.<OsmPrimitive>emptyList(), SplitWayCommand.Strategy.keepFirstChunk()); 1023 1023 1024 1024 if (split != null) { 1025 1025 //execute the command, we need the results 1026 cmds.add(split .getCommand());1026 cmds.add(split); 1027 1027 commitCommands(marktr("Split ways into fragments")); 1028 1028 -
trunk/src/org/openstreetmap/josm/actions/SplitWayAction.java
r12726 r12828 11 11 import java.awt.event.KeyEvent; 12 12 import java.util.ArrayList; 13 import java.util.Arrays;14 13 import java.util.Collection; 15 14 import java.util.Collections; 16 import java.util.HashSet;17 15 import java.util.Iterator; 18 import java.util.LinkedList;19 16 import java.util.List; 20 import java.util.Optional;21 import java.util.Set;22 17 import java.util.concurrent.atomic.AtomicInteger; 23 18 … … 30 25 31 26 import org.openstreetmap.josm.Main; 32 import org.openstreetmap.josm.command.AddCommand;33 import org.openstreetmap.josm.command.ChangeCommand;34 27 import org.openstreetmap.josm.command.Command; 35 import org.openstreetmap.josm.command.S equenceCommand;28 import org.openstreetmap.josm.command.SplitWayCommand; 36 29 import org.openstreetmap.josm.data.osm.DefaultNameFormatter; 37 30 import org.openstreetmap.josm.data.osm.Node; … … 39 32 import org.openstreetmap.josm.data.osm.PrimitiveId; 40 33 import org.openstreetmap.josm.data.osm.Relation; 41 import org.openstreetmap.josm.data.osm.RelationMember;42 34 import org.openstreetmap.josm.data.osm.Way; 43 35 import org.openstreetmap.josm.data.osm.WaySegment; … … 47 39 import org.openstreetmap.josm.gui.Notification; 48 40 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 49 import org.openstreetmap.josm.tools.CheckParameterUtil;50 41 import org.openstreetmap.josm.tools.GBC; 51 42 import org.openstreetmap.josm.tools.Shortcut; … … 63 54 * @see SplitWayAction#splitWay 64 55 * @see SplitWayAction#split 65 */ 56 * @deprecated To be removed end of 2017. Use {@link SplitWayCommand} instead 57 */ 58 @Deprecated 66 59 public static class SplitWayResult { 67 60 private final Command command; … … 81 74 this.originalWay = originalWay; 82 75 this.newWays = newWays; 76 } 77 78 /** 79 * @param command The command to be performed to split the way (which is saved for later retrieval with {@link #getCommand}) 80 * @since 12828 81 */ 82 protected SplitWayResult(SplitWayCommand command) { 83 this.command = command; 84 this.newSelection = command.getNewSelection(); 85 this.originalWay = command.getOriginalWay(); 86 this.newWays = command.getNewWays(); 83 87 } 84 88 … … 194 198 // Finally, applicableWays contains only one perfect way 195 199 final Way selectedWay = applicableWays.get(0); 196 final List<List<Node>> wayChunks = buildSplitChunks(selectedWay, selectedNodes);200 final List<List<Node>> wayChunks = SplitWayCommand.buildSplitChunks(selectedWay, selectedNodes); 197 201 if (wayChunks != null) { 198 202 List<Relation> selectedRelations = OsmPrimitive.getFilteredList(selection, Relation.class); … … 202 206 203 207 final List<Way> newWays = createNewWaysFromChunks(selectedWay, wayChunks); 204 final Way wayToKeep = S trategy.keepLongestChunk().determineWayToKeep(newWays);208 final Way wayToKeep = SplitWayCommand.Strategy.keepLongestChunk().determineWayToKeep(newWays); 205 209 206 210 if (ExpertToggleAction.isExpert() && !selectedWay.isNew()) { … … 214 218 } 215 219 if (wayToKeep != null) { 216 final SplitWayResult result = doSplitWay(selectedWay, wayToKeep, newWays, sel); 217 MainApplication.undoRedo.add(result.getCommand()); 218 if (!result.getNewSelection().isEmpty()) { 219 getLayerManager().getEditDataSet().setSelected(result.getNewSelection()); 220 } 220 doSplitWay(selectedWay, wayToKeep, newWays, sel); 221 221 } 222 222 } … … 294 294 toggleSaveState(); // necessary since #showDialog() does not handle it due to the non-modal dialog 295 295 if (getValue() == 1) { 296 SplitWayResult result = doSplitWay(selectedWay, list.getSelectedValue(), newWays, selection); 297 MainApplication.undoRedo.add(result.getCommand()); 298 if (!result.getNewSelection().isEmpty()) { 299 MainApplication.getLayerManager().getEditDataSet().setSelected(result.getNewSelection()); 300 } 296 doSplitWay(selectedWay, list.getSelectedValue(), newWays, selection); 301 297 } 302 298 } … … 322 318 * @since 8954 323 319 * @since 10599 (functional interface) 324 */ 320 * @deprecated to be removed end of 2017. Use {@link org.openstreetmap.josm.command.SplitWayCommand.Strategy} instead 321 */ 322 @Deprecated 325 323 @FunctionalInterface 326 324 public interface Strategy { … … 339 337 */ 340 338 static Strategy keepLongestChunk() { 341 return wayChunks -> { 342 Way wayToKeep = null; 343 for (Way i : wayChunks) { 344 if (wayToKeep == null || i.getNodesCount() > wayToKeep.getNodesCount()) { 345 wayToKeep = i; 346 } 347 } 348 return wayToKeep; 349 }; 339 return SplitWayCommand.Strategy.keepLongestChunk()::determineWayToKeep; 350 340 } 351 341 … … 355 345 */ 356 346 static Strategy keepFirstChunk() { 357 return wayChunks -> wayChunks.iterator().next();347 return SplitWayCommand.Strategy.keepFirstChunk()::determineWayToKeep; 358 348 } 359 349 } … … 406 396 * @param splitPoints the nodes where the way is split. Must not be null. 407 397 * @return the list of chunks 408 */ 398 * @deprecated To be removed end of 2017. Use {@link SplitWayCommand#buildSplitChunks} instead 399 */ 400 @Deprecated 409 401 public static List<List<Node>> buildSplitChunks(Way wayToSplit, List<Node> splitPoints) { 410 CheckParameterUtil.ensureParameterNotNull(wayToSplit, "wayToSplit"); 411 CheckParameterUtil.ensureParameterNotNull(splitPoints, "splitPoints"); 412 413 Set<Node> nodeSet = new HashSet<>(splitPoints); 414 List<List<Node>> wayChunks = new LinkedList<>(); 415 List<Node> currentWayChunk = new ArrayList<>(); 416 wayChunks.add(currentWayChunk); 417 418 Iterator<Node> it = wayToSplit.getNodes().iterator(); 419 while (it.hasNext()) { 420 Node currentNode = it.next(); 421 boolean atEndOfWay = currentWayChunk.isEmpty() || !it.hasNext(); 422 currentWayChunk.add(currentNode); 423 if (nodeSet.contains(currentNode) && !atEndOfWay) { 424 currentWayChunk = new ArrayList<>(); 425 currentWayChunk.add(currentNode); 426 wayChunks.add(currentWayChunk); 427 } 428 } 429 430 // Handle circular ways specially. 431 // If you split at a circular way at two nodes, you just want to split 432 // it at these points, not also at the former endpoint. 433 // So if the last node is the same first node, join the last and the 434 // first way chunk. 435 List<Node> lastWayChunk = wayChunks.get(wayChunks.size() - 1); 436 if (wayChunks.size() >= 2 437 && wayChunks.get(0).get(0) == lastWayChunk.get(lastWayChunk.size() - 1) 438 && !nodeSet.contains(wayChunks.get(0).get(0))) { 439 if (wayChunks.size() == 2) { 440 new Notification( 441 tr("You must select two or more nodes to split a circular way.")) 442 .setIcon(JOptionPane.WARNING_MESSAGE) 443 .show(); 444 return null; 445 } 446 lastWayChunk.remove(lastWayChunk.size() - 1); 447 lastWayChunk.addAll(wayChunks.get(0)); 448 wayChunks.remove(wayChunks.size() - 1); 449 wayChunks.set(0, lastWayChunk); 450 } 451 452 if (wayChunks.size() < 2) { 453 if (wayChunks.get(0).get(0) == wayChunks.get(0).get(wayChunks.get(0).size() - 1)) { 454 new Notification( 455 tr("You must select two or more nodes to split a circular way.")) 456 .setIcon(JOptionPane.WARNING_MESSAGE) 457 .show(); 458 } else { 459 new Notification( 460 tr("The way cannot be split at the selected nodes. (Hint: Select nodes in the middle of the way.)")) 461 .setIcon(JOptionPane.WARNING_MESSAGE) 462 .show(); 463 } 464 return null; 465 } 466 return wayChunks; 402 return SplitWayCommand.buildSplitChunks(wayToSplit, splitPoints); 467 403 } 468 404 … … 472 408 * @param wayChunks the way chunks 473 409 * @return the new way objects 474 */ 410 * @deprecated To be removed end of 2017. Use {@link SplitWayCommand#createNewWaysFromChunks} instead 411 */ 412 @Deprecated 475 413 protected static List<Way> createNewWaysFromChunks(Way way, Iterable<List<Node>> wayChunks) { 476 final List<Way> newWays = new ArrayList<>(); 477 for (List<Node> wayChunk : wayChunks) { 478 Way wayToAdd = new Way(); 479 wayToAdd.setKeys(way.getKeys()); 480 wayToAdd.setNodes(wayChunk); 481 newWays.add(wayToAdd); 482 } 483 return newWays; 414 return SplitWayCommand.createNewWaysFromChunks(way, wayChunks); 484 415 } 485 416 … … 518 449 * @return the result from the split operation 519 450 * @since 12718 520 */ 451 * @deprecated to be removed end of 2017. Use {@link #splitWay(Way, List, Collection)} instead 452 */ 453 @Deprecated 521 454 public static SplitWayResult splitWay(Way way, List<List<Node>> wayChunks, 522 455 Collection<? extends OsmPrimitive> selection) { … … 565 498 * @return the result from the split operation 566 499 * @since 12718 567 */ 500 * @deprecated to be removed end of 2017. Use {@link SplitWayCommand#splitWay} instead 501 */ 502 @Deprecated 568 503 public static SplitWayResult splitWay(Way way, List<List<Node>> wayChunks, 569 504 Collection<? extends OsmPrimitive> selection, Strategy splitStrategy) { 570 // build a list of commands, and also a new selection list 571 final List<OsmPrimitive> newSelection = new ArrayList<>(selection.size() + wayChunks.size()); 572 newSelection.addAll(selection); 573 574 // Create all potential new ways 575 final List<Way> newWays = createNewWaysFromChunks(way, wayChunks); 576 577 // Determine which part reuses the existing way 578 final Way wayToKeep = splitStrategy.determineWayToKeep(newWays); 579 580 return wayToKeep != null ? doSplitWay(way, wayToKeep, newWays, newSelection) : null; 581 } 582 583 static SplitWayResult doSplitWay(Way way, Way wayToKeep, List<Way> newWays, List<OsmPrimitive> newSelection) { 584 585 Collection<Command> commandList = new ArrayList<>(newWays.size()); 586 Collection<String> nowarnroles = Main.pref.getCollection("way.split.roles.nowarn", 587 Arrays.asList("outer", "inner", "forward", "backward", "north", "south", "east", "west")); 588 505 SplitWayCommand cmd = SplitWayCommand.splitWay(way, wayChunks, selection, x -> splitStrategy.determineWayToKeep(x)); 506 return cmd != null ? new SplitWayResult(cmd) : null; 507 } 508 509 static void doSplitWay(Way way, Way wayToKeep, List<Way> newWays, List<OsmPrimitive> newSelection) { 589 510 final MapFrame map = MainApplication.getMap(); 590 511 final boolean isMapModeDraw = map != null && map.mapMode == map.mapModeDraw; 591 592 // Change the original way 593 final Way changedWay = new Way(way); 594 changedWay.setNodes(wayToKeep.getNodes()); 595 commandList.add(new ChangeCommand(way, changedWay)); 596 if (!isMapModeDraw && !newSelection.contains(way)) { 597 newSelection.add(way); 598 } 599 final int indexOfWayToKeep = newWays.indexOf(wayToKeep); 600 newWays.remove(wayToKeep); 601 602 if (!isMapModeDraw) { 603 newSelection.addAll(newWays); 604 } 605 for (Way wayToAdd : newWays) { 606 commandList.add(new AddCommand(way.getDataSet(), wayToAdd)); 607 } 608 609 boolean warnmerole = false; 610 boolean warnme = false; 611 // now copy all relations to new way also 612 613 for (Relation r : OsmPrimitive.getFilteredList(way.getReferrers(), Relation.class)) { 614 if (!r.isUsable()) { 615 continue; 616 } 617 Relation c = null; 618 String type = Optional.ofNullable(r.get("type")).orElse(""); 619 620 int ic = 0; 621 int ir = 0; 622 List<RelationMember> relationMembers = r.getMembers(); 623 for (RelationMember rm: relationMembers) { 624 if (rm.isWay() && rm.getMember() == way) { 625 boolean insert = true; 626 if ("restriction".equals(type) || "destination_sign".equals(type)) { 627 /* this code assumes the restriction is correct. No real error checking done */ 628 String role = rm.getRole(); 629 if ("from".equals(role) || "to".equals(role)) { 630 OsmPrimitive via = findVia(r, type); 631 List<Node> nodes = new ArrayList<>(); 632 if (via != null) { 633 if (via instanceof Node) { 634 nodes.add((Node) via); 635 } else if (via instanceof Way) { 636 nodes.add(((Way) via).lastNode()); 637 nodes.add(((Way) via).firstNode()); 638 } 639 } 640 Way res = null; 641 for (Node n : nodes) { 642 if (changedWay.isFirstLastNode(n)) { 643 res = way; 644 } 645 } 646 if (res == null) { 647 for (Way wayToAdd : newWays) { 648 for (Node n : nodes) { 649 if (wayToAdd.isFirstLastNode(n)) { 650 res = wayToAdd; 651 } 652 } 653 } 654 if (res != null) { 655 if (c == null) { 656 c = new Relation(r); 657 } 658 c.addMember(new RelationMember(role, res)); 659 c.removeMembersFor(way); 660 insert = false; 661 } 662 } else { 663 insert = false; 664 } 665 } else if (!"via".equals(role)) { 666 warnme = true; 667 } 668 } else if (!("route".equals(type)) && !("multipolygon".equals(type))) { 669 warnme = true; 670 } 671 if (c == null) { 672 c = new Relation(r); 673 } 674 675 if (insert) { 676 if (rm.hasRole() && !nowarnroles.contains(rm.getRole())) { 677 warnmerole = true; 678 } 679 680 Boolean backwards = null; 681 int k = 1; 682 while (ir - k >= 0 || ir + k < relationMembers.size()) { 683 if ((ir - k >= 0) && relationMembers.get(ir - k).isWay()) { 684 Way w = relationMembers.get(ir - k).getWay(); 685 if ((w.lastNode() == way.firstNode()) || w.firstNode() == way.firstNode()) { 686 backwards = Boolean.FALSE; 687 } else if ((w.firstNode() == way.lastNode()) || w.lastNode() == way.lastNode()) { 688 backwards = Boolean.TRUE; 689 } 690 break; 691 } 692 if ((ir + k < relationMembers.size()) && relationMembers.get(ir + k).isWay()) { 693 Way w = relationMembers.get(ir + k).getWay(); 694 if ((w.lastNode() == way.firstNode()) || w.firstNode() == way.firstNode()) { 695 backwards = Boolean.TRUE; 696 } else if ((w.firstNode() == way.lastNode()) || w.lastNode() == way.lastNode()) { 697 backwards = Boolean.FALSE; 698 } 699 break; 700 } 701 k++; 702 } 703 704 int j = ic; 705 final List<Way> waysToAddBefore = newWays.subList(0, indexOfWayToKeep); 706 for (Way wayToAdd : waysToAddBefore) { 707 RelationMember em = new RelationMember(rm.getRole(), wayToAdd); 708 j++; 709 if (Boolean.TRUE.equals(backwards)) { 710 c.addMember(ic + 1, em); 711 } else { 712 c.addMember(j - 1, em); 713 } 714 } 715 final List<Way> waysToAddAfter = newWays.subList(indexOfWayToKeep, newWays.size()); 716 for (Way wayToAdd : waysToAddAfter) { 717 RelationMember em = new RelationMember(rm.getRole(), wayToAdd); 718 j++; 719 if (Boolean.TRUE.equals(backwards)) { 720 c.addMember(ic, em); 721 } else { 722 c.addMember(j, em); 723 } 724 } 725 ic = j; 726 } 727 } 728 ic++; 729 ir++; 730 } 731 732 if (c != null) { 733 commandList.add(new ChangeCommand(r.getDataSet(), r, c)); 734 } 735 } 736 if (warnmerole) { 737 new Notification( 738 tr("A role based relation membership was copied to all new ways.<br>You should verify this and correct it when necessary.")) 739 .setIcon(JOptionPane.WARNING_MESSAGE) 740 .show(); 741 } else if (warnme) { 742 new Notification( 743 tr("A relation membership was copied to all new ways.<br>You should verify this and correct it when necessary.")) 744 .setIcon(JOptionPane.WARNING_MESSAGE) 745 .show(); 746 } 747 748 return new SplitWayResult( 749 new SequenceCommand( 750 /* for correct i18n of plural forms - see #9110 */ 751 trn("Split way {0} into {1} part", "Split way {0} into {1} parts", newWays.size() + 1, 752 way.getDisplayName(DefaultNameFormatter.getInstance()), newWays.size() + 1), 753 commandList 754 ), 755 newSelection, 756 way, 757 newWays 758 ); 759 } 760 761 static OsmPrimitive findVia(Relation r, String type) { 762 for (RelationMember rmv : r.getMembers()) { 763 if (("restriction".equals(type) && "via".equals(rmv.getRole())) 764 || ("destination_sign".equals(type) && rmv.hasRole("sign", "intersection"))) { 765 return rmv.getMember(); 766 } 767 } 768 return null; 512 final SplitWayCommand result = SplitWayCommand.doSplitWay(way, wayToKeep, newWays, !isMapModeDraw ? newSelection : null); 513 MainApplication.undoRedo.add(result); 514 List<? extends PrimitiveId> newSel = result.getNewSelection(); 515 if (newSel != null && !newSel.isEmpty()) { 516 MainApplication.getLayerManager().getEditDataSet().setSelected(newSel); 517 } 769 518 } 770 519 … … 806 555 * @return the result from the split operation 807 556 * @since 12718 808 */ 557 * @deprecated to be removed end of 2017. Use {@link #splitWay(Way, List, Collection)} instead 558 */ 559 @Deprecated 809 560 public static SplitWayResult split(Way way, List<Node> atNodes, Collection<? extends OsmPrimitive> selection) { 810 561 List<List<Node>> chunks = buildSplitChunks(way, atNodes); -
trunk/src/org/openstreetmap/josm/command/DeleteCommand.java
r12763 r12828 22 22 import javax.swing.Icon; 23 23 24 import org.openstreetmap.josm.actions.SplitWayAction;25 import org.openstreetmap.josm.actions.SplitWayAction.SplitWayResult;26 24 import org.openstreetmap.josm.data.osm.DataSet; 27 25 import org.openstreetmap.josm.data.osm.DefaultNameFormatter; … … 659 657 return new ChangeCommand(ws.way, wnew); 660 658 } else { 661 SplitWayResult split = SplitWayAction.splitWay(ws.way, Arrays.asList(n1, n2), Collections.<OsmPrimitive>emptyList()); 662 return split != null ? split.getCommand() : null; 659 return SplitWayCommand.splitWay(ws.way, Arrays.asList(n1, n2), Collections.<OsmPrimitive>emptyList()); 663 660 } 664 661 } -
trunk/src/org/openstreetmap/josm/gui/MainApplication.java
r12825 r12828 78 78 import org.openstreetmap.josm.actions.search.SearchAction; 79 79 import org.openstreetmap.josm.command.DeleteCommand; 80 import org.openstreetmap.josm.command.SplitWayCommand; 80 81 import org.openstreetmap.josm.data.Bounds; 81 82 import org.openstreetmap.josm.data.UndoRedoHandler; … … 1098 1099 MessageNotifier.setNotifierCallback(MainApplication::notifyNewMessages); 1099 1100 DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback); 1101 SplitWayCommand.setWarningNotifier(msg -> new Notification(msg).setIcon(JOptionPane.WARNING_MESSAGE).show()); 1100 1102 FileWatcher.registerLoader(SourceType.MAP_PAINT_STYLE, MapPaintStyleLoader::reloadStyle); 1101 1103 FileWatcher.registerLoader(SourceType.TAGCHECKER_RULE, MapCSSTagChecker::reloadRule); -
trunk/test/unit/org/openstreetmap/josm/actions/SplitWayActionTest.java
r12726 r12828 2 2 package org.openstreetmap.josm.actions; 3 3 4 import static org.junit.Assert.assertEquals;5 import static org.junit.Assert.assertNull;6 4 import static org.junit.Assert.assertSame; 7 5 import static org.junit.Assert.assertTrue; 8 6 9 import java.util.ArrayList;10 7 import java.util.Arrays; 11 import java.util.Iterator;12 8 13 9 import org.junit.Before; 14 10 import org.junit.Rule; 15 11 import org.junit.Test; 16 import org.openstreetmap.josm.actions.SplitWayAction.Strategy;17 12 import org.openstreetmap.josm.data.coor.EastNorth; 18 import org.openstreetmap.josm.data.coor.LatLon;19 13 import org.openstreetmap.josm.data.osm.DataSet; 20 14 import org.openstreetmap.josm.data.osm.Node; 21 import org.openstreetmap.josm.data.osm.OsmPrimitive;22 import org.openstreetmap.josm.data.osm.Relation;23 import org.openstreetmap.josm.data.osm.RelationMember;24 15 import org.openstreetmap.josm.data.osm.Way; 25 16 import org.openstreetmap.josm.gui.MainApplication; … … 112 103 } 113 104 } 114 115 /**116 * Unit test of {@link SplitWayAction#findVia}.117 */118 @Test119 public void testFindVia() {120 // empty relation121 assertNull(SplitWayAction.findVia(new Relation(), null));122 // restriction relation without via member123 Relation r = new Relation();124 r.addMember(new RelationMember("", new Node()));125 assertNull(SplitWayAction.findVia(r, "restriction"));126 // restriction relation with via member127 r = new Relation();128 OsmPrimitive via = new Node();129 r.addMember(new RelationMember("via", via));130 assertEquals(via, SplitWayAction.findVia(r, "restriction"));131 // destination_sign relation without sign nor intersection132 r = new Relation();133 r.addMember(new RelationMember("", new Node()));134 assertNull(SplitWayAction.findVia(r, "destination_sign"));135 // destination_sign with sign136 r = new Relation();137 via = new Node();138 r.addMember(new RelationMember("sign", via));139 assertEquals(via, SplitWayAction.findVia(r, "destination_sign"));140 // destination_sign with intersection141 r = new Relation();142 via = new Node();143 r.addMember(new RelationMember("intersection", via));144 assertEquals(via, SplitWayAction.findVia(r, "destination_sign"));145 }146 147 /**148 * Unit tests of route relations.149 */150 @Test151 public void testRouteRelation() {152 doTestRouteRelation(false, 0);153 doTestRouteRelation(false, 1);154 doTestRouteRelation(false, 2);155 doTestRouteRelation(false, 3);156 doTestRouteRelation(true, 0);157 doTestRouteRelation(true, 1);158 doTestRouteRelation(true, 2);159 doTestRouteRelation(true, 3);160 }161 162 void doTestRouteRelation(final boolean wayIsReversed, final int indexOfWayToKeep) {163 final DataSet dataSet = new DataSet();164 final Node n1 = new Node(new LatLon(1, 0));165 final Node n2 = new Node(new LatLon(2, 0));166 final Node n3 = new Node(new LatLon(3, 0));167 final Node n4 = new Node(new LatLon(4, 0));168 final Node n5 = new Node(new LatLon(5, 0));169 final Node n6 = new Node(new LatLon(6, 0));170 final Node n7 = new Node(new LatLon(7, 0));171 final Way w1 = new Way();172 final Way w2 = new Way();173 final Way w3 = new Way();174 final Relation route = new Relation();175 for (OsmPrimitive p : Arrays.asList(n1, n2, n3, n4, n5, n6, n7, w1, w2, w3, route)) {176 dataSet.addPrimitive(p);177 }178 w1.setNodes(Arrays.asList(n1, n2));179 w2.setNodes(wayIsReversed180 ? Arrays.asList(n6, n5, n4, n3, n2)181 : Arrays.asList(n2, n3, n4, n5, n6)182 );183 w3.setNodes(Arrays.asList(n6, n7));184 route.put("type", "route");185 route.addMember(new RelationMember("", w1));186 route.addMember(new RelationMember("", w2));187 route.addMember(new RelationMember("", w3));188 dataSet.setSelected(Arrays.asList(w2, n3, n4, n5));189 190 final Strategy strategy = wayChunks -> {191 final Iterator<Way> it = wayChunks.iterator();192 for (int i = 0; i < indexOfWayToKeep; i++) {193 it.next();194 }195 return it.next();196 };197 final SplitWayAction.SplitWayResult result = SplitWayAction.splitWay(198 w2, SplitWayAction.buildSplitChunks(w2, Arrays.asList(n3, n4, n5)), new ArrayList<OsmPrimitive>(), strategy);199 MainApplication.undoRedo.add(result.getCommand());200 201 assertEquals(6, route.getMembersCount());202 assertEquals(w1, route.getMemberPrimitivesList().get(0));203 assertEquals(w3, route.getMemberPrimitivesList().get(5));204 assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(0)), n1);205 assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(0)), n2);206 assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(1)), n2);207 assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(1)), n3);208 assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(2)), n3);209 assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(2)), n4);210 assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(3)), n4);211 assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(3)), n5);212 assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(4)), n5);213 assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(4)), n6);214 assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(5)), n6);215 assertFirstLastNodeIs(((Way) route.getMemberPrimitivesList().get(5)), n7);216 }217 218 static void assertFirstLastNodeIs(Way way, Node node) {219 assertTrue("First/last node of " + way + " should be " + node, node.equals(way.firstNode()) || node.equals(way.lastNode()));220 }221 105 }
Note:
See TracChangeset
for help on using the changeset viewer.