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