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