source: josm/trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java@ 12425

Last change on this file since 12425 was 11822, checked in by michael2402, 7 years ago

See #14528: Revert join areas algorithm to old version for 17.03 release.

  • Property svn:eol-style set to native
File size: 57.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.HashMap;
14import java.util.HashSet;
15import java.util.LinkedHashSet;
16import java.util.LinkedList;
17import java.util.List;
18import java.util.Map;
19import java.util.Objects;
20import java.util.Set;
21import java.util.TreeMap;
22
23import javax.swing.JOptionPane;
24
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.actions.ReverseWayAction.ReverseWayResult;
27import org.openstreetmap.josm.actions.SplitWayAction.SplitWayResult;
28import org.openstreetmap.josm.command.AddCommand;
29import org.openstreetmap.josm.command.ChangeCommand;
30import org.openstreetmap.josm.command.Command;
31import org.openstreetmap.josm.command.DeleteCommand;
32import org.openstreetmap.josm.command.SequenceCommand;
33import org.openstreetmap.josm.data.UndoRedoHandler;
34import org.openstreetmap.josm.data.coor.EastNorth;
35import org.openstreetmap.josm.data.osm.DataSet;
36import org.openstreetmap.josm.data.osm.Node;
37import org.openstreetmap.josm.data.osm.NodePositionComparator;
38import org.openstreetmap.josm.data.osm.OsmPrimitive;
39import org.openstreetmap.josm.data.osm.Relation;
40import org.openstreetmap.josm.data.osm.RelationMember;
41import org.openstreetmap.josm.data.osm.TagCollection;
42import org.openstreetmap.josm.data.osm.Way;
43import org.openstreetmap.josm.gui.Notification;
44import org.openstreetmap.josm.gui.conflict.tags.CombinePrimitiveResolverDialog;
45import org.openstreetmap.josm.gui.layer.OsmDataLayer;
46import org.openstreetmap.josm.tools.Geometry;
47import org.openstreetmap.josm.tools.JosmRuntimeException;
48import org.openstreetmap.josm.tools.Pair;
49import org.openstreetmap.josm.tools.Shortcut;
50import org.openstreetmap.josm.tools.UserCancelException;
51import org.openstreetmap.josm.tools.Utils;
52
53/**
54 * Join Areas (i.e. closed ways and multipolygons).
55 * @since 2575
56 */
57public class JoinAreasAction extends JosmAction {
58 // This will be used to commit commands and unite them into one large command sequence at the end
59 private final transient LinkedList<Command> cmds = new LinkedList<>();
60 private int cmdsCount;
61 private final transient List<Relation> addedRelations = new LinkedList<>();
62
63 /**
64 * This helper class describes join areas action result.
65 * @author viesturs
66 */
67 public static class JoinAreasResult {
68
69 private final boolean hasChanges;
70 private final List<Multipolygon> polygons;
71
72 /**
73 * Constructs a new {@code JoinAreasResult}.
74 * @param hasChanges whether the result has changes
75 * @param polygons the result polygons, can be null
76 */
77 public JoinAreasResult(boolean hasChanges, List<Multipolygon> polygons) {
78 this.hasChanges = hasChanges;
79 this.polygons = polygons;
80 }
81
82 /**
83 * Determines if the result has changes.
84 * @return {@code true} if the result has changes
85 */
86 public final boolean hasChanges() {
87 return hasChanges;
88 }
89
90 /**
91 * Returns the result polygons, can be null.
92 * @return the result polygons, can be null
93 */
94 public final List<Multipolygon> getPolygons() {
95 return polygons;
96 }
97 }
98
99 public static class Multipolygon {
100 private final Way outerWay;
101 private final List<Way> innerWays;
102
103 /**
104 * Constructs a new {@code Multipolygon}.
105 * @param way outer way
106 */
107 public Multipolygon(Way way) {
108 outerWay = way;
109 innerWays = new ArrayList<>();
110 }
111
112 /**
113 * Returns the outer way.
114 * @return the outer way
115 */
116 public final Way getOuterWay() {
117 return outerWay;
118 }
119
120 /**
121 * Returns the inner ways.
122 * @return the inner ways
123 */
124 public final List<Way> getInnerWays() {
125 return innerWays;
126 }
127 }
128
129 // HelperClass
130 // Saves a relation and a role an OsmPrimitve was part of until it was stripped from all relations
131 private static class RelationRole {
132 public final Relation rel;
133 public final String role;
134
135 RelationRole(Relation rel, String role) {
136 this.rel = rel;
137 this.role = role;
138 }
139
140 @Override
141 public int hashCode() {
142 return Objects.hash(rel, role);
143 }
144
145 @Override
146 public boolean equals(Object other) {
147 if (this == other) return true;
148 if (other == null || getClass() != other.getClass()) return false;
149 RelationRole that = (RelationRole) other;
150 return Objects.equals(rel, that.rel) &&
151 Objects.equals(role, that.role);
152 }
153 }
154
155 /**
156 * HelperClass - saves a way and the "inside" side.
157 *
158 * insideToTheLeft: if true left side is "in", false -right side is "in".
159 * Left and right are determined along the orientation of way.
160 */
161 public static class WayInPolygon {
162 public final Way way;
163 public boolean insideToTheRight;
164
165 public WayInPolygon(Way way, boolean insideRight) {
166 this.way = way;
167 this.insideToTheRight = insideRight;
168 }
169
170 @Override
171 public int hashCode() {
172 return Objects.hash(way, insideToTheRight);
173 }
174
175 @Override
176 public boolean equals(Object other) {
177 if (this == other) return true;
178 if (other == null || getClass() != other.getClass()) return false;
179 WayInPolygon that = (WayInPolygon) other;
180 return insideToTheRight == that.insideToTheRight &&
181 Objects.equals(way, that.way);
182 }
183 }
184
185 /**
186 * This helper class describes a polygon, assembled from several ways.
187 * @author viesturs
188 *
189 */
190 public static class AssembledPolygon {
191 public List<WayInPolygon> ways;
192
193 public AssembledPolygon(List<WayInPolygon> boundary) {
194 this.ways = boundary;
195 }
196
197 public List<Node> getNodes() {
198 List<Node> nodes = new ArrayList<>();
199 for (WayInPolygon way : this.ways) {
200 //do not add the last node as it will be repeated in the next way
201 if (way.insideToTheRight) {
202 for (int pos = 0; pos < way.way.getNodesCount() - 1; pos++) {
203 nodes.add(way.way.getNode(pos));
204 }
205 } else {
206 for (int pos = way.way.getNodesCount() - 1; pos > 0; pos--) {
207 nodes.add(way.way.getNode(pos));
208 }
209 }
210 }
211
212 return nodes;
213 }
214
215 /**
216 * Inverse inside and outside
217 */
218 public void reverse() {
219 for (WayInPolygon way: ways) {
220 way.insideToTheRight = !way.insideToTheRight;
221 }
222 Collections.reverse(ways);
223 }
224 }
225
226 public static class AssembledMultipolygon {
227 public AssembledPolygon outerWay;
228 public List<AssembledPolygon> innerWays;
229
230 public AssembledMultipolygon(AssembledPolygon way) {
231 outerWay = way;
232 innerWays = new ArrayList<>();
233 }
234 }
235
236 /**
237 * This hepler class implements algorithm traversing trough connected ways.
238 * Assumes you are going in clockwise orientation.
239 * @author viesturs
240 */
241 private static class WayTraverser {
242
243 /** Set of {@link WayInPolygon} to be joined by walk algorithm */
244 private final Set<WayInPolygon> availableWays;
245 /** Current state of walk algorithm */
246 private WayInPolygon lastWay;
247 /** Direction of current way */
248 private boolean lastWayReverse;
249
250 /** Constructor
251 * @param ways available ways
252 */
253 WayTraverser(Collection<WayInPolygon> ways) {
254 availableWays = new HashSet<>(ways);
255 lastWay = null;
256 }
257
258 /**
259 * Remove ways from available ways
260 * @param ways Collection of WayInPolygon
261 */
262 public void removeWays(Collection<WayInPolygon> ways) {
263 availableWays.removeAll(ways);
264 }
265
266 /**
267 * Remove a single way from available ways
268 * @param way WayInPolygon
269 */
270 public void removeWay(WayInPolygon way) {
271 availableWays.remove(way);
272 }
273
274 /**
275 * Reset walk algorithm to a new start point
276 * @param way New start point
277 */
278 public void setStartWay(WayInPolygon way) {
279 lastWay = way;
280 lastWayReverse = !way.insideToTheRight;
281 }
282
283 /**
284 * Reset walk algorithm to a new start point.
285 * @return The new start point or null if no available way remains
286 */
287 public WayInPolygon startNewWay() {
288 if (availableWays.isEmpty()) {
289 lastWay = null;
290 } else {
291 lastWay = availableWays.iterator().next();
292 lastWayReverse = !lastWay.insideToTheRight;
293 }
294
295 return lastWay;
296 }
297
298 /**
299 * Walking through {@link WayInPolygon} segments, head node is the current position
300 * @return Head node
301 */
302 private Node getHeadNode() {
303 return !lastWayReverse ? lastWay.way.lastNode() : lastWay.way.firstNode();
304 }
305
306 /**
307 * Node just before head node.
308 * @return Previous node
309 */
310 private Node getPrevNode() {
311 return !lastWayReverse ? lastWay.way.getNode(lastWay.way.getNodesCount() - 2) : lastWay.way.getNode(1);
312 }
313
314 /**
315 * Returns oriented angle (N1N2, N1N3) in range [0; 2*Math.PI[
316 * @param n1 first node
317 * @param n2 second node
318 * @param n3 third node
319 * @return oriented angle (N1N2, N1N3) in range [0; 2*Math.PI[
320 */
321 private static double getAngle(Node n1, Node n2, Node n3) {
322 EastNorth en1 = n1.getEastNorth();
323 EastNorth en2 = n2.getEastNorth();
324 EastNorth en3 = n3.getEastNorth();
325 double angle = Math.atan2(en3.getY() - en1.getY(), en3.getX() - en1.getX()) -
326 Math.atan2(en2.getY() - en1.getY(), en2.getX() - en1.getX());
327 while (angle >= 2*Math.PI) {
328 angle -= 2*Math.PI;
329 }
330 while (angle < 0) {
331 angle += 2*Math.PI;
332 }
333 return angle;
334 }
335
336 /**
337 * Get the next way creating a clockwise path, ensure it is the most right way. #7959
338 * @return The next way.
339 */
340 public WayInPolygon walk() {
341 Node headNode = getHeadNode();
342 Node prevNode = getPrevNode();
343
344 double headAngle = Math.atan2(headNode.getEastNorth().east() - prevNode.getEastNorth().east(),
345 headNode.getEastNorth().north() - prevNode.getEastNorth().north());
346 double bestAngle = 0;
347
348 //find best next way
349 WayInPolygon bestWay = null;
350 boolean bestWayReverse = false;
351
352 for (WayInPolygon way : availableWays) {
353 Node nextNode;
354
355 // Check for a connected way
356 if (way.way.firstNode().equals(headNode) && way.insideToTheRight) {
357 nextNode = way.way.getNode(1);
358 } else if (way.way.lastNode().equals(headNode) && !way.insideToTheRight) {
359 nextNode = way.way.getNode(way.way.getNodesCount() - 2);
360 } else {
361 continue;
362 }
363
364 if (nextNode == prevNode) {
365 // go back
366 lastWay = way;
367 lastWayReverse = !way.insideToTheRight;
368 return lastWay;
369 }
370
371 double angle = Math.atan2(nextNode.getEastNorth().east() - headNode.getEastNorth().east(),
372 nextNode.getEastNorth().north() - headNode.getEastNorth().north()) - headAngle;
373 if (angle > Math.PI)
374 angle -= 2*Math.PI;
375 if (angle <= -Math.PI)
376 angle += 2*Math.PI;
377
378 // Now we have a valid candidate way, is it better than the previous one ?
379 if (bestWay == null || angle > bestAngle) {
380 //the new way is better
381 bestWay = way;
382 bestWayReverse = !way.insideToTheRight;
383 bestAngle = angle;
384 }
385 }
386
387 lastWay = bestWay;
388 lastWayReverse = bestWayReverse;
389 return lastWay;
390 }
391
392 /**
393 * Search for an other way coming to the same head node at left side from last way. #9951
394 * @return left way or null if none found
395 */
396 public WayInPolygon leftComingWay() {
397 Node headNode = getHeadNode();
398 Node prevNode = getPrevNode();
399
400 WayInPolygon mostLeft = null; // most left way connected to head node
401 boolean comingToHead = false; // true if candidate come to head node
402 double angle = 2*Math.PI;
403
404 for (WayInPolygon candidateWay : availableWays) {
405 boolean candidateComingToHead;
406 Node candidatePrevNode;
407
408 if (candidateWay.way.firstNode().equals(headNode)) {
409 candidateComingToHead = !candidateWay.insideToTheRight;
410 candidatePrevNode = candidateWay.way.getNode(1);
411 } else if (candidateWay.way.lastNode().equals(headNode)) {
412 candidateComingToHead = candidateWay.insideToTheRight;
413 candidatePrevNode = candidateWay.way.getNode(candidateWay.way.getNodesCount() - 2);
414 } else
415 continue;
416 if (candidateComingToHead && candidateWay.equals(lastWay))
417 continue;
418
419 double candidateAngle = getAngle(headNode, candidatePrevNode, prevNode);
420
421 if (mostLeft == null || candidateAngle < angle || (Utils.equalsEpsilon(candidateAngle, angle) && !candidateComingToHead)) {
422 // Candidate is most left
423 mostLeft = candidateWay;
424 comingToHead = candidateComingToHead;
425 angle = candidateAngle;
426 }
427 }
428
429 return comingToHead ? mostLeft : null;
430 }
431 }
432
433 /**
434 * Helper storage class for finding findOuterWays
435 * @author viesturs
436 */
437 static class PolygonLevel {
438 public final int level;
439 public final AssembledMultipolygon pol;
440
441 PolygonLevel(AssembledMultipolygon pol, int level) {
442 this.pol = pol;
443 this.level = level;
444 }
445 }
446
447 /**
448 * Constructs a new {@code JoinAreasAction}.
449 */
450 public JoinAreasAction() {
451 this(true);
452 }
453
454 /**
455 * Constructs a new {@code JoinAreasAction} with optional shortcut.
456 * @param addShortcut controls whether the shortcut should be registered or not
457 * @since 11611
458 */
459 public JoinAreasAction(boolean addShortcut) {
460 super(tr("Join overlapping Areas"), "joinareas", tr("Joins areas that overlap each other"), addShortcut ?
461 Shortcut.registerShortcut("tools:joinareas", tr("Tool: {0}", tr("Join overlapping Areas")), KeyEvent.VK_J, Shortcut.SHIFT)
462 : null, true);
463 }
464
465 /**
466 * Gets called whenever the shortcut is pressed or the menu entry is selected.
467 * Checks whether the selected objects are suitable to join and joins them if so.
468 */
469 @Override
470 public void actionPerformed(ActionEvent e) {
471 join(Main.getLayerManager().getEditDataSet().getSelectedWays());
472 }
473
474 /**
475 * Joins the given ways.
476 * @param ways Ways to join
477 * @since 7534
478 */
479 public void join(Collection<Way> ways) {
480 addedRelations.clear();
481
482 if (ways.isEmpty()) {
483 new Notification(
484 tr("Please select at least one closed way that should be joined."))
485 .setIcon(JOptionPane.INFORMATION_MESSAGE)
486 .show();
487 return;
488 }
489
490 List<Node> allNodes = new ArrayList<>();
491 for (Way way : ways) {
492 if (!way.isClosed()) {
493 new Notification(
494 tr("One of the selected ways is not closed and therefore cannot be joined."))
495 .setIcon(JOptionPane.INFORMATION_MESSAGE)
496 .show();
497 return;
498 }
499
500 allNodes.addAll(way.getNodes());
501 }
502
503 // TODO: Only display this warning when nodes outside dataSourceArea are deleted
504 boolean ok = Command.checkAndConfirmOutlyingOperation("joinarea", tr("Join area confirmation"),
505 trn("The selected way has nodes outside of the downloaded data region.",
506 "The selected ways have nodes outside of the downloaded data region.",
507 ways.size()) + "<br/>"
508 + tr("This can lead to nodes being deleted accidentally.") + "<br/>"
509 + tr("Are you really sure to continue?")
510 + tr("Please abort if you are not sure"),
511 tr("The selected area is incomplete. Continue?"),
512 allNodes, null);
513 if (!ok) return;
514
515 //analyze multipolygon relations and collect all areas
516 List<Multipolygon> areas = collectMultipolygons(ways);
517
518 if (areas == null)
519 //too complex multipolygon relations found
520 return;
521
522 if (!testJoin(areas)) {
523 new Notification(
524 tr("No intersection found. Nothing was changed."))
525 .setIcon(JOptionPane.INFORMATION_MESSAGE)
526 .show();
527 return;
528 }
529
530 if (!resolveTagConflicts(areas))
531 return;
532 //user canceled, do nothing.
533
534 try {
535 // see #11026 - Because <ways> is a dynamic filtered (on ways) of a filtered (on selected objects) collection,
536 // retrieve effective dataset before joining the ways (which affects the selection, thus, the <ways> collection)
537 // Dataset retrieving allows to call this code without relying on Main.getCurrentDataSet(), thus, on a mapview instance
538 DataSet ds = ways.iterator().next().getDataSet();
539
540 // Do the job of joining areas
541 JoinAreasResult result = joinAreas(areas);
542
543 if (result.hasChanges) {
544 // move tags from ways to newly created relations
545 // TODO: do we need to also move tags for the modified relations?
546 for (Relation r: addedRelations) {
547 cmds.addAll(CreateMultipolygonAction.removeTagsFromWaysIfNeeded(r));
548 }
549 commitCommands(tr("Move tags from ways to relations"));
550
551 List<Way> allWays = new ArrayList<>();
552 for (Multipolygon pol : result.polygons) {
553 allWays.add(pol.outerWay);
554 allWays.addAll(pol.innerWays);
555 }
556 if (ds != null) {
557 ds.setSelected(allWays);
558 }
559 } else {
560 new Notification(
561 tr("No intersection found. Nothing was changed."))
562 .setIcon(JOptionPane.INFORMATION_MESSAGE)
563 .show();
564 }
565 } catch (UserCancelException exception) {
566 Main.trace(exception);
567 //revert changes
568 //FIXME: this is dirty hack
569 makeCommitsOneAction(tr("Reverting changes"));
570 Main.main.undoRedo.undo();
571 Main.main.undoRedo.redoCommands.clear();
572 }
573 }
574
575 /**
576 * Tests if the areas have some intersections to join.
577 * @param areas Areas to test
578 * @return {@code true} if areas are joinable
579 */
580 private boolean testJoin(List<Multipolygon> areas) {
581 List<Way> allStartingWays = new ArrayList<>();
582
583 for (Multipolygon area : areas) {
584 allStartingWays.add(area.outerWay);
585 allStartingWays.addAll(area.innerWays);
586 }
587
588 //find intersection points
589 Set<Node> nodes = Geometry.addIntersections(allStartingWays, true, cmds);
590 return !nodes.isEmpty();
591 }
592
593 /**
594 * Will join two or more overlapping areas
595 * @param areas list of areas to join
596 * @return new area formed.
597 * @throws UserCancelException if user cancels the operation
598 */
599 public JoinAreasResult joinAreas(List<Multipolygon> areas) throws UserCancelException {
600
601 boolean hasChanges = false;
602
603 List<Way> allStartingWays = new ArrayList<>();
604 List<Way> innerStartingWays = new ArrayList<>();
605 List<Way> outerStartingWays = new ArrayList<>();
606
607 for (Multipolygon area : areas) {
608 outerStartingWays.add(area.outerWay);
609 innerStartingWays.addAll(area.innerWays);
610 }
611
612 allStartingWays.addAll(innerStartingWays);
613 allStartingWays.addAll(outerStartingWays);
614
615 //first remove nodes in the same coordinate
616 boolean removedDuplicates = false;
617 removedDuplicates |= removeDuplicateNodes(allStartingWays);
618
619 if (removedDuplicates) {
620 hasChanges = true;
621 commitCommands(marktr("Removed duplicate nodes"));
622 }
623
624 //find intersection points
625 Set<Node> nodes = Geometry.addIntersections(allStartingWays, false, cmds);
626
627 //no intersections, return.
628 if (nodes.isEmpty())
629 return new JoinAreasResult(hasChanges, null);
630 commitCommands(marktr("Added node on all intersections"));
631
632 List<RelationRole> relations = new ArrayList<>();
633
634 // Remove ways from all relations so ways can be combined/split quietly
635 for (Way way : allStartingWays) {
636 relations.addAll(removeFromAllRelations(way));
637 }
638
639 // Don't warn now, because it will really look corrupted
640 boolean warnAboutRelations = !relations.isEmpty() && allStartingWays.size() > 1;
641
642 List<WayInPolygon> preparedWays = new ArrayList<>();
643
644 for (Way way : outerStartingWays) {
645 List<Way> splitWays = splitWayOnNodes(way, nodes);
646 preparedWays.addAll(markWayInsideSide(splitWays, false));
647 }
648
649 for (Way way : innerStartingWays) {
650 List<Way> splitWays = splitWayOnNodes(way, nodes);
651 preparedWays.addAll(markWayInsideSide(splitWays, true));
652 }
653
654 // Find boundary ways
655 List<Way> discardedWays = new ArrayList<>();
656 List<AssembledPolygon> boundaries = findBoundaryPolygons(preparedWays, discardedWays);
657
658 //find polygons
659 List<AssembledMultipolygon> preparedPolygons = findPolygons(boundaries);
660
661 //assemble final polygons
662 List<Multipolygon> polygons = new ArrayList<>();
663 Set<Relation> relationsToDelete = new LinkedHashSet<>();
664
665 for (AssembledMultipolygon pol : preparedPolygons) {
666
667 //create the new ways
668 Multipolygon resultPol = joinPolygon(pol);
669
670 //create multipolygon relation, if necessary.
671 RelationRole ownMultipolygonRelation = addOwnMultipolygonRelation(resultPol.innerWays);
672
673 //add back the original relations, merged with our new multipolygon relation
674 fixRelations(relations, resultPol.outerWay, ownMultipolygonRelation, relationsToDelete);
675
676 //strip tags from inner ways
677 //TODO: preserve tags on existing inner ways
678 stripTags(resultPol.innerWays);
679
680 polygons.add(resultPol);
681 }
682
683 commitCommands(marktr("Assemble new polygons"));
684
685 for (Relation rel: relationsToDelete) {
686 cmds.add(new DeleteCommand(rel));
687 }
688
689 commitCommands(marktr("Delete relations"));
690
691 // Delete the discarded inner ways
692 if (!discardedWays.isEmpty()) {
693 Command deleteCmd = DeleteCommand.delete(Main.getLayerManager().getEditLayer(), discardedWays, true);
694 if (deleteCmd != null) {
695 cmds.add(deleteCmd);
696 commitCommands(marktr("Delete Ways that are not part of an inner multipolygon"));
697 }
698 }
699
700 makeCommitsOneAction(marktr("Joined overlapping areas"));
701
702 if (warnAboutRelations) {
703 new Notification(
704 tr("Some of the ways were part of relations that have been modified.<br>Please verify no errors have been introduced."))
705 .setIcon(JOptionPane.INFORMATION_MESSAGE)
706 .setDuration(Notification.TIME_LONG)
707 .show();
708 }
709
710 return new JoinAreasResult(true, polygons);
711 }
712
713 /**
714 * Checks if tags of two given ways differ, and presents the user a dialog to solve conflicts
715 * @param polygons ways to check
716 * @return {@code true} if all conflicts are resolved, {@code false} if conflicts remain.
717 */
718 private boolean resolveTagConflicts(List<Multipolygon> polygons) {
719
720 List<Way> ways = new ArrayList<>();
721
722 for (Multipolygon pol : polygons) {
723 ways.add(pol.outerWay);
724 ways.addAll(pol.innerWays);
725 }
726
727 if (ways.size() < 2) {
728 return true;
729 }
730
731 TagCollection wayTags = TagCollection.unionOfAllPrimitives(ways);
732 try {
733 cmds.addAll(CombinePrimitiveResolverDialog.launchIfNecessary(wayTags, ways, ways));
734 commitCommands(marktr("Fix tag conflicts"));
735 return true;
736 } catch (UserCancelException ex) {
737 Main.trace(ex);
738 return false;
739 }
740 }
741
742 /**
743 * This method removes duplicate points (if any) from the input way.
744 * @param ways the ways to process
745 * @return {@code true} if any changes where made
746 */
747 private boolean removeDuplicateNodes(List<Way> ways) {
748 //TODO: maybe join nodes with JoinNodesAction, rather than reconnect the ways.
749
750 Map<Node, Node> nodeMap = new TreeMap<>(new NodePositionComparator());
751 int totalNodesRemoved = 0;
752
753 for (Way way : ways) {
754 if (way.getNodes().size() < 2) {
755 continue;
756 }
757
758 int nodesRemoved = 0;
759 List<Node> newNodes = new ArrayList<>();
760 Node prevNode = null;
761
762 for (Node node : way.getNodes()) {
763 if (!nodeMap.containsKey(node)) {
764 //new node
765 nodeMap.put(node, node);
766
767 //avoid duplicate nodes
768 if (prevNode != node) {
769 newNodes.add(node);
770 } else {
771 nodesRemoved++;
772 }
773 } else {
774 //node with same coordinates already exists, substitute with existing node
775 Node representator = nodeMap.get(node);
776
777 if (representator != node) {
778 nodesRemoved++;
779 }
780
781 //avoid duplicate node
782 if (prevNode != representator) {
783 newNodes.add(representator);
784 }
785 }
786 prevNode = node;
787 }
788
789 if (nodesRemoved > 0) {
790
791 if (newNodes.size() == 1) { //all nodes in the same coordinate - add one more node, to have closed way.
792 newNodes.add(newNodes.get(0));
793 }
794
795 Way newWay = new Way(way);
796 newWay.setNodes(newNodes);
797 cmds.add(new ChangeCommand(way, newWay));
798 totalNodesRemoved += nodesRemoved;
799 }
800 }
801
802 return totalNodesRemoved > 0;
803 }
804
805 /**
806 * Commits the command list with a description
807 * @param description The description of what the commands do
808 */
809 private void commitCommands(String description) {
810 switch(cmds.size()) {
811 case 0:
812 return;
813 case 1:
814 commitCommand(cmds.getFirst());
815 break;
816 default:
817 commitCommand(new SequenceCommand(tr(description), cmds));
818 break;
819 }
820
821 cmds.clear();
822 cmdsCount++;
823 }
824
825 private static void commitCommand(Command c) {
826 if (Main.main != null) {
827 Main.main.undoRedo.add(c);
828 } else {
829 c.executeCommand();
830 }
831 }
832
833 /**
834 * This method analyzes the way and assigns each part what direction polygon "inside" is.
835 * @param parts the split parts of the way
836 * @param isInner - if true, reverts the direction (for multipolygon islands)
837 * @return list of parts, marked with the inside orientation.
838 * @throws IllegalArgumentException if parts is empty or not circular
839 */
840 private static List<WayInPolygon> markWayInsideSide(List<Way> parts, boolean isInner) {
841
842 //prepare next map
843 Map<Way, Way> nextWayMap = new HashMap<>();
844
845 for (int pos = 0; pos < parts.size(); pos++) {
846
847 if (!parts.get(pos).lastNode().equals(parts.get((pos + 1) % parts.size()).firstNode()))
848 throw new IllegalArgumentException("Way not circular");
849
850 nextWayMap.put(parts.get(pos), parts.get((pos + 1) % parts.size()));
851 }
852
853 //find the node with minimum y - it's guaranteed to be outer. (What about the south pole?)
854 Way topWay = null;
855 Node topNode = null;
856 int topIndex = 0;
857 double minY = Double.POSITIVE_INFINITY;
858
859 for (Way way : parts) {
860 for (int pos = 0; pos < way.getNodesCount(); pos++) {
861 Node node = way.getNode(pos);
862
863 if (node.getEastNorth().getY() < minY) {
864 minY = node.getEastNorth().getY();
865 topWay = way;
866 topNode = node;
867 topIndex = pos;
868 }
869 }
870 }
871
872 if (topWay == null || topNode == null) {
873 throw new IllegalArgumentException();
874 }
875
876 //get the upper way and it's orientation.
877
878 boolean wayClockwise; // orientation of the top way.
879
880 if (topNode.equals(topWay.firstNode()) || topNode.equals(topWay.lastNode())) {
881 Node headNode; // the node at junction
882 Node prevNode; // last node from previous path
883
884 //node is in split point - find the outermost way from this point
885
886 headNode = topNode;
887 //make a fake node that is downwards from head node (smaller Y). It will be a division point between paths.
888 prevNode = new Node(new EastNorth(headNode.getEastNorth().getX(), headNode.getEastNorth().getY() - 1e5));
889
890 topWay = null;
891 wayClockwise = false;
892 Node bestWayNextNode = null;
893
894 for (Way way : parts) {
895 if (way.firstNode().equals(headNode)) {
896 Node nextNode = way.getNode(1);
897
898 if (topWay == null || !Geometry.isToTheRightSideOfLine(prevNode, headNode, bestWayNextNode, nextNode)) {
899 //the new way is better
900 topWay = way;
901 wayClockwise = true;
902 bestWayNextNode = nextNode;
903 }
904 }
905
906 if (way.lastNode().equals(headNode)) {
907 //end adjacent to headNode
908 Node nextNode = way.getNode(way.getNodesCount() - 2);
909
910 if (topWay == null || !Geometry.isToTheRightSideOfLine(prevNode, headNode, bestWayNextNode, nextNode)) {
911 //the new way is better
912 topWay = way;
913 wayClockwise = false;
914 bestWayNextNode = nextNode;
915 }
916 }
917 }
918 } else {
919 //node is inside way - pick the clockwise going end.
920 Node prev = topWay.getNode(topIndex - 1);
921 Node next = topWay.getNode(topIndex + 1);
922
923 //there will be no parallel segments in the middle of way, so all fine.
924 wayClockwise = Geometry.angleIsClockwise(prev, topNode, next);
925 }
926
927 Way curWay = topWay;
928 boolean curWayInsideToTheRight = wayClockwise ^ isInner;
929 List<WayInPolygon> result = new ArrayList<>();
930
931 //iterate till full circle is reached
932 while (curWay != null) {
933
934 //add cur way
935 WayInPolygon resultWay = new WayInPolygon(curWay, curWayInsideToTheRight);
936 result.add(resultWay);
937
938 //process next way
939 Way nextWay = nextWayMap.get(curWay);
940 Node prevNode = curWay.getNode(curWay.getNodesCount() - 2);
941 Node headNode = curWay.lastNode();
942 Node nextNode = nextWay.getNode(1);
943
944 if (nextWay == topWay) {
945 //full loop traversed - all done.
946 break;
947 }
948
949 //find intersecting segments
950 // the intersections will look like this:
951 //
952 // ^
953 // |
954 // X wayBNode
955 // |
956 // wayB |
957 // |
958 // curWay | nextWay
959 //----X----------------->X----------------------X---->
960 // prevNode ^headNode nextNode
961 // |
962 // |
963 // wayA |
964 // |
965 // X wayANode
966 // |
967
968 int intersectionCount = 0;
969
970 for (Way wayA : parts) {
971
972 if (wayA == curWay) {
973 continue;
974 }
975
976 if (wayA.lastNode().equals(headNode)) {
977
978 Way wayB = nextWayMap.get(wayA);
979
980 //test if wayA is opposite wayB relative to curWay and nextWay
981
982 Node wayANode = wayA.getNode(wayA.getNodesCount() - 2);
983 Node wayBNode = wayB.getNode(1);
984
985 boolean wayAToTheRight = Geometry.isToTheRightSideOfLine(prevNode, headNode, nextNode, wayANode);
986 boolean wayBToTheRight = Geometry.isToTheRightSideOfLine(prevNode, headNode, nextNode, wayBNode);
987
988 if (wayAToTheRight != wayBToTheRight) {
989 intersectionCount++;
990 }
991 }
992 }
993
994 //if odd number of crossings, invert orientation
995 if (intersectionCount % 2 != 0) {
996 curWayInsideToTheRight = !curWayInsideToTheRight;
997 }
998
999 curWay = nextWay;
1000 }
1001
1002 return result;
1003 }
1004
1005 /**
1006 * This is a method that splits way into smaller parts, using the prepared nodes list as split points.
1007 * Uses {@link SplitWayAction#splitWay} for the heavy lifting.
1008 * @param way way to split
1009 * @param nodes split points
1010 * @return list of split ways (or original ways if no splitting is done).
1011 */
1012 private List<Way> splitWayOnNodes(Way way, Set<Node> nodes) {
1013
1014 List<Way> result = new ArrayList<>();
1015 List<List<Node>> chunks = buildNodeChunks(way, nodes);
1016
1017 if (chunks.size() > 1) {
1018 SplitWayResult split = SplitWayAction.splitWay(getLayerManager().getEditLayer(), way, chunks,
1019 Collections.<OsmPrimitive>emptyList(), SplitWayAction.Strategy.keepFirstChunk());
1020
1021 if (split != null) {
1022 //execute the command, we need the results
1023 cmds.add(split.getCommand());
1024 commitCommands(marktr("Split ways into fragments"));
1025
1026 result.add(split.getOriginalWay());
1027 result.addAll(split.getNewWays());
1028 }
1029 }
1030 if (result.isEmpty()) {
1031 //nothing to split
1032 result.add(way);
1033 }
1034
1035 return result;
1036 }
1037
1038 /**
1039 * Simple chunking version. Does not care about circular ways and result being
1040 * proper, we will glue it all back together later on.
1041 * @param way the way to chunk
1042 * @param splitNodes the places where to cut.
1043 * @return list of node paths to produce.
1044 */
1045 private static List<List<Node>> buildNodeChunks(Way way, Collection<Node> splitNodes) {
1046 List<List<Node>> result = new ArrayList<>();
1047 List<Node> curList = new ArrayList<>();
1048
1049 for (Node node : way.getNodes()) {
1050 curList.add(node);
1051 if (curList.size() > 1 && splitNodes.contains(node)) {
1052 result.add(curList);
1053 curList = new ArrayList<>();
1054 curList.add(node);
1055 }
1056 }
1057
1058 if (curList.size() > 1) {
1059 result.add(curList);
1060 }
1061
1062 return result;
1063 }
1064
1065 /**
1066 * This method finds which ways are outer and which are inner.
1067 * @param boundaries list of joined boundaries to search in
1068 * @return outer ways
1069 */
1070 private static List<AssembledMultipolygon> findPolygons(Collection<AssembledPolygon> boundaries) {
1071
1072 List<PolygonLevel> list = findOuterWaysImpl(0, boundaries);
1073 List<AssembledMultipolygon> result = new ArrayList<>();
1074
1075 //take every other level
1076 for (PolygonLevel pol : list) {
1077 if (pol.level % 2 == 0) {
1078 result.add(pol.pol);
1079 }
1080 }
1081
1082 return result;
1083 }
1084
1085 /**
1086 * Collects outer way and corresponding inner ways from all boundaries.
1087 * @param level depth level
1088 * @param boundaryWays list of joined boundaries to search in
1089 * @return the outermost Way.
1090 */
1091 private static List<PolygonLevel> findOuterWaysImpl(int level, Collection<AssembledPolygon> boundaryWays) {
1092
1093 //TODO: bad performance for deep nestings...
1094 List<PolygonLevel> result = new ArrayList<>();
1095
1096 for (AssembledPolygon outerWay : boundaryWays) {
1097
1098 boolean outerGood = true;
1099 List<AssembledPolygon> innerCandidates = new ArrayList<>();
1100
1101 for (AssembledPolygon innerWay : boundaryWays) {
1102 if (innerWay == outerWay) {
1103 continue;
1104 }
1105
1106 if (wayInsideWay(outerWay, innerWay)) {
1107 outerGood = false;
1108 break;
1109 } else if (wayInsideWay(innerWay, outerWay)) {
1110 innerCandidates.add(innerWay);
1111 }
1112 }
1113
1114 if (!outerGood) {
1115 continue;
1116 }
1117
1118 //add new outer polygon
1119 AssembledMultipolygon pol = new AssembledMultipolygon(outerWay);
1120 PolygonLevel polLev = new PolygonLevel(pol, level);
1121
1122 //process inner ways
1123 if (!innerCandidates.isEmpty()) {
1124 List<PolygonLevel> innerList = findOuterWaysImpl(level + 1, innerCandidates);
1125 result.addAll(innerList);
1126
1127 for (PolygonLevel pl : innerList) {
1128 if (pl.level == level + 1) {
1129 pol.innerWays.add(pl.pol.outerWay);
1130 }
1131 }
1132 }
1133
1134 result.add(polLev);
1135 }
1136
1137 return result;
1138 }
1139
1140 /**
1141 * Finds all ways that form inner or outer boundaries.
1142 * @param multigonWays A list of (splitted) ways that form a multigon and share common end nodes on intersections.
1143 * @param discardedResult this list is filled with ways that are to be discarded
1144 * @return A list of ways that form the outer and inner boundaries of the multigon.
1145 */
1146 public static List<AssembledPolygon> findBoundaryPolygons(Collection<WayInPolygon> multigonWays,
1147 List<Way> discardedResult) {
1148 // In multigonWays collection, some way are just a point (i.e. way like nodeA-nodeA)
1149 // This seems to appear when is apply over invalid way like #9911 test-case
1150 // Remove all of these way to make the next work.
1151 List<WayInPolygon> cleanMultigonWays = new ArrayList<>();
1152 for (WayInPolygon way: multigonWays) {
1153 if (way.way.getNodesCount() != 2 || !way.way.isClosed())
1154 cleanMultigonWays.add(way);
1155 }
1156
1157 WayTraverser traverser = new WayTraverser(cleanMultigonWays);
1158 List<AssembledPolygon> result = new ArrayList<>();
1159
1160 WayInPolygon startWay;
1161 while ((startWay = traverser.startNewWay()) != null) {
1162 List<WayInPolygon> path = new ArrayList<>();
1163 List<WayInPolygon> startWays = new ArrayList<>();
1164 path.add(startWay);
1165 while (true) {
1166 WayInPolygon leftComing;
1167 while ((leftComing = traverser.leftComingWay()) != null) {
1168 if (startWays.contains(leftComing))
1169 break;
1170 // Need restart traverser walk
1171 path.clear();
1172 path.add(leftComing);
1173 traverser.setStartWay(leftComing);
1174 startWays.add(leftComing);
1175 break;
1176 }
1177 WayInPolygon nextWay = traverser.walk();
1178 if (nextWay == null)
1179 throw new JosmRuntimeException("Join areas internal error.");
1180 if (path.get(0) == nextWay) {
1181 // path is closed -> stop here
1182 AssembledPolygon ring = new AssembledPolygon(path);
1183 if (ring.getNodes().size() <= 2) {
1184 // Invalid ring (2 nodes) -> remove
1185 traverser.removeWays(path);
1186 for (WayInPolygon way: path) {
1187 discardedResult.add(way.way);
1188 }
1189 } else {
1190 // Close ring -> add
1191 result.add(ring);
1192 traverser.removeWays(path);
1193 }
1194 break;
1195 }
1196 if (path.contains(nextWay)) {
1197 // Inner loop -> remove
1198 int index = path.indexOf(nextWay);
1199 while (path.size() > index) {
1200 WayInPolygon currentWay = path.get(index);
1201 discardedResult.add(currentWay.way);
1202 traverser.removeWay(currentWay);
1203 path.remove(index);
1204 }
1205 traverser.setStartWay(path.get(index-1));
1206 } else {
1207 path.add(nextWay);
1208 }
1209 }
1210 }
1211
1212 return fixTouchingPolygons(result);
1213 }
1214
1215 /**
1216 * This method checks if polygons have several touching parts and splits them in several polygons.
1217 * @param polygons the polygons to process.
1218 * @return the resulting list of polygons
1219 */
1220 public static List<AssembledPolygon> fixTouchingPolygons(List<AssembledPolygon> polygons) {
1221 List<AssembledPolygon> newPolygons = new ArrayList<>();
1222
1223 for (AssembledPolygon ring : polygons) {
1224 ring.reverse();
1225 WayTraverser traverser = new WayTraverser(ring.ways);
1226 WayInPolygon startWay;
1227
1228 while ((startWay = traverser.startNewWay()) != null) {
1229 List<WayInPolygon> simpleRingWays = new ArrayList<>();
1230 simpleRingWays.add(startWay);
1231 WayInPolygon nextWay;
1232 while ((nextWay = traverser.walk()) != startWay) {
1233 if (nextWay == null)
1234 throw new JosmRuntimeException("Join areas internal error.");
1235 simpleRingWays.add(nextWay);
1236 }
1237 traverser.removeWays(simpleRingWays);
1238 AssembledPolygon simpleRing = new AssembledPolygon(simpleRingWays);
1239 simpleRing.reverse();
1240 newPolygons.add(simpleRing);
1241 }
1242 }
1243
1244 return newPolygons;
1245 }
1246
1247 /**
1248 * Tests if way is inside other way
1249 * @param outside outer polygon description
1250 * @param inside inner polygon description
1251 * @return {@code true} if inner is inside outer
1252 */
1253 public static boolean wayInsideWay(AssembledPolygon inside, AssembledPolygon outside) {
1254 Set<Node> outsideNodes = new HashSet<>(outside.getNodes());
1255 List<Node> insideNodes = inside.getNodes();
1256
1257 for (Node insideNode : insideNodes) {
1258
1259 if (!outsideNodes.contains(insideNode))
1260 //simply test the one node
1261 return Geometry.nodeInsidePolygon(insideNode, outside.getNodes());
1262 }
1263
1264 //all nodes shared.
1265 return false;
1266 }
1267
1268 /**
1269 * Joins the lists of ways.
1270 * @param polygon The list of outer ways that belong to that multipolygon.
1271 * @return The newly created outer way
1272 * @throws UserCancelException if user cancels the operation
1273 */
1274 private Multipolygon joinPolygon(AssembledMultipolygon polygon) throws UserCancelException {
1275 Multipolygon result = new Multipolygon(joinWays(polygon.outerWay.ways));
1276
1277 for (AssembledPolygon pol : polygon.innerWays) {
1278 result.innerWays.add(joinWays(pol.ways));
1279 }
1280
1281 return result;
1282 }
1283
1284 /**
1285 * Joins the outer ways and deletes all short ways that can't be part of a multipolygon anyway.
1286 * @param ways The list of outer ways that belong to that multigon.
1287 * @return The newly created outer way
1288 * @throws UserCancelException if user cancels the operation
1289 */
1290 private Way joinWays(List<WayInPolygon> ways) throws UserCancelException {
1291
1292 //leave original orientation, if all paths are reverse.
1293 boolean allReverse = true;
1294 for (WayInPolygon way : ways) {
1295 allReverse &= !way.insideToTheRight;
1296 }
1297
1298 if (allReverse) {
1299 for (WayInPolygon way : ways) {
1300 way.insideToTheRight = !way.insideToTheRight;
1301 }
1302 }
1303
1304 Way joinedWay = joinOrientedWays(ways);
1305
1306 //should not happen
1307 if (joinedWay == null || !joinedWay.isClosed())
1308 throw new JosmRuntimeException("Join areas internal error.");
1309
1310 return joinedWay;
1311 }
1312
1313 /**
1314 * Joins a list of ways (using CombineWayAction and ReverseWayAction as specified in WayInPath)
1315 * @param ways The list of ways to join and reverse
1316 * @return The newly created way
1317 * @throws UserCancelException if user cancels the operation
1318 */
1319 private Way joinOrientedWays(List<WayInPolygon> ways) throws UserCancelException {
1320 if (ways.size() < 2)
1321 return ways.get(0).way;
1322
1323 // This will turn ways so all of them point in the same direction and CombineAction won't bug
1324 // the user about this.
1325
1326 //TODO: ReverseWay and Combine way are really slow and we use them a lot here. This slows down large joins.
1327 List<Way> actionWays = new ArrayList<>(ways.size());
1328
1329 for (WayInPolygon way : ways) {
1330 actionWays.add(way.way);
1331
1332 if (!way.insideToTheRight) {
1333 ReverseWayResult res = ReverseWayAction.reverseWay(way.way);
1334 commitCommand(res.getReverseCommand());
1335 cmdsCount++;
1336 }
1337 }
1338
1339 Pair<Way, Command> result = CombineWayAction.combineWaysWorker(actionWays);
1340
1341 commitCommand(result.b);
1342 cmdsCount++;
1343
1344 return result.a;
1345 }
1346
1347 /**
1348 * This method analyzes multipolygon relationships of given ways and collects addition inner ways to consider.
1349 * @param selectedWays the selected ways
1350 * @return list of polygons, or null if too complex relation encountered.
1351 */
1352 public static List<Multipolygon> collectMultipolygons(Collection<Way> selectedWays) {
1353
1354 List<Multipolygon> result = new ArrayList<>();
1355
1356 //prepare the lists, to minimize memory allocation.
1357 List<Way> outerWays = new ArrayList<>();
1358 List<Way> innerWays = new ArrayList<>();
1359
1360 Set<Way> processedOuterWays = new LinkedHashSet<>();
1361 Set<Way> processedInnerWays = new LinkedHashSet<>();
1362
1363 for (Relation r : OsmPrimitive.getParentRelations(selectedWays)) {
1364 if (r.isDeleted() || !r.isMultipolygon()) {
1365 continue;
1366 }
1367
1368 boolean hasKnownOuter = false;
1369 outerWays.clear();
1370 innerWays.clear();
1371
1372 for (RelationMember rm : r.getMembers()) {
1373 if ("outer".equalsIgnoreCase(rm.getRole())) {
1374 outerWays.add(rm.getWay());
1375 hasKnownOuter |= selectedWays.contains(rm.getWay());
1376 } else if ("inner".equalsIgnoreCase(rm.getRole())) {
1377 innerWays.add(rm.getWay());
1378 }
1379 }
1380
1381 if (!hasKnownOuter) {
1382 continue;
1383 }
1384
1385 if (outerWays.size() > 1) {
1386 new Notification(
1387 tr("Sorry. Cannot handle multipolygon relations with multiple outer ways."))
1388 .setIcon(JOptionPane.INFORMATION_MESSAGE)
1389 .show();
1390 return null;
1391 }
1392
1393 Way outerWay = outerWays.get(0);
1394
1395 //retain only selected inner ways
1396 innerWays.retainAll(selectedWays);
1397
1398 if (processedOuterWays.contains(outerWay)) {
1399 new Notification(
1400 tr("Sorry. Cannot handle way that is outer in multiple multipolygon relations."))
1401 .setIcon(JOptionPane.INFORMATION_MESSAGE)
1402 .show();
1403 return null;
1404 }
1405
1406 if (processedInnerWays.contains(outerWay)) {
1407 new Notification(
1408 tr("Sorry. Cannot handle way that is both inner and outer in multipolygon relations."))
1409 .setIcon(JOptionPane.INFORMATION_MESSAGE)
1410 .show();
1411 return null;
1412 }
1413
1414 for (Way way :innerWays) {
1415 if (processedOuterWays.contains(way)) {
1416 new Notification(
1417 tr("Sorry. Cannot handle way that is both inner and outer in multipolygon relations."))
1418 .setIcon(JOptionPane.INFORMATION_MESSAGE)
1419 .show();
1420 return null;
1421 }
1422
1423 if (processedInnerWays.contains(way)) {
1424 new Notification(
1425 tr("Sorry. Cannot handle way that is inner in multiple multipolygon relations."))
1426 .setIcon(JOptionPane.INFORMATION_MESSAGE)
1427 .show();
1428 return null;
1429 }
1430 }
1431
1432 processedOuterWays.add(outerWay);
1433 processedInnerWays.addAll(innerWays);
1434
1435 Multipolygon pol = new Multipolygon(outerWay);
1436 pol.innerWays.addAll(innerWays);
1437
1438 result.add(pol);
1439 }
1440
1441 //add remaining ways, not in relations
1442 for (Way way : selectedWays) {
1443 if (processedOuterWays.contains(way) || processedInnerWays.contains(way)) {
1444 continue;
1445 }
1446
1447 result.add(new Multipolygon(way));
1448 }
1449
1450 return result;
1451 }
1452
1453 /**
1454 * Will add own multipolygon relation to the "previously existing" relations. Fixup is done by fixRelations
1455 * @param inner List of already closed inner ways
1456 * @return The list of relation with roles to add own relation to
1457 */
1458 private RelationRole addOwnMultipolygonRelation(Collection<Way> inner) {
1459 if (inner.isEmpty()) return null;
1460 OsmDataLayer layer = Main.getLayerManager().getEditLayer();
1461 // Create new multipolygon relation and add all inner ways to it
1462 Relation newRel = new Relation();
1463 newRel.put("type", "multipolygon");
1464 for (Way w : inner) {
1465 newRel.addMember(new RelationMember("inner", w));
1466 }
1467 cmds.add(layer != null ? new AddCommand(layer, newRel) :
1468 new AddCommand(inner.iterator().next().getDataSet(), newRel));
1469 addedRelations.add(newRel);
1470
1471 // We don't add outer to the relation because it will be handed to fixRelations()
1472 // which will then do the remaining work.
1473 return new RelationRole(newRel, "outer");
1474 }
1475
1476 /**
1477 * Removes a given OsmPrimitive from all relations.
1478 * @param osm Element to remove from all relations
1479 * @return List of relations with roles the primitives was part of
1480 */
1481 private List<RelationRole> removeFromAllRelations(OsmPrimitive osm) {
1482 List<RelationRole> result = new ArrayList<>();
1483
1484 for (Relation r : osm.getDataSet().getRelations()) {
1485 if (r.isDeleted()) {
1486 continue;
1487 }
1488 for (RelationMember rm : r.getMembers()) {
1489 if (rm.getMember() != osm) {
1490 continue;
1491 }
1492
1493 Relation newRel = new Relation(r);
1494 List<RelationMember> members = newRel.getMembers();
1495 members.remove(rm);
1496 newRel.setMembers(members);
1497
1498 cmds.add(new ChangeCommand(r, newRel));
1499 RelationRole saverel = new RelationRole(r, rm.getRole());
1500 if (!result.contains(saverel)) {
1501 result.add(saverel);
1502 }
1503 break;
1504 }
1505 }
1506
1507 commitCommands(marktr("Removed Element from Relations"));
1508 return result;
1509 }
1510
1511 /**
1512 * Adds the previously removed relations again to the outer way. If there are multiple multipolygon
1513 * relations where the joined areas were in "outer" role a new relation is created instead with all
1514 * members of both. This function depends on multigon relations to be valid already, it won't fix them.
1515 * @param rels List of relations with roles the (original) ways were part of
1516 * @param outer The newly created outer area/way
1517 * @param ownMultipol elements to directly add as outer
1518 * @param relationsToDelete set of relations to delete.
1519 */
1520 private void fixRelations(List<RelationRole> rels, Way outer, RelationRole ownMultipol, Set<Relation> relationsToDelete) {
1521 List<RelationRole> multiouters = new ArrayList<>();
1522
1523 if (ownMultipol != null) {
1524 multiouters.add(ownMultipol);
1525 }
1526
1527 for (RelationRole r : rels) {
1528 if (r.rel.isMultipolygon() && "outer".equalsIgnoreCase(r.role)) {
1529 multiouters.add(r);
1530 continue;
1531 }
1532 // Add it back!
1533 Relation newRel = new Relation(r.rel);
1534 newRel.addMember(new RelationMember(r.role, outer));
1535 cmds.add(new ChangeCommand(r.rel, newRel));
1536 }
1537
1538 OsmDataLayer layer = Main.getLayerManager().getEditLayer();
1539 Relation newRel;
1540 switch (multiouters.size()) {
1541 case 0:
1542 return;
1543 case 1:
1544 // Found only one to be part of a multipolygon relation, so just add it back as well
1545 newRel = new Relation(multiouters.get(0).rel);
1546 newRel.addMember(new RelationMember(multiouters.get(0).role, outer));
1547 cmds.add(new ChangeCommand(multiouters.get(0).rel, newRel));
1548 return;
1549 default:
1550 // Create a new relation with all previous members and (Way)outer as outer.
1551 newRel = new Relation();
1552 for (RelationRole r : multiouters) {
1553 // Add members
1554 for (RelationMember rm : r.rel.getMembers()) {
1555 if (!newRel.getMembers().contains(rm)) {
1556 newRel.addMember(rm);
1557 }
1558 }
1559 // Add tags
1560 for (String key : r.rel.keySet()) {
1561 newRel.put(key, r.rel.get(key));
1562 }
1563 // Delete old relation
1564 relationsToDelete.add(r.rel);
1565 }
1566 newRel.addMember(new RelationMember("outer", outer));
1567 cmds.add(layer != null ? new AddCommand(layer, newRel) : new AddCommand(outer.getDataSet(), newRel));
1568 }
1569 }
1570
1571 /**
1572 * Remove all tags from the all the way
1573 * @param ways The List of Ways to remove all tags from
1574 */
1575 private void stripTags(Collection<Way> ways) {
1576 for (Way w : ways) {
1577 final Way wayWithoutTags = new Way(w);
1578 wayWithoutTags.removeAll();
1579 cmds.add(new ChangeCommand(w, wayWithoutTags));
1580 }
1581 /* I18N: current action printed in status display */
1582 commitCommands(marktr("Remove tags from inner ways"));
1583 }
1584
1585 /**
1586 * Takes the last cmdsCount actions back and combines them into a single action
1587 * (for when the user wants to undo the join action)
1588 * @param message The commit message to display
1589 */
1590 private void makeCommitsOneAction(String message) {
1591 cmds.clear();
1592 if (Main.main != null) {
1593 UndoRedoHandler ur = Main.main.undoRedo;
1594 int i = Math.max(ur.commands.size() - cmdsCount, 0);
1595 for (; i < ur.commands.size(); i++) {
1596 cmds.add(ur.commands.get(i));
1597 }
1598
1599 for (i = 0; i < cmds.size(); i++) {
1600 ur.undo();
1601 }
1602 }
1603
1604 commitCommands(message == null ? marktr("Join Areas Function") : message);
1605 cmdsCount = 0;
1606 }
1607
1608 @Override
1609 protected void updateEnabledState() {
1610 updateEnabledStateOnCurrentSelection();
1611 }
1612
1613 @Override
1614 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
1615 setEnabled(selection != null && !selection.isEmpty());
1616 }
1617}
Note: See TracBrowser for help on using the repository browser.