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