Ticket #18295: split_multipolygon_utilsplugin2_fix_2021-08-02.patch
| File split_multipolygon_utilsplugin2_fix_2021-08-02.patch, 6.1 KB (added by , 5 years ago) |
|---|
-
plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/SplitObjectAction.java
276 276 * @param allowInvalidSplit allow multipolygon splits that result in invalid multipolygons. 277 277 * @return the new multipolygon relations after splitting + the executed commands 278 278 * (already executed and added to the {@link UndoRedoHandler}). 279 * Re lation and command lists are emptyif split did not succeed.279 * Returns null if split did not succeed. 280 280 */ 281 public static Pair<List<Relation>, List<Command>> splitMultipolygonAtWayChecked(281 public static Pair<List<Relation>, SplitMultipolygonCommand> splitMultipolygonAtWayChecked( 282 282 Relation mpRelation, Way splitWay, boolean allowInvalidSplit) { 283 283 284 284 CheckParameterUtil.ensureParameterNotNull(mpRelation, "mpRelation"); … … 286 286 CheckParameterUtil.ensureThat(mpRelation.isMultipolygon(), "mpRelation.isMultipolygon"); 287 287 288 288 try { 289 Pair<List<Relation>, List<Command>> splitResult = splitMultipolygonAtWay(mpRelation, splitWay, allowInvalidSplit);289 Pair<List<Relation>, SplitMultipolygonCommand> splitResult = splitMultipolygonAtWay(mpRelation, splitWay, allowInvalidSplit); 290 290 List<Relation> mpRelations = splitResult.a; 291 List<Command> commands= splitResult.b;291 SplitMultipolygonCommand splitCmd = splitResult.b; 292 292 293 293 List<TestError> mpErrorsPostSplit = new ArrayList<>(); 294 294 for (Relation mp : mpRelations) { … … 310 310 for (TestError testError : mpErrorsPostSplit) { 311 311 showWarningNotification(testError.getMessage()); 312 312 } 313 for (int i = commands.size()-1; i >= 0; --i) {314 commands.get(i).undoCommand();315 }316 313 317 return new Pair<>(new ArrayList<>(), new ArrayList<>()); 314 splitCmd.undoCommand(); 315 316 return null; 318 317 } else { 319 318 showWarningNotification(tr("Multipolygon split created invalid multipolygons! Please review and fix these errors.")); 320 319 for (TestError testError : mpErrorsPostSplit) { … … 323 322 } 324 323 } 325 324 326 for (Command mpSplitCommand : commands) { 327 UndoRedoHandler.getInstance().add(mpSplitCommand, false); 328 } 325 UndoRedoHandler.getInstance().add(splitCmd, false); 326 mpRelation.getDataSet().setSelected(mpRelations); 329 327 330 mpRelation.getDataSet().setSelected(mpRelations);331 328 return splitResult; 332 329 333 330 } catch (IllegalArgumentException e) { 334 331 // Changes were already undone in splitMultipolygonAtWay 335 332 showWarningNotification(e.getMessage()); 336 return n ew Pair<>(new ArrayList<>(), new ArrayList<>());333 return null; 337 334 } 338 335 } 339 336 … … 349 346 * @throws IllegalArgumentException if the multipolygon has errors and/or the splitWay is unsuitable for 350 347 * splitting the multipolygon (e.g. because it crosses inners and {@code allowInvalidSplit == false}). 351 348 */ 352 public static Pair<List<Relation>, List<Command>> splitMultipolygonAtWay(Relation mpRelation,349 public static Pair<List<Relation>, SplitMultipolygonCommand> splitMultipolygonAtWay(Relation mpRelation, 353 350 Way splitWay, 354 351 boolean allowInvalidSplit) throws IllegalArgumentException { 355 352 CheckParameterUtil.ensureParameterNotNull(mpRelation, "mpRelation"); … … 498 495 mpCreationCommands.add(new ChangeMembersCommand(mpRelation, mpMembers)); 499 496 mpCreationCommands.add(new AddCommand(mpRelation.getDataSet(), newMpRelation)); 500 497 501 SequenceCommand sequenceCommand = new SequenceCommand(mpRelation.getDataSet(), "Split Multipolygon", mpCreationCommands, false); 498 SequenceCommand sequenceCommand = new SequenceCommand(mpRelation.getDataSet(), 499 "Create new multipolygon and assign members", mpCreationCommands, false); 500 502 501 sequenceCommand.executeCommand(); 503 502 commands.add(sequenceCommand); 504 503 … … 506 505 } 507 506 } 508 507 509 return new Pair<>(mpRelations, commands);508 return new Pair<>(mpRelations, new SplitMultipolygonCommand(commands)); 510 509 } 511 510 512 511 /** … … 568 567 } else 569 568 return false; 570 569 } 571 return (node == 2 || ways == 1 || ways == 2) || //only 2 nodes selected. one split-way selected. split-way + way to split. 572 (multipolygons == 1 && ways == 1); 570 571 // When splitting closed ways: only 2 nodes selected. one split-way selected. split-way + way to split. 572 // When splitting multipolygons: 1 multipolygon + 1 split way 573 return ((multipolygons == 0) && (node == 2 || ways == 1 || ways == 2)) || 574 ((multipolygons == 1) && (ways == 1)); 573 575 } 574 576 575 577 @Override … … 590 592 new Notification(msg) 591 593 .setIcon(JOptionPane.WARNING_MESSAGE).show(); 592 594 } 595 596 private static class SplitMultipolygonCommand extends SequenceCommand { 597 SplitMultipolygonCommand(Collection<Command> sequence) { 598 super(tr("Split multipolygon"), sequence, true); 599 setSequenceComplete(true); // commands were already executed 600 } 601 602 @Override 603 public void undoCommand() { 604 getAffectedDataSet().update(super::undoCommand); 605 } 606 607 @Override 608 public boolean executeCommand() { 609 return getAffectedDataSet().update(super::executeCommand); 610 } 611 } 593 612 }
