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