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 |
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.Polygon;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.geom.Area;
|
---|
12 | import java.awt.geom.Line2D;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.HashMap;
|
---|
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 | import java.util.TreeSet;
|
---|
23 | import java.util.Map.Entry;
|
---|
24 |
|
---|
25 | import javax.swing.Box;
|
---|
26 | import javax.swing.JComboBox;
|
---|
27 | import javax.swing.JLabel;
|
---|
28 | import javax.swing.JOptionPane;
|
---|
29 | import javax.swing.JPanel;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.command.AddCommand;
|
---|
33 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
34 | import org.openstreetmap.josm.command.Command;
|
---|
35 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
36 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
37 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
38 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
39 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
40 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
41 | import org.openstreetmap.josm.data.osm.Node;
|
---|
42 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
43 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
44 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
45 | import org.openstreetmap.josm.data.osm.TigerUtils;
|
---|
46 | import org.openstreetmap.josm.data.osm.Way;
|
---|
47 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
48 | import org.openstreetmap.josm.tools.GBC;
|
---|
49 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
50 |
|
---|
51 | public class JoinAreasAction extends JosmAction {
|
---|
52 | // This will be used to commit commands and unite them into one large command sequence at the end
|
---|
53 | private LinkedList<Command> cmds = new LinkedList<Command>();
|
---|
54 | private int cmdsCount = 0;
|
---|
55 |
|
---|
56 | // HelperClass
|
---|
57 | // Saves a node and two positions where to insert the node into the ways
|
---|
58 | private static class NodeToSegs implements Comparable<NodeToSegs> {
|
---|
59 | public int pos;
|
---|
60 | public Node n;
|
---|
61 | public double dis;
|
---|
62 | public NodeToSegs(int pos, Node n, LatLon dis) {
|
---|
63 | this.pos = pos;
|
---|
64 | this.n = n;
|
---|
65 | this.dis = n.getCoor().greatCircleDistance(dis);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public int compareTo(NodeToSegs o) {
|
---|
69 | if(this.pos == o.pos)
|
---|
70 | return (this.dis - o.dis) > 0 ? 1 : -1;
|
---|
71 | return this.pos - o.pos;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public int hashCode() {
|
---|
76 | return pos;
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public boolean equals(Object o) {
|
---|
81 | if (o instanceof NodeToSegs)
|
---|
82 | return compareTo((NodeToSegs) o) == 0;
|
---|
83 | else
|
---|
84 | return false;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | // HelperClass
|
---|
89 | // Saves a relation and a role an OsmPrimitve was part of until it was stripped from all relations
|
---|
90 | private static class RelationRole {
|
---|
91 | public final Relation rel;
|
---|
92 | public final String role;
|
---|
93 | public RelationRole(Relation rel, String role) {
|
---|
94 | this.rel = rel;
|
---|
95 | this.role = role;
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public int hashCode() {
|
---|
100 | return rel.hashCode();
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public boolean equals(Object other) {
|
---|
105 | if (!(other instanceof RelationRole)) return false;
|
---|
106 | RelationRole otherMember = (RelationRole) other;
|
---|
107 | return otherMember.role.equals(role) && otherMember.rel.equals(rel);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | // Adds the menu entry, Shortcuts, etc.
|
---|
112 | public JoinAreasAction() {
|
---|
113 | super(tr("Join overlapping Areas"), "joinareas", tr("Joins areas that overlap each other"), Shortcut.registerShortcut("tools:joinareas", tr("Tool: {0}", tr("Join overlapping Areas")),
|
---|
114 | KeyEvent.VK_J, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Gets called whenever the shortcut is pressed or the menu entry is selected
|
---|
119 | * Checks whether the selected objects are suitable to join and joins them if so
|
---|
120 | */
|
---|
121 | public void actionPerformed(ActionEvent e) {
|
---|
122 | LinkedList<Way> ways = new LinkedList<Way>(Main.main.getCurrentDataSet().getSelectedWays());
|
---|
123 |
|
---|
124 | if (ways.isEmpty()) {
|
---|
125 | JOptionPane.showMessageDialog(Main.parent, tr("Please select at least one closed way that should be joined."));
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | // Too many ways
|
---|
130 | if(ways.size() > 2) {
|
---|
131 | JOptionPane.showMessageDialog(Main.parent, tr("Only up to two areas can be joined at the moment."));
|
---|
132 | return;
|
---|
133 | }
|
---|
134 |
|
---|
135 | List<Node> allNodes = new ArrayList<Node>();
|
---|
136 | for (Way way: ways) {
|
---|
137 | if(!way.isClosed()) {
|
---|
138 | JOptionPane.showMessageDialog(Main.parent, tr("\"{0}\" is not closed and therefore can't be joined.", way.getName()));
|
---|
139 | return;
|
---|
140 | }
|
---|
141 |
|
---|
142 | allNodes.addAll(way.getNodes());
|
---|
143 | }
|
---|
144 |
|
---|
145 | // TODO: Only display this warning when nodes outside dataSourceArea are deleted
|
---|
146 | Area dataSourceArea = Main.main.getCurrentDataSet().getDataSourceArea();
|
---|
147 | if (dataSourceArea != null) {
|
---|
148 | for (Node node: allNodes) {
|
---|
149 | if (!dataSourceArea.contains(node.getCoor())) {
|
---|
150 | int option = JOptionPane.showConfirmDialog(Main.parent,
|
---|
151 | tr("The selected way(s) have nodes outside of the downloaded data region.\n"
|
---|
152 | + "This can lead to nodes being deleted accidentally.\n"
|
---|
153 | + "Are you really sure to continue?"),
|
---|
154 | tr("Please abort if you are not sure"), JOptionPane.YES_NO_OPTION,
|
---|
155 | JOptionPane.WARNING_MESSAGE);
|
---|
156 |
|
---|
157 | if (option != JOptionPane.YES_OPTION) return;
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | if(joinAreas(ways.getFirst(), ways.getLast())) {
|
---|
164 | Main.map.mapView.repaint();
|
---|
165 | DataSet ds = Main.main.getCurrentDataSet();
|
---|
166 | ds.fireSelectionChanged();
|
---|
167 | } else {
|
---|
168 | JOptionPane.showMessageDialog(Main.parent, tr("No intersection found. Nothing was changed."));
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Will join two overlapping areas
|
---|
174 | * @param Way First way/area
|
---|
175 | * @param Way Second way/area
|
---|
176 | * @return boolean Whether to display the "no operation" message
|
---|
177 | */
|
---|
178 | private boolean joinAreas(Way a, Way b) {
|
---|
179 | // Fix self-overlapping first or other errors
|
---|
180 | boolean same = a.equals(b);
|
---|
181 | boolean hadChanges = false;
|
---|
182 | if(!same) {
|
---|
183 | if(checkForTagConflicts(a, b)) return true; // User aborted, so don't warn again
|
---|
184 | hadChanges = joinAreas(a, a);
|
---|
185 | hadChanges = joinAreas(b, b) || hadChanges;
|
---|
186 | }
|
---|
187 |
|
---|
188 | ArrayList<OsmPrimitive> nodes = addIntersections(a, b);
|
---|
189 | if(nodes.size() == 0) return hadChanges;
|
---|
190 | commitCommands(marktr("Added node on all intersections"));
|
---|
191 |
|
---|
192 | // Remove ways from all relations so ways can be combined/split quietly
|
---|
193 | ArrayList<RelationRole> relations = removeFromRelations(a);
|
---|
194 | if(!same) {
|
---|
195 | relations.addAll(removeFromRelations(b));
|
---|
196 | }
|
---|
197 |
|
---|
198 | // Don't warn now, because it will really look corrupted
|
---|
199 | boolean warnAboutRelations = relations.size() > 0;
|
---|
200 |
|
---|
201 | Collection<Way> allWays = splitWaysOnNodes(a, b, nodes);
|
---|
202 |
|
---|
203 | // Find all nodes and inner ways save them to a list
|
---|
204 | Collection<Node> allNodes = getNodesFromWays(allWays);
|
---|
205 | Collection<Way> innerWays = findInnerWays(allWays, allNodes);
|
---|
206 |
|
---|
207 | // Join outer ways
|
---|
208 | Way outerWay = joinOuterWays(allWays, innerWays);
|
---|
209 |
|
---|
210 | // Fix Multipolygons if there are any
|
---|
211 | Collection<Way> newInnerWays = fixMultigons(innerWays, outerWay);
|
---|
212 |
|
---|
213 | // Delete the remaining inner ways
|
---|
214 | if(innerWays != null && innerWays.size() > 0) {
|
---|
215 | cmds.add(DeleteCommand.delete(Main.map.mapView.getEditLayer(), innerWays, true));
|
---|
216 | }
|
---|
217 | commitCommands(marktr("Delete Ways that are not part of an inner multipolygon"));
|
---|
218 |
|
---|
219 | // We can attach our new multipolygon relation and pretend it has always been there
|
---|
220 | addOwnMultigonRelation(newInnerWays, outerWay, relations);
|
---|
221 | fixRelations(relations, outerWay);
|
---|
222 | commitCommands(marktr("Fix relations"));
|
---|
223 |
|
---|
224 | stripTags(newInnerWays);
|
---|
225 | makeCommitsOneAction(
|
---|
226 | same
|
---|
227 | ? marktr("Joined self-overlapping area")
|
---|
228 | : marktr("Joined overlapping areas")
|
---|
229 | );
|
---|
230 |
|
---|
231 | if(warnAboutRelations) {
|
---|
232 | JOptionPane.showMessageDialog(Main.parent, tr("Some of the ways were part of relations that have been modified. Please verify no errors have been introduced."));
|
---|
233 | }
|
---|
234 |
|
---|
235 | return true;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Checks if tags of two given ways differ, and presents the user a dialog to solve conflicts
|
---|
240 | * @param Way First way to check
|
---|
241 | * @param Way Second Way to check
|
---|
242 | * @return boolean True if not all conflicts could be resolved, False if everything's fine
|
---|
243 | */
|
---|
244 | private boolean checkForTagConflicts(Way a, Way b) {
|
---|
245 | ArrayList<Way> ways = new ArrayList<Way>();
|
---|
246 | ways.add(a);
|
---|
247 | ways.add(b);
|
---|
248 |
|
---|
249 | // This is mostly copied and pasted from CombineWayAction.java and one day should be moved into tools
|
---|
250 | Map<String, Set<String>> props = new TreeMap<String, Set<String>>();
|
---|
251 | for (Way w : ways) {
|
---|
252 | for (Entry<String,String> e : w.entrySet()) {
|
---|
253 | if (!props.containsKey(e.getKey())) {
|
---|
254 | props.put(e.getKey(), new TreeSet<String>());
|
---|
255 | }
|
---|
256 | props.get(e.getKey()).add(e.getValue());
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | Way ax = new Way(a);
|
---|
261 | Way bx = new Way(b);
|
---|
262 |
|
---|
263 | Map<String, JComboBox> components = new HashMap<String, JComboBox>();
|
---|
264 | JPanel p = new JPanel(new GridBagLayout());
|
---|
265 | for (Entry<String, Set<String>> e : props.entrySet()) {
|
---|
266 | if (TigerUtils.isTigerTag(e.getKey())) {
|
---|
267 | String combined = TigerUtils.combineTags(e.getKey(), e.getValue());
|
---|
268 | ax.put(e.getKey(), combined);
|
---|
269 | bx.put(e.getKey(), combined);
|
---|
270 | } else if (e.getValue().size() > 1) {
|
---|
271 | if("created_by".equals(e.getKey()))
|
---|
272 | {
|
---|
273 | ax.put("created_by", "JOSM");
|
---|
274 | bx.put("created_by", "JOSM");
|
---|
275 | } else {
|
---|
276 | JComboBox c = new JComboBox(e.getValue().toArray());
|
---|
277 | c.setEditable(true);
|
---|
278 | p.add(new JLabel(e.getKey()), GBC.std());
|
---|
279 | p.add(Box.createHorizontalStrut(10), GBC.std());
|
---|
280 | p.add(c, GBC.eol());
|
---|
281 | components.put(e.getKey(), c);
|
---|
282 | }
|
---|
283 | } else {
|
---|
284 | String val = e.getValue().iterator().next();
|
---|
285 | ax.put(e.getKey(), val);
|
---|
286 | bx.put(e.getKey(), val);
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | if (components.isEmpty())
|
---|
291 | return false; // No conflicts found
|
---|
292 |
|
---|
293 | ExtendedDialog ed = new ExtendedDialog(Main.parent,
|
---|
294 | tr("Enter values for all conflicts."),
|
---|
295 | new String[] {tr("Solve Conflicts"), tr("Cancel")});
|
---|
296 | ed.setButtonIcons(new String[] {"dialogs/conflict.png", "cancel.png"});
|
---|
297 | ed.setContent(p);
|
---|
298 | ed.showDialog();
|
---|
299 |
|
---|
300 | if (ed.getValue() != 1) return true; // user cancel, unresolvable conflicts
|
---|
301 |
|
---|
302 | for (Entry<String, JComboBox> e : components.entrySet()) {
|
---|
303 | String val = e.getValue().getEditor().getItem().toString();
|
---|
304 | ax.put(e.getKey(), val);
|
---|
305 | bx.put(e.getKey(), val);
|
---|
306 | }
|
---|
307 |
|
---|
308 | cmds.add(new ChangeCommand(a, ax));
|
---|
309 | cmds.add(new ChangeCommand(b, bx));
|
---|
310 | commitCommands(marktr("Fix tag conflicts"));
|
---|
311 | return false;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Will find all intersection and add nodes there for two given ways
|
---|
316 | * @param Way First way
|
---|
317 | * @param Way Second way
|
---|
318 | * @return ArrayList<OsmPrimitive> List of new nodes
|
---|
319 | */
|
---|
320 | private ArrayList<OsmPrimitive> addIntersections(Way a, Way b) {
|
---|
321 | boolean same = a.equals(b);
|
---|
322 | int nodesSizeA = a.getNodesCount();
|
---|
323 | int nodesSizeB = b.getNodesCount();
|
---|
324 |
|
---|
325 | // We use OsmPrimitive here instead of Node because we later need to split a way at these nodes.
|
---|
326 | // With OsmPrimitve we can simply add the way and don't have to loop over the nodes
|
---|
327 | ArrayList<OsmPrimitive> nodes = new ArrayList<OsmPrimitive>();
|
---|
328 | ArrayList<NodeToSegs> nodesA = new ArrayList<NodeToSegs>();
|
---|
329 | ArrayList<NodeToSegs> nodesB = new ArrayList<NodeToSegs>();
|
---|
330 |
|
---|
331 | for (int i = (same ? 1 : 0); i < nodesSizeA - 1; i++) {
|
---|
332 | for (int j = (same ? i + 2 : 0); j < nodesSizeB - 1; j++) {
|
---|
333 | // Avoid re-adding nodes that already exist on (some) intersections
|
---|
334 | if(a.getNode(i).equals(b.getNode(j)) || a.getNode(i+1).equals(b.getNode(j))) {
|
---|
335 | nodes.add(b.getNode(j));
|
---|
336 | continue;
|
---|
337 | } else
|
---|
338 | if(a.getNode(i).equals(b.getNode(j+1)) || a.getNode(i+1).equals(b.getNode(j+1))) {
|
---|
339 | nodes.add(b.getNode(j+1));
|
---|
340 | continue;
|
---|
341 | }
|
---|
342 | LatLon intersection = getLineLineIntersection(
|
---|
343 | a.getNode(i) .getEastNorth().east(), a.getNode(i) .getEastNorth().north(),
|
---|
344 | a.getNode(i+1).getEastNorth().east(), a.getNode(i+1).getEastNorth().north(),
|
---|
345 | b.getNode(j) .getEastNorth().east(), b.getNode(j) .getEastNorth().north(),
|
---|
346 | b.getNode(j+1).getEastNorth().east(), b.getNode(j+1).getEastNorth().north());
|
---|
347 | if(intersection == null) {
|
---|
348 | continue;
|
---|
349 | }
|
---|
350 |
|
---|
351 | // Create the node. Adding them to the ways must be delayed because we still loop over them
|
---|
352 | Node n = new Node(intersection);
|
---|
353 | cmds.add(new AddCommand(n));
|
---|
354 | nodes.add(n);
|
---|
355 | // The distance is needed to sort and add the nodes in direction of the way
|
---|
356 | nodesA.add(new NodeToSegs(i, n, a.getNode(i).getCoor()));
|
---|
357 | if(same) {
|
---|
358 | nodesA.add(new NodeToSegs(j, n, a.getNode(j).getCoor()));
|
---|
359 | } else {
|
---|
360 | nodesB.add(new NodeToSegs(j, n, b.getNode(j).getCoor()));
|
---|
361 | }
|
---|
362 | }
|
---|
363 | }
|
---|
364 |
|
---|
365 | addNodesToWay(a, nodesA);
|
---|
366 | if(!same) {
|
---|
367 | addNodesToWay(b, nodesB);
|
---|
368 | }
|
---|
369 |
|
---|
370 | return nodes;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Finds the intersection of two lines
|
---|
375 | * @return LatLon null if no intersection was found, the LatLon coordinates of the intersection otherwise
|
---|
376 | */
|
---|
377 | static private LatLon getLineLineIntersection(
|
---|
378 | double x1, double y1, double x2, double y2,
|
---|
379 | double x3, double y3, double x4, double y4) {
|
---|
380 |
|
---|
381 | if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null;
|
---|
382 |
|
---|
383 | // Convert line from (point, point) form to ax+by=c
|
---|
384 | double a1 = y2 - y1;
|
---|
385 | double b1 = x1 - x2;
|
---|
386 | double c1 = x2*y1 - x1*y2;
|
---|
387 |
|
---|
388 | double a2 = y4 - y3;
|
---|
389 | double b2 = x3 - x4;
|
---|
390 | double c2 = x4*y3 - x3*y4;
|
---|
391 |
|
---|
392 | // Solve the equations
|
---|
393 | double det = a1*b2 - a2*b1;
|
---|
394 | if(det == 0) return null; // Lines are parallel
|
---|
395 |
|
---|
396 | return Main.proj.eastNorth2latlon(new EastNorth(
|
---|
397 | (b1*c2 - b2*c1)/det,
|
---|
398 | (a2*c1 -a1*c2)/det
|
---|
399 | ));
|
---|
400 | }
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Inserts given nodes with positions into the given ways
|
---|
404 | * @param Way The way to insert the nodes into
|
---|
405 | * @param Collection<NodeToSegs> The list of nodes with positions to insert
|
---|
406 | */
|
---|
407 | private void addNodesToWay(Way a, ArrayList<NodeToSegs> nodes) {
|
---|
408 | Way ax=new Way(a);
|
---|
409 | Collections.sort(nodes);
|
---|
410 |
|
---|
411 | int numOfAdds = 1;
|
---|
412 | for(NodeToSegs n : nodes) {
|
---|
413 | ax.addNode(n.pos + numOfAdds, n.n);
|
---|
414 | numOfAdds++;
|
---|
415 | }
|
---|
416 |
|
---|
417 | cmds.add(new ChangeCommand(a, ax));
|
---|
418 | }
|
---|
419 |
|
---|
420 | /**
|
---|
421 | * Commits the command list with a description
|
---|
422 | * @param String The description of what the commands do
|
---|
423 | */
|
---|
424 | private void commitCommands(String description) {
|
---|
425 | switch(cmds.size()) {
|
---|
426 | case 0:
|
---|
427 | return;
|
---|
428 | case 1:
|
---|
429 | Main.main.undoRedo.add(cmds.getFirst());
|
---|
430 | break;
|
---|
431 | default:
|
---|
432 | Command c = new SequenceCommand(tr(description), cmds);
|
---|
433 | Main.main.undoRedo.add(c);
|
---|
434 | break;
|
---|
435 | }
|
---|
436 |
|
---|
437 | cmds.clear();
|
---|
438 | cmdsCount++;
|
---|
439 | }
|
---|
440 |
|
---|
441 | /**
|
---|
442 | * Removes a given OsmPrimitive from all relations
|
---|
443 | * @param OsmPrimitive Element to remove from all relations
|
---|
444 | * @return ArrayList<RelationRole> List of relations with roles the primitives was part of
|
---|
445 | */
|
---|
446 | private ArrayList<RelationRole> removeFromRelations(OsmPrimitive osm) {
|
---|
447 | ArrayList<RelationRole> result = new ArrayList<RelationRole>();
|
---|
448 | for (Relation r : Main.main.getCurrentDataSet().getRelations()) {
|
---|
449 | if (r.isDeleted()) {
|
---|
450 | continue;
|
---|
451 | }
|
---|
452 | for (RelationMember rm : r.getMembers()) {
|
---|
453 | if (rm.getMember() != osm) {
|
---|
454 | continue;
|
---|
455 | }
|
---|
456 |
|
---|
457 | Relation newRel = new Relation(r);
|
---|
458 | List<RelationMember> members = newRel.getMembers();
|
---|
459 | members.remove(rm);
|
---|
460 | newRel.setMembers(members);
|
---|
461 |
|
---|
462 | cmds.add(new ChangeCommand(r, newRel));
|
---|
463 | RelationRole saverel = new RelationRole(r, rm.getRole());
|
---|
464 | if(!result.contains(saverel)) {
|
---|
465 | result.add(saverel);
|
---|
466 | }
|
---|
467 | break;
|
---|
468 | }
|
---|
469 | }
|
---|
470 |
|
---|
471 | commitCommands(marktr("Removed Element from Relations"));
|
---|
472 | return result;
|
---|
473 | }
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * This is a hacky implementation to make use of the splitWayAction code and
|
---|
477 | * should be improved. SplitWayAction needs to expose its splitWay function though.
|
---|
478 | */
|
---|
479 | private Collection<Way> splitWaysOnNodes(Way a, Way b, Collection<OsmPrimitive> nodes) {
|
---|
480 | ArrayList<Way> ways = new ArrayList<Way>();
|
---|
481 | ways.add(a);
|
---|
482 | if(!a.equals(b)) {
|
---|
483 | ways.add(b);
|
---|
484 | }
|
---|
485 |
|
---|
486 | List<OsmPrimitive> affected = new ArrayList<OsmPrimitive>();
|
---|
487 | for (Way way : ways) {
|
---|
488 | nodes.add(way);
|
---|
489 | Main.main.getCurrentDataSet().setSelected(nodes);
|
---|
490 | nodes.remove(way);
|
---|
491 | new SplitWayAction().actionPerformed(null);
|
---|
492 | cmdsCount++;
|
---|
493 | affected.addAll(Main.main.getCurrentDataSet().getSelectedWays());
|
---|
494 | }
|
---|
495 | return osmprim2way(affected);
|
---|
496 | }
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Converts a list of OsmPrimitives to a list of Ways
|
---|
500 | * @param Collection<OsmPrimitive> The OsmPrimitives list that's needed as a list of Ways
|
---|
501 | * @return Collection<Way> The list as list of Ways
|
---|
502 | */
|
---|
503 | static private Collection<Way> osmprim2way(Collection<OsmPrimitive> ways) {
|
---|
504 | Collection<Way> result = new ArrayList<Way>();
|
---|
505 | for(OsmPrimitive w: ways) {
|
---|
506 | if(w instanceof Way) {
|
---|
507 | result.add((Way) w);
|
---|
508 | }
|
---|
509 | }
|
---|
510 | return result;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /**
|
---|
514 | * Returns all nodes for given ways
|
---|
515 | * @param Collection<Way> The list of ways which nodes are to be returned
|
---|
516 | * @return Collection<Node> The list of nodes the ways contain
|
---|
517 | */
|
---|
518 | private Collection<Node> getNodesFromWays(Collection<Way> ways) {
|
---|
519 | Collection<Node> allNodes = new ArrayList<Node>();
|
---|
520 | for(Way w: ways) {
|
---|
521 | allNodes.addAll(w.getNodes());
|
---|
522 | }
|
---|
523 | return allNodes;
|
---|
524 | }
|
---|
525 |
|
---|
526 | /**
|
---|
527 | * Finds all inner ways for a given list of Ways and Nodes from a multigon by constructing a polygon
|
---|
528 | * for each way, looking for inner nodes that are not part of this way. If a node is found, all ways
|
---|
529 | * containing this node are added to the list
|
---|
530 | * @param Collection<Way> A list of (splitted) ways that form a multigon
|
---|
531 | * @param Collection<Node> A list of nodes that belong to the multigon
|
---|
532 | * @return Collection<Way> A list of ways that are positioned inside the outer borders of the multigon
|
---|
533 | */
|
---|
534 | private Collection<Way> findInnerWays(Collection<Way> multigonWays, Collection<Node> multigonNodes) {
|
---|
535 | Collection<Way> innerWays = new ArrayList<Way>();
|
---|
536 | for(Way w: multigonWays) {
|
---|
537 | Polygon poly = new Polygon();
|
---|
538 | for(Node n: (w).getNodes()) {
|
---|
539 | poly.addPoint(latlonToXY(n.getCoor().lat()), latlonToXY(n.getCoor().lon()));
|
---|
540 | }
|
---|
541 |
|
---|
542 | for(Node n: multigonNodes) {
|
---|
543 | if(!(w).containsNode(n) && poly.contains(latlonToXY(n.getCoor().lat()), latlonToXY(n.getCoor().lon()))) {
|
---|
544 | getWaysByNode(innerWays, multigonWays, n);
|
---|
545 | }
|
---|
546 | }
|
---|
547 | }
|
---|
548 |
|
---|
549 | return innerWays;
|
---|
550 | }
|
---|
551 |
|
---|
552 | // Polygon only supports int coordinates, so convert them
|
---|
553 | private int latlonToXY(double val) {
|
---|
554 | return (int)Math.round(val*1000000);
|
---|
555 | }
|
---|
556 |
|
---|
557 | /**
|
---|
558 | * Finds all ways that contain the given node.
|
---|
559 | * @param Collection<Way> A list to which matching ways will be added
|
---|
560 | * @param Collection<Way> A list of ways to check
|
---|
561 | * @param Node The node the ways should be checked against
|
---|
562 | */
|
---|
563 | private void getWaysByNode(Collection<Way> innerWays, Collection<Way> w, Node n) {
|
---|
564 | for(Way way : w) {
|
---|
565 | if(!(way).containsNode(n)) {
|
---|
566 | continue;
|
---|
567 | }
|
---|
568 | if(!innerWays.contains(way)) {
|
---|
569 | innerWays.add(way); // Will need this later for multigons
|
---|
570 | }
|
---|
571 | }
|
---|
572 | }
|
---|
573 |
|
---|
574 | /**
|
---|
575 | * Joins the two outer ways and deletes all short ways that can't be part of a multipolygon anyway
|
---|
576 | * @param Collection<OsmPrimitive> The list of all ways that belong to that multigon
|
---|
577 | * @param Collection<Way> The list of inner ways that belong to that multigon
|
---|
578 | * @return Way The newly created outer way
|
---|
579 | */
|
---|
580 | private Way joinOuterWays(Collection<Way> multigonWays, Collection<Way> innerWays) {
|
---|
581 | ArrayList<Way> join = new ArrayList<Way>();
|
---|
582 | for(Way w: multigonWays) {
|
---|
583 | // Skip inner ways
|
---|
584 | if(innerWays.contains(w)) {
|
---|
585 | continue;
|
---|
586 | }
|
---|
587 |
|
---|
588 | if(w.getNodesCount() <= 2) {
|
---|
589 | cmds.add(new DeleteCommand(w));
|
---|
590 | } else {
|
---|
591 | join.add(w);
|
---|
592 | }
|
---|
593 | }
|
---|
594 |
|
---|
595 | commitCommands(marktr("Join Areas: Remove Short Ways"));
|
---|
596 | return closeWay(joinWays(join));
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Ensures a way is closed. If it isn't, last and first node are connected.
|
---|
601 | * @param Way the way to ensure it's closed
|
---|
602 | * @return Way The joined way.
|
---|
603 | */
|
---|
604 | private Way closeWay(Way w) {
|
---|
605 | if(w.isClosed())
|
---|
606 | return w;
|
---|
607 | Main.main.getCurrentDataSet().setSelected(w);
|
---|
608 | Way wnew = new Way(w);
|
---|
609 | wnew.addNode(wnew.firstNode());
|
---|
610 | cmds.add(new ChangeCommand(w, wnew));
|
---|
611 | commitCommands(marktr("Closed Way"));
|
---|
612 | return (Way)(Main.main.getCurrentDataSet().getSelectedWays().toArray())[0];
|
---|
613 | }
|
---|
614 |
|
---|
615 | /**
|
---|
616 | * Joins a list of ways (using CombineWayAction and ReverseWayAction if necessary to quiet the former)
|
---|
617 | * @param ArrayList<Way> The list of ways to join
|
---|
618 | * @return Way The newly created way
|
---|
619 | */
|
---|
620 | private Way joinWays(ArrayList<Way> ways) {
|
---|
621 | if(ways.size() < 2) return ways.get(0);
|
---|
622 |
|
---|
623 | // This will turn ways so all of them point in the same direction and CombineAction won't bug
|
---|
624 | // the user about this.
|
---|
625 | Way a = null;
|
---|
626 | for(Way b : ways) {
|
---|
627 | if(a == null) {
|
---|
628 | a = b;
|
---|
629 | continue;
|
---|
630 | }
|
---|
631 | if(a.getNode(0).equals(b.getNode(0)) ||
|
---|
632 | a.getNode(a.getNodesCount()-1).equals(b.getNode(b.getNodesCount()-1))) {
|
---|
633 | Main.main.getCurrentDataSet().setSelected(b);
|
---|
634 | new ReverseWayAction().actionPerformed(null);
|
---|
635 | cmdsCount++;
|
---|
636 | }
|
---|
637 | a = b;
|
---|
638 | }
|
---|
639 | Main.main.getCurrentDataSet().setSelected(ways);
|
---|
640 | // TODO: It might be possible that a confirmation dialog is presented even after reversing (for
|
---|
641 | // "strange" ways). If the user cancels this, makeCommitsOneAction will wrongly consume a previous
|
---|
642 | // action. Make CombineWayAction either silent or expose its combining capabilities.
|
---|
643 | new CombineWayAction().actionPerformed(null);
|
---|
644 | cmdsCount++;
|
---|
645 | return (Way)(Main.main.getCurrentDataSet().getSelectedWays().toArray())[0];
|
---|
646 | }
|
---|
647 |
|
---|
648 | /**
|
---|
649 | * Finds all ways that may be part of a multipolygon relation and removes them from the given list.
|
---|
650 | * It will automatically combine "good" ways
|
---|
651 | * @param Collection<Way> The list of inner ways to check
|
---|
652 | * @param Way The newly created outer way
|
---|
653 | * @return ArrayList<Way> The List of newly created inner ways
|
---|
654 | */
|
---|
655 | private ArrayList<Way> fixMultigons(Collection<Way> uninterestingWays, Way outerWay) {
|
---|
656 | Collection<Node> innerNodes = getNodesFromWays(uninterestingWays);
|
---|
657 | Collection<Node> outerNodes = outerWay.getNodes();
|
---|
658 |
|
---|
659 | // The newly created inner ways. uninterestingWays is passed by reference and therefore modified in-place
|
---|
660 | ArrayList<Way> newInnerWays = new ArrayList<Way>();
|
---|
661 |
|
---|
662 | // Now we need to find all inner ways that contain a remaining node, but no outer nodes
|
---|
663 | // Remaining nodes are those that contain to more than one way. All nodes that belong to an
|
---|
664 | // inner multigon part will have at least two ways, so we can use this to find which ways do
|
---|
665 | // belong to the multigon.
|
---|
666 | ArrayList<Way> possibleWays = new ArrayList<Way>();
|
---|
667 | wayIterator: for(Way w : uninterestingWays) {
|
---|
668 | boolean hasInnerNodes = false;
|
---|
669 | for(Node n : w.getNodes()) {
|
---|
670 | if(outerNodes.contains(n)) {
|
---|
671 | continue wayIterator;
|
---|
672 | }
|
---|
673 | if(!hasInnerNodes && innerNodes.contains(n)) {
|
---|
674 | hasInnerNodes = true;
|
---|
675 | }
|
---|
676 | }
|
---|
677 | if(!hasInnerNodes || w.getNodesCount() < 2) {
|
---|
678 | continue;
|
---|
679 | }
|
---|
680 | possibleWays.add(w);
|
---|
681 | }
|
---|
682 |
|
---|
683 | // This removes unnecessary ways that might have been added.
|
---|
684 | removeAlmostAlikeWays(possibleWays);
|
---|
685 | removePartlyUnconnectedWays(possibleWays);
|
---|
686 |
|
---|
687 | // Join all ways that have one start/ending node in common
|
---|
688 | Way joined = null;
|
---|
689 | outerIterator: do {
|
---|
690 | joined = null;
|
---|
691 | for(Way w1 : possibleWays) {
|
---|
692 | if(w1.isClosed()) {
|
---|
693 | if(!wayIsCollapsed(w1)) {
|
---|
694 | uninterestingWays.remove(w1);
|
---|
695 | newInnerWays.add(w1);
|
---|
696 | }
|
---|
697 | joined = w1;
|
---|
698 | possibleWays.remove(w1);
|
---|
699 | continue outerIterator;
|
---|
700 | }
|
---|
701 | for(Way w2 : possibleWays) {
|
---|
702 | // w2 cannot be closed, otherwise it would have been removed above
|
---|
703 | if(!waysCanBeCombined(w1, w2)) {
|
---|
704 | continue;
|
---|
705 | }
|
---|
706 |
|
---|
707 | ArrayList<Way> joinThem = new ArrayList<Way>();
|
---|
708 | joinThem.add(w1);
|
---|
709 | joinThem.add(w2);
|
---|
710 | uninterestingWays.removeAll(joinThem);
|
---|
711 | possibleWays.removeAll(joinThem);
|
---|
712 |
|
---|
713 | // Although we joined the ways, we cannot simply assume that they are closed
|
---|
714 | joined = joinWays(joinThem);
|
---|
715 | uninterestingWays.add(joined);
|
---|
716 | possibleWays.add(joined);
|
---|
717 | continue outerIterator;
|
---|
718 | }
|
---|
719 | }
|
---|
720 | } while(joined != null);
|
---|
721 | return newInnerWays;
|
---|
722 | }
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Removes almost alike ways (= ways that are on top of each other for all nodes)
|
---|
726 | * @param ArrayList<Way> the ways to remove almost-duplicates from
|
---|
727 | */
|
---|
728 | private void removeAlmostAlikeWays(ArrayList<Way> ways) {
|
---|
729 | Collection<Way> removables = new ArrayList<Way>();
|
---|
730 | outer: for(int i=0; i < ways.size(); i++) {
|
---|
731 | Way a = ways.get(i);
|
---|
732 | for(int j=i+1; j < ways.size(); j++) {
|
---|
733 | Way b = ways.get(j);
|
---|
734 | List<Node> revNodes = new ArrayList<Node>(b.getNodes());
|
---|
735 | Collections.reverse(revNodes);
|
---|
736 | if(a.getNodes().equals(b.getNodes()) || a.getNodes().equals(revNodes)) {
|
---|
737 | removables.add(a);
|
---|
738 | continue outer;
|
---|
739 | }
|
---|
740 | }
|
---|
741 | }
|
---|
742 | ways.removeAll(removables);
|
---|
743 | }
|
---|
744 |
|
---|
745 | /**
|
---|
746 | * Removes ways from the given list whose starting or ending node doesn't
|
---|
747 | * connect to other ways from the same list (it's like removing spikes).
|
---|
748 | * @param ArrayList<Way> The list of ways to remove "spikes" from
|
---|
749 | */
|
---|
750 | private void removePartlyUnconnectedWays(ArrayList<Way> ways) {
|
---|
751 | List<Way> removables = new ArrayList<Way>();
|
---|
752 | for(Way a : ways) {
|
---|
753 | if(a.isClosed()) {
|
---|
754 | continue;
|
---|
755 | }
|
---|
756 | boolean connectedStart = false;
|
---|
757 | boolean connectedEnd = false;
|
---|
758 | for(Way b : ways) {
|
---|
759 | if(a.equals(b)) {
|
---|
760 | continue;
|
---|
761 | }
|
---|
762 | if(b.isFirstLastNode(a.firstNode())) {
|
---|
763 | connectedStart = true;
|
---|
764 | }
|
---|
765 | if(b.isFirstLastNode(a.lastNode())) {
|
---|
766 | connectedEnd = true;
|
---|
767 | }
|
---|
768 | }
|
---|
769 | if(!connectedStart || !connectedEnd) {
|
---|
770 | removables.add(a);
|
---|
771 | }
|
---|
772 | }
|
---|
773 | ways.removeAll(removables);
|
---|
774 | }
|
---|
775 |
|
---|
776 | /**
|
---|
777 | * Checks if a way is collapsed (i.e. looks like <---->)
|
---|
778 | * @param Way A *closed* way to check if it is collapsed
|
---|
779 | * @return boolean If the closed way is collapsed or not
|
---|
780 | */
|
---|
781 | private boolean wayIsCollapsed(Way w) {
|
---|
782 | if(w.getNodesCount() <= 3) return true;
|
---|
783 |
|
---|
784 | // If a way contains more than one node twice, it must be collapsed (only start/end node may be the same)
|
---|
785 | Way x = new Way(w);
|
---|
786 | int count = 0;
|
---|
787 | for(Node n : w.getNodes()) {
|
---|
788 | x.removeNode(n);
|
---|
789 | if(x.containsNode(n)) {
|
---|
790 | count++;
|
---|
791 | }
|
---|
792 | if(count == 2) return true;
|
---|
793 | }
|
---|
794 | return false;
|
---|
795 | }
|
---|
796 |
|
---|
797 | /**
|
---|
798 | * Checks if two ways share one starting/ending node
|
---|
799 | * @param Way first way
|
---|
800 | * @param Way second way
|
---|
801 | * @return boolean Wheter the ways share a starting/ending node or not
|
---|
802 | */
|
---|
803 | private boolean waysCanBeCombined(Way w1, Way w2) {
|
---|
804 | if(w1.equals(w2)) return false;
|
---|
805 |
|
---|
806 | if(w1.getNode(0).equals(w2.getNode(0))) return true;
|
---|
807 | if(w1.getNode(0).equals(w2.getNode(w2.getNodesCount()-1))) return true;
|
---|
808 |
|
---|
809 | if(w1.getNode(w1.getNodesCount()-1).equals(w2.getNode(0))) return true;
|
---|
810 | if(w1.getNode(w1.getNodesCount()-1).equals(w2.getNode(w2.getNodesCount()-1))) return true;
|
---|
811 |
|
---|
812 | return false;
|
---|
813 | }
|
---|
814 |
|
---|
815 | /**
|
---|
816 | * Will add own multipolygon relation to the "previously existing" relations. Fixup is done by fixRelations
|
---|
817 | * @param Collection<Way> List of already closed inner ways
|
---|
818 | * @param Way The outer way
|
---|
819 | * @param ArrayList<RelationRole> The list of relation with roles to add own relation to
|
---|
820 | */
|
---|
821 | private void addOwnMultigonRelation(Collection<Way> inner, Way outer, ArrayList<RelationRole> rels) {
|
---|
822 | if(inner.size() == 0) return;
|
---|
823 | // Create new multipolygon relation and add all inner ways to it
|
---|
824 | Relation newRel = new Relation();
|
---|
825 | newRel.put("type", "multipolygon");
|
---|
826 | for(Way w : inner) {
|
---|
827 | newRel.addMember(new RelationMember("inner", w));
|
---|
828 | }
|
---|
829 | cmds.add(new AddCommand(newRel));
|
---|
830 |
|
---|
831 | // We don't add outer to the relation because it will be handed to fixRelations()
|
---|
832 | // which will then do the remaining work. Collections are passed by reference, so no
|
---|
833 | // need to return it
|
---|
834 | rels.add(new RelationRole(newRel, "outer"));
|
---|
835 | //return rels;
|
---|
836 | }
|
---|
837 |
|
---|
838 | /**
|
---|
839 | * Adds the previously removed relations again to the outer way. If there are multiple multipolygon
|
---|
840 | * relations where the joined areas were in "outer" role a new relation is created instead with all
|
---|
841 | * members of both. This function depends on multigon relations to be valid already, it won't fix them.
|
---|
842 | * @param ArrayList<RelationRole> List of relations with roles the (original) ways were part of
|
---|
843 | * @param Way The newly created outer area/way
|
---|
844 | */
|
---|
845 | private void fixRelations(ArrayList<RelationRole> rels, Way outer) {
|
---|
846 | ArrayList<RelationRole> multiouters = new ArrayList<RelationRole>();
|
---|
847 | for(RelationRole r : rels) {
|
---|
848 | if( r.rel.get("type") != null &&
|
---|
849 | r.rel.get("type").equalsIgnoreCase("multipolygon") &&
|
---|
850 | r.role.equalsIgnoreCase("outer")
|
---|
851 | ) {
|
---|
852 | multiouters.add(r);
|
---|
853 | continue;
|
---|
854 | }
|
---|
855 | // Add it back!
|
---|
856 | Relation newRel = new Relation(r.rel);
|
---|
857 | newRel.addMember(new RelationMember(r.role, outer));
|
---|
858 | cmds.add(new ChangeCommand(r.rel, newRel));
|
---|
859 | }
|
---|
860 |
|
---|
861 | Relation newRel = null;
|
---|
862 | switch(multiouters.size()) {
|
---|
863 | case 0:
|
---|
864 | return;
|
---|
865 | case 1:
|
---|
866 | // Found only one to be part of a multipolygon relation, so just add it back as well
|
---|
867 | newRel = new Relation(multiouters.get(0).rel);
|
---|
868 | newRel.addMember(new RelationMember(multiouters.get(0).role, outer));
|
---|
869 | cmds.add(new ChangeCommand(multiouters.get(0).rel, newRel));
|
---|
870 | return;
|
---|
871 | default:
|
---|
872 | // Create a new relation with all previous members and (Way)outer as outer.
|
---|
873 | newRel = new Relation();
|
---|
874 | for(RelationRole r : multiouters) {
|
---|
875 | // Add members
|
---|
876 | for(RelationMember rm : r.rel.getMembers())
|
---|
877 | if(!newRel.getMembers().contains(rm)) {
|
---|
878 | newRel.addMember(rm);
|
---|
879 | }
|
---|
880 | // Add tags
|
---|
881 | for (String key : r.rel.keySet()) {
|
---|
882 | newRel.put(key, r.rel.get(key));
|
---|
883 | }
|
---|
884 | // Delete old relation
|
---|
885 | cmds.add(new DeleteCommand(r.rel));
|
---|
886 | }
|
---|
887 | newRel.addMember(new RelationMember("outer", outer));
|
---|
888 | cmds.add(new AddCommand(newRel));
|
---|
889 | }
|
---|
890 | }
|
---|
891 |
|
---|
892 | /**
|
---|
893 | * @param Collection<Way> The List of Ways to remove all tags from
|
---|
894 | */
|
---|
895 | private void stripTags(Collection<Way> ways) {
|
---|
896 | for(Way w: ways) {
|
---|
897 | stripTags(w);
|
---|
898 | }
|
---|
899 | commitCommands(marktr("Remove tags from inner ways"));
|
---|
900 | }
|
---|
901 |
|
---|
902 | /**
|
---|
903 | * @param Way The Way to remove all tags from
|
---|
904 | */
|
---|
905 | private void stripTags(Way x) {
|
---|
906 | if(x.getKeys() == null) return;
|
---|
907 | Way y = new Way(x);
|
---|
908 | for (String key : x.keySet()) {
|
---|
909 | y.remove(key);
|
---|
910 | }
|
---|
911 | cmds.add(new ChangeCommand(x, y));
|
---|
912 | }
|
---|
913 |
|
---|
914 | /**
|
---|
915 | * Takes the last cmdsCount actions back and combines them into a single action
|
---|
916 | * (for when the user wants to undo the join action)
|
---|
917 | * @param String The commit message to display
|
---|
918 | */
|
---|
919 | private void makeCommitsOneAction(String message) {
|
---|
920 | UndoRedoHandler ur = Main.main.undoRedo;
|
---|
921 | cmds.clear();
|
---|
922 | int i = Math.max(ur.commands.size() - cmdsCount, 0);
|
---|
923 | for(; i < ur.commands.size(); i++) {
|
---|
924 | cmds.add(ur.commands.get(i));
|
---|
925 | }
|
---|
926 |
|
---|
927 | for(i = 0; i < cmds.size(); i++) {
|
---|
928 | ur.undo();
|
---|
929 | }
|
---|
930 |
|
---|
931 | commitCommands(message == null ? marktr("Join Areas Function") : message);
|
---|
932 | cmdsCount = 0;
|
---|
933 | }
|
---|
934 |
|
---|
935 | @Override
|
---|
936 | protected void updateEnabledState() {
|
---|
937 | if (getCurrentDataSet() == null) {
|
---|
938 | setEnabled(false);
|
---|
939 | } else {
|
---|
940 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
941 | }
|
---|
942 | }
|
---|
943 |
|
---|
944 | @Override
|
---|
945 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
946 | setEnabled(selection != null && !selection.isEmpty());
|
---|
947 | }
|
---|
948 | }
|
---|