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