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