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

Last change on this file since 4458 was 4458, checked in by simon04, 13 years ago

fix #3951 - user should be warned when unglue-ing two ways outside the download area

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