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