[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[283] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[2220] | 4 | import static org.openstreetmap.josm.gui.conflict.tags.TagConflictResolutionUtil.combineTigerTags;
|
---|
| 5 | import static org.openstreetmap.josm.gui.conflict.tags.TagConflictResolutionUtil.completeTagCollectionForEditing;
|
---|
| 6 | import static org.openstreetmap.josm.gui.conflict.tags.TagConflictResolutionUtil.normalizeTagCollectionBeforeEditing;
|
---|
[2564] | 7 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[283] | 8 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 9 |
|
---|
| 10 | import java.awt.event.ActionEvent;
|
---|
| 11 | import java.awt.event.KeyEvent;
|
---|
[1951] | 12 | import java.util.ArrayList;
|
---|
[283] | 13 | import java.util.Collection;
|
---|
[2210] | 14 | import java.util.Collections;
|
---|
[283] | 15 | import java.util.HashMap;
|
---|
[582] | 16 | import java.util.HashSet;
|
---|
[283] | 17 | import java.util.LinkedList;
|
---|
[343] | 18 | import java.util.List;
|
---|
[283] | 19 | import java.util.Set;
|
---|
[2070] | 20 | import java.util.Stack;
|
---|
[283] | 21 |
|
---|
| 22 | import javax.swing.JOptionPane;
|
---|
[2070] | 23 | import javax.swing.SwingUtilities;
|
---|
[283] | 24 |
|
---|
| 25 | import org.openstreetmap.josm.Main;
|
---|
| 26 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
| 27 | import org.openstreetmap.josm.command.Command;
|
---|
| 28 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
| 29 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
[2573] | 30 | import org.openstreetmap.josm.corrector.ReverseWayTagCorrector;
|
---|
| 31 | import org.openstreetmap.josm.corrector.UserCancelException;
|
---|
[582] | 32 | import org.openstreetmap.josm.data.osm.Node;
|
---|
[283] | 33 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[406] | 34 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
[2070] | 35 | import org.openstreetmap.josm.data.osm.TagCollection;
|
---|
[283] | 36 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[3409] | 37 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
[1397] | 38 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
[2095] | 39 | import org.openstreetmap.josm.gui.conflict.tags.CombinePrimitiveResolverDialog;
|
---|
[429] | 40 | import org.openstreetmap.josm.tools.Pair;
|
---|
[1084] | 41 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[2573] | 42 |
|
---|
[283] | 43 | /**
|
---|
| 44 | * Combines multiple ways into one.
|
---|
[1023] | 45 | *
|
---|
[283] | 46 | */
|
---|
[1820] | 47 | public class CombineWayAction extends JosmAction {
|
---|
[283] | 48 |
|
---|
[3409] | 49 | private static final BooleanProperty PROP_REVERSE_WAY = new BooleanProperty("tag-correction.reverse-way", true);
|
---|
| 50 |
|
---|
[1169] | 51 | public CombineWayAction() {
|
---|
| 52 | super(tr("Combine Way"), "combineway", tr("Combine several ways into one."),
|
---|
[1814] | 53 | Shortcut.registerShortcut("tools:combineway", tr("Tool: {0}", tr("Combine Way")), KeyEvent.VK_C, Shortcut.GROUP_EDIT), true);
|
---|
[2323] | 54 | putValue("help", ht("/Action/CombineWay"));
|
---|
[1169] | 55 | }
|
---|
[283] | 56 |
|
---|
[2070] | 57 | protected boolean confirmChangeDirectionOfWays() {
|
---|
| 58 | ExtendedDialog ed = new ExtendedDialog(Main.parent,
|
---|
| 59 | tr("Change directions?"),
|
---|
| 60 | new String[] {tr("Reverse and Combine"), tr("Cancel")});
|
---|
| 61 | ed.setButtonIcons(new String[] {"wayflip.png", "cancel.png"});
|
---|
| 62 | ed.setContent(tr("The ways can not be combined in their current directions. "
|
---|
| 63 | + "Do you want to reverse some of them?"));
|
---|
| 64 | ed.showDialog();
|
---|
| 65 | return ed.getValue() == 1;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | protected void warnCombiningImpossible() {
|
---|
| 69 | String msg = tr("Could not combine ways "
|
---|
| 70 | + "(They could not be merged into a single string of nodes)");
|
---|
| 71 | JOptionPane.showMessageDialog(
|
---|
| 72 | Main.parent,
|
---|
[2202] | 73 | msg,
|
---|
[2070] | 74 | tr("Information"),
|
---|
| 75 | JOptionPane.INFORMATION_MESSAGE
|
---|
| 76 | );
|
---|
| 77 | return;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | protected Way getTargetWay(Collection<Way> combinedWays) {
|
---|
| 81 | // init with an arbitrary way
|
---|
| 82 | Way targetWay = combinedWays.iterator().next();
|
---|
| 83 |
|
---|
| 84 | // look for the first way already existing on
|
---|
| 85 | // the server
|
---|
| 86 | for (Way w : combinedWays) {
|
---|
| 87 | targetWay = w;
|
---|
[2273] | 88 | if (!w.isNew()) {
|
---|
[2070] | 89 | break;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | return targetWay;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[2564] | 95 | /**
|
---|
| 96 | * Replies the set of referring relations
|
---|
| 97 | *
|
---|
| 98 | * @return the set of referring relations
|
---|
| 99 | */
|
---|
| 100 | protected Set<Relation> getParentRelations(Collection<Way> ways) {
|
---|
| 101 | HashSet<Relation> ret = new HashSet<Relation>();
|
---|
| 102 | for (Way w: ways) {
|
---|
| 103 | ret.addAll(OsmPrimitive.getFilteredList(w.getReferrers(), Relation.class));
|
---|
| 104 | }
|
---|
| 105 | return ret;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[3230] | 108 | public Way combineWays(Collection<Way> ways) {
|
---|
[2070] | 109 |
|
---|
| 110 | // prepare and clean the list of ways to combine
|
---|
| 111 | //
|
---|
| 112 | if (ways == null || ways.isEmpty())
|
---|
[3230] | 113 | return null;
|
---|
[2070] | 114 | ways.remove(null); // just in case - remove all null ways from the collection
|
---|
| 115 | ways = new HashSet<Way>(ways); // remove duplicates
|
---|
| 116 |
|
---|
[2202] | 117 | // try to build a new way which includes all the combined
|
---|
| 118 | // ways
|
---|
[2070] | 119 | //
|
---|
[2573] | 120 | NodeGraph graph = NodeGraph.createUndirectedGraphFromNodeWays(ways);
|
---|
[2070] | 121 | List<Node> path = graph.buildSpanningPath();
|
---|
| 122 | if (path == null) {
|
---|
[2573] | 123 | warnCombiningImpossible();
|
---|
[3230] | 124 | return null;
|
---|
[2573] | 125 | }
|
---|
| 126 | // check whether any ways have been reversed in the process
|
---|
| 127 | // and build the collection of tags used by the ways to combine
|
---|
| 128 | //
|
---|
| 129 | TagCollection wayTags = TagCollection.unionOfAllPrimitives(ways);
|
---|
| 130 |
|
---|
| 131 | List<Way> reversedWays = new LinkedList<Way>();
|
---|
| 132 | List<Way> unreversedWays = new LinkedList<Way>();
|
---|
| 133 | for (Way w: ways) {
|
---|
| 134 | if ((path.indexOf(w.getNode(0)) + 1) == path.lastIndexOf(w.getNode(1))) {
|
---|
| 135 | unreversedWays.add(w);
|
---|
[2070] | 136 | } else {
|
---|
[2573] | 137 | reversedWays.add(w);
|
---|
[2070] | 138 | }
|
---|
| 139 | }
|
---|
[2573] | 140 | // reverse path if all ways have been reversed
|
---|
| 141 | if (unreversedWays.isEmpty()) {
|
---|
| 142 | Collections.reverse(path);
|
---|
| 143 | unreversedWays = reversedWays;
|
---|
| 144 | reversedWays = null;
|
---|
| 145 | }
|
---|
| 146 | if ((reversedWays != null) && !reversedWays.isEmpty()) {
|
---|
[3230] | 147 | if (!confirmChangeDirectionOfWays()) return null;
|
---|
[2573] | 148 | // filter out ways that have no direction-dependent tags
|
---|
| 149 | unreversedWays = ReverseWayTagCorrector.irreversibleWays(unreversedWays);
|
---|
| 150 | reversedWays = ReverseWayTagCorrector.irreversibleWays(reversedWays);
|
---|
| 151 | // reverse path if there are more reversed than unreversed ways with direction-dependent tags
|
---|
| 152 | if (reversedWays.size() > unreversedWays.size()) {
|
---|
| 153 | Collections.reverse(path);
|
---|
| 154 | List<Way> tempWays = unreversedWays;
|
---|
| 155 | unreversedWays = reversedWays;
|
---|
| 156 | reversedWays = tempWays;
|
---|
| 157 | }
|
---|
| 158 | // if there are still reversed ways with direction-dependent tags, reverse their tags
|
---|
[3409] | 159 | if (!reversedWays.isEmpty() && PROP_REVERSE_WAY.get()) {
|
---|
[2573] | 160 | List<Way> unreversedTagWays = new ArrayList<Way>(ways);
|
---|
| 161 | unreversedTagWays.removeAll(reversedWays);
|
---|
| 162 | ReverseWayTagCorrector reverseWayTagCorrector = new ReverseWayTagCorrector();
|
---|
| 163 | List<Way> reversedTagWays = new ArrayList<Way>();
|
---|
| 164 | Collection<Command> changePropertyCommands = null;
|
---|
| 165 | for (Way w : reversedWays) {
|
---|
| 166 | Way wnew = new Way(w);
|
---|
| 167 | reversedTagWays.add(wnew);
|
---|
| 168 | try {
|
---|
| 169 | changePropertyCommands = reverseWayTagCorrector.execute(w, wnew);
|
---|
| 170 | }
|
---|
| 171 | catch(UserCancelException ex) {
|
---|
[3230] | 172 | return null;
|
---|
[2573] | 173 | }
|
---|
| 174 | }
|
---|
| 175 | if ((changePropertyCommands != null) && !changePropertyCommands.isEmpty()) {
|
---|
| 176 | for (Command c : changePropertyCommands) {
|
---|
| 177 | c.executeCommand();
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | wayTags = TagCollection.unionOfAllPrimitives(reversedTagWays);
|
---|
| 181 | wayTags.add(TagCollection.unionOfAllPrimitives(unreversedTagWays));
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
[2070] | 184 |
|
---|
| 185 | // create the new way and apply the new node list
|
---|
| 186 | //
|
---|
| 187 | Way targetWay = getTargetWay(ways);
|
---|
| 188 | Way modifiedTargetWay = new Way(targetWay);
|
---|
| 189 | modifiedTargetWay.setNodes(path);
|
---|
| 190 |
|
---|
[2079] | 191 | TagCollection completeWayTags = new TagCollection(wayTags);
|
---|
[2202] | 192 | combineTigerTags(completeWayTags);
|
---|
[2220] | 193 | normalizeTagCollectionBeforeEditing(completeWayTags, ways);
|
---|
[2099] | 194 | TagCollection tagsToEdit = new TagCollection(completeWayTags);
|
---|
| 195 | completeTagCollectionForEditing(tagsToEdit);
|
---|
[2079] | 196 |
|
---|
[2095] | 197 | CombinePrimitiveResolverDialog dialog = CombinePrimitiveResolverDialog.getInstance();
|
---|
[2220] | 198 | dialog.getTagConflictResolverModel().populate(tagsToEdit, completeWayTags.getKeysWithMultipleValues());
|
---|
[2095] | 199 | dialog.setTargetPrimitive(targetWay);
|
---|
[2564] | 200 | Set<Relation> parentRelations = getParentRelations(ways);
|
---|
[2070] | 201 | dialog.getRelationMemberConflictResolverModel().populate(
|
---|
[2564] | 202 | parentRelations,
|
---|
| 203 | ways
|
---|
[2070] | 204 | );
|
---|
| 205 | dialog.prepareDefaultDecisions();
|
---|
| 206 |
|
---|
| 207 | // resolve tag conflicts if necessary
|
---|
| 208 | //
|
---|
[2564] | 209 | if (!completeWayTags.isApplicableToPrimitive() || !parentRelations.isEmpty()) {
|
---|
[2070] | 210 | dialog.setVisible(true);
|
---|
| 211 | if (dialog.isCancelled())
|
---|
[3230] | 212 | return null;
|
---|
[2070] | 213 | }
|
---|
| 214 |
|
---|
| 215 | LinkedList<Command> cmds = new LinkedList<Command>();
|
---|
| 216 | LinkedList<Way> deletedWays = new LinkedList<Way>(ways);
|
---|
| 217 | deletedWays.remove(targetWay);
|
---|
| 218 |
|
---|
| 219 | cmds.add(new ChangeCommand(targetWay, modifiedTargetWay));
|
---|
[2095] | 220 | cmds.addAll(dialog.buildResolutionCommands());
|
---|
[3170] | 221 | cmds.add(new DeleteCommand(deletedWays));
|
---|
[2070] | 222 | final SequenceCommand sequenceCommand = new SequenceCommand(tr("Combine {0} ways", ways.size()), cmds);
|
---|
[3230] | 223 | Main.main.undoRedo.add(sequenceCommand);
|
---|
[2070] | 224 |
|
---|
[3230] | 225 | return targetWay;
|
---|
[2070] | 226 | }
|
---|
| 227 |
|
---|
[1169] | 228 | public void actionPerformed(ActionEvent event) {
|
---|
[1817] | 229 | if (getCurrentDataSet() == null)
|
---|
| 230 | return;
|
---|
[1814] | 231 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
|
---|
[2070] | 232 | Set<Way> selectedWays = OsmPrimitive.getFilteredSet(selection, Way.class);
|
---|
[1169] | 233 | if (selectedWays.size() < 2) {
|
---|
[2017] | 234 | JOptionPane.showMessageDialog(
|
---|
[1847] | 235 | Main.parent,
|
---|
| 236 | tr("Please select at least two ways to combine."),
|
---|
| 237 | tr("Information"),
|
---|
| 238 | JOptionPane.INFORMATION_MESSAGE
|
---|
| 239 | );
|
---|
[1169] | 240 | return;
|
---|
| 241 | }
|
---|
[3230] | 242 | // combine and update gui
|
---|
| 243 | final Way selectedWay = combineWays(selectedWays);
|
---|
| 244 | if(selectedWay != null)
|
---|
| 245 | {
|
---|
| 246 | Runnable guiTask = new Runnable() {
|
---|
| 247 | public void run() {
|
---|
| 248 | getCurrentDataSet().setSelected(selectedWay);
|
---|
| 249 | }
|
---|
| 250 | };
|
---|
| 251 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
| 252 | guiTask.run();
|
---|
| 253 | } else {
|
---|
| 254 | SwingUtilities.invokeLater(guiTask);
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
[2070] | 257 | }
|
---|
[426] | 258 |
|
---|
[2070] | 259 | @Override
|
---|
| 260 | protected void updateEnabledState() {
|
---|
| 261 | if (getCurrentDataSet() == null) {
|
---|
| 262 | setEnabled(false);
|
---|
| 263 | return;
|
---|
| 264 | }
|
---|
| 265 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
|
---|
[2256] | 266 | updateEnabledState(selection);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | @Override
|
---|
| 270 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
[2070] | 271 | int numWays = 0;
|
---|
| 272 | for (OsmPrimitive osm : selection)
|
---|
| 273 | if (osm instanceof Way) {
|
---|
| 274 | numWays++;
|
---|
| 275 | }
|
---|
| 276 | setEnabled(numWays >= 2);
|
---|
| 277 | }
|
---|
[426] | 278 |
|
---|
[2070] | 279 | static public class NodePair {
|
---|
| 280 | private Node a;
|
---|
| 281 | private Node b;
|
---|
| 282 | public NodePair(Node a, Node b) {
|
---|
| 283 | this.a =a;
|
---|
| 284 | this.b = b;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | public NodePair(Pair<Node,Node> pair) {
|
---|
| 288 | this.a = pair.a;
|
---|
| 289 | this.b = pair.b;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | public NodePair(NodePair other) {
|
---|
| 293 | this.a = other.a;
|
---|
| 294 | this.b = other.b;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | public Node getA() {
|
---|
| 298 | return a;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | public Node getB() {
|
---|
| 302 | return b;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | public boolean isAdjacentToA(NodePair other) {
|
---|
| 306 | return other.getA() == a || other.getB() == a;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | public boolean isAdjacentToB(NodePair other) {
|
---|
| 310 | return other.getA() == b || other.getB() == b;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | public boolean isSuccessorOf(NodePair other) {
|
---|
| 314 | return other.getB() == a;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | public boolean isPredecessorOf(NodePair other) {
|
---|
| 318 | return b == other.getA();
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | public NodePair swap() {
|
---|
| 322 | return new NodePair(b,a);
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | @Override
|
---|
| 326 | public String toString() {
|
---|
| 327 | return new StringBuilder()
|
---|
| 328 | .append("[")
|
---|
| 329 | .append(a.getId())
|
---|
| 330 | .append(",")
|
---|
| 331 | .append(b.getId())
|
---|
| 332 | .append("]")
|
---|
| 333 | .toString();
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | public boolean contains(Node n) {
|
---|
| 337 | return a == n || b == n;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | @Override
|
---|
| 341 | public int hashCode() {
|
---|
| 342 | final int prime = 31;
|
---|
| 343 | int result = 1;
|
---|
| 344 | result = prime * result + ((a == null) ? 0 : a.hashCode());
|
---|
| 345 | result = prime * result + ((b == null) ? 0 : b.hashCode());
|
---|
| 346 | return result;
|
---|
| 347 | }
|
---|
| 348 | @Override
|
---|
| 349 | public boolean equals(Object obj) {
|
---|
| 350 | if (this == obj)
|
---|
| 351 | return true;
|
---|
| 352 | if (obj == null)
|
---|
| 353 | return false;
|
---|
| 354 | if (getClass() != obj.getClass())
|
---|
| 355 | return false;
|
---|
| 356 | NodePair other = (NodePair) obj;
|
---|
| 357 | if (a == null) {
|
---|
| 358 | if (other.a != null)
|
---|
| 359 | return false;
|
---|
| 360 | } else if (!a.equals(other.a))
|
---|
| 361 | return false;
|
---|
| 362 | if (b == null) {
|
---|
| 363 | if (other.b != null)
|
---|
| 364 | return false;
|
---|
| 365 | } else if (!b.equals(other.b))
|
---|
| 366 | return false;
|
---|
| 367 | return true;
|
---|
| 368 | }
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | static public class NodeGraph {
|
---|
| 372 | static public List<NodePair> buildNodePairs(Way way, boolean directed) {
|
---|
| 373 | ArrayList<NodePair> pairs = new ArrayList<NodePair>();
|
---|
| 374 | for (Pair<Node,Node> pair: way.getNodePairs(false /* don't sort */)) {
|
---|
| 375 | pairs.add(new NodePair(pair));
|
---|
| 376 | if (!directed) {
|
---|
| 377 | pairs.add(new NodePair(pair).swap());
|
---|
[1814] | 378 | }
|
---|
[1169] | 379 | }
|
---|
[2070] | 380 | return pairs;
|
---|
[1169] | 381 | }
|
---|
[343] | 382 |
|
---|
[2070] | 383 | static public List<NodePair> buildNodePairs(List<Way> ways, boolean directed) {
|
---|
| 384 | ArrayList<NodePair> pairs = new ArrayList<NodePair>();
|
---|
| 385 | for (Way w: ways) {
|
---|
| 386 | pairs.addAll(buildNodePairs(w, directed));
|
---|
| 387 | }
|
---|
| 388 | return pairs;
|
---|
| 389 | }
|
---|
[2032] | 390 |
|
---|
[2070] | 391 | static public List<NodePair> eliminateDuplicateNodePairs(List<NodePair> pairs) {
|
---|
| 392 | ArrayList<NodePair> cleaned = new ArrayList<NodePair>();
|
---|
| 393 | for(NodePair p: pairs) {
|
---|
| 394 | if (!cleaned.contains(p) && !cleaned.contains(p.swap())) {
|
---|
| 395 | cleaned.add(p);
|
---|
| 396 | }
|
---|
[1169] | 397 | }
|
---|
[2070] | 398 | return cleaned;
|
---|
[1169] | 399 | }
|
---|
[343] | 400 |
|
---|
[2070] | 401 | static public NodeGraph createDirectedGraphFromNodePairs(List<NodePair> pairs) {
|
---|
| 402 | NodeGraph graph = new NodeGraph();
|
---|
| 403 | for (NodePair pair: pairs) {
|
---|
| 404 | graph.add(pair);
|
---|
| 405 | }
|
---|
| 406 | return graph;
|
---|
| 407 | }
|
---|
[699] | 408 |
|
---|
[2070] | 409 | static public NodeGraph createDirectedGraphFromWays(Collection<Way> ways) {
|
---|
| 410 | NodeGraph graph = new NodeGraph();
|
---|
| 411 | for (Way w: ways) {
|
---|
| 412 | graph.add(buildNodePairs(w, true /* directed */));
|
---|
[1814] | 413 | }
|
---|
[2070] | 414 | return graph;
|
---|
[1169] | 415 | }
|
---|
[699] | 416 |
|
---|
[2070] | 417 | static public NodeGraph createUndirectedGraphFromNodeList(List<NodePair> pairs) {
|
---|
| 418 | NodeGraph graph = new NodeGraph();
|
---|
| 419 | for (NodePair pair: pairs) {
|
---|
| 420 | graph.add(pair);
|
---|
| 421 | graph.add(pair.swap());
|
---|
| 422 | }
|
---|
| 423 | return graph;
|
---|
| 424 | }
|
---|
[426] | 425 |
|
---|
[2070] | 426 | static public NodeGraph createUndirectedGraphFromNodeWays(Collection<Way> ways) {
|
---|
| 427 | NodeGraph graph = new NodeGraph();
|
---|
| 428 | for (Way w: ways) {
|
---|
| 429 | graph.add(buildNodePairs(w, false /* undirected */));
|
---|
[1530] | 430 | }
|
---|
[2070] | 431 | return graph;
|
---|
[1169] | 432 | }
|
---|
[426] | 433 |
|
---|
[2070] | 434 | private Set<NodePair> edges;
|
---|
| 435 | private int numUndirectedEges = 0;
|
---|
[2210] | 436 | private HashMap<Node, List<NodePair>> successors;
|
---|
| 437 | private HashMap<Node, List<NodePair>> predecessors;
|
---|
[2032] | 438 |
|
---|
[2210] | 439 | protected void rememberSuccessor(NodePair pair) {
|
---|
| 440 | if (successors.containsKey(pair.getA())) {
|
---|
| 441 | if (!successors.get(pair.getA()).contains(pair)) {
|
---|
| 442 | successors.get(pair.getA()).add(pair);
|
---|
| 443 | }
|
---|
| 444 | } else {
|
---|
| 445 | ArrayList<NodePair> l = new ArrayList<NodePair>();
|
---|
| 446 | l.add(pair);
|
---|
| 447 | successors.put(pair.getA(), l);
|
---|
| 448 | }
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | protected void rememberPredecessors(NodePair pair) {
|
---|
| 452 | if (predecessors.containsKey(pair.getB())) {
|
---|
| 453 | if (!predecessors.get(pair.getB()).contains(pair)) {
|
---|
| 454 | predecessors.get(pair.getB()).add(pair);
|
---|
| 455 | }
|
---|
| 456 | } else {
|
---|
| 457 | ArrayList<NodePair> l = new ArrayList<NodePair>();
|
---|
| 458 | l.add(pair);
|
---|
| 459 | predecessors.put(pair.getB(), l);
|
---|
| 460 | }
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | protected boolean isTerminalNode(Node n) {
|
---|
| 464 | if (successors.get(n) == null) return false;
|
---|
| 465 | if (successors.get(n).size() != 1) return false;
|
---|
| 466 | if (predecessors.get(n) == null) return true;
|
---|
| 467 | if (predecessors.get(n).size() == 1) {
|
---|
| 468 | NodePair p1 = successors.get(n).iterator().next();
|
---|
| 469 | NodePair p2 = predecessors.get(n).iterator().next();
|
---|
| 470 | return p1.equals(p2.swap());
|
---|
| 471 | }
|
---|
| 472 | return false;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | protected void prepare() {
|
---|
[2070] | 476 | Set<NodePair> undirectedEdges = new HashSet<NodePair>();
|
---|
[2210] | 477 | successors = new HashMap<Node, List<NodePair>>();
|
---|
| 478 | predecessors = new HashMap<Node, List<NodePair>>();
|
---|
| 479 |
|
---|
[2070] | 480 | for (NodePair pair: edges) {
|
---|
| 481 | if (!undirectedEdges.contains(pair) && ! undirectedEdges.contains(pair.swap())) {
|
---|
| 482 | undirectedEdges.add(pair);
|
---|
| 483 | }
|
---|
[2210] | 484 | rememberSuccessor(pair);
|
---|
| 485 | rememberPredecessors(pair);
|
---|
[2070] | 486 | }
|
---|
| 487 | numUndirectedEges = undirectedEdges.size();
|
---|
| 488 | }
|
---|
[1621] | 489 |
|
---|
[2070] | 490 | public NodeGraph() {
|
---|
| 491 | edges = new HashSet<NodePair>();
|
---|
| 492 | }
|
---|
[2032] | 493 |
|
---|
[2070] | 494 | public void add(NodePair pair) {
|
---|
| 495 | if (!edges.contains(pair)) {
|
---|
| 496 | edges.add(pair);
|
---|
[1814] | 497 | }
|
---|
[1169] | 498 | }
|
---|
[283] | 499 |
|
---|
[2070] | 500 | public void add(List<NodePair> pairs) {
|
---|
| 501 | for (NodePair pair: pairs) {
|
---|
| 502 | add(pair);
|
---|
| 503 | }
|
---|
| 504 | }
|
---|
[426] | 505 |
|
---|
[2070] | 506 | protected Node getStartNode() {
|
---|
[2210] | 507 | Set<Node> nodes = getNodes();
|
---|
| 508 | for (Node n: nodes) {
|
---|
| 509 | if (successors.get(n) != null && successors.get(n).size() ==1)
|
---|
| 510 | return n;
|
---|
| 511 | }
|
---|
| 512 | return null;
|
---|
[2070] | 513 | }
|
---|
| 514 |
|
---|
[2210] | 515 | protected Set<Node> getTerminalNodes() {
|
---|
| 516 | Set<Node> ret = new HashSet<Node>();
|
---|
| 517 | for (Node n: getNodes()) {
|
---|
| 518 | if (isTerminalNode(n)) {
|
---|
| 519 | ret.add(n);
|
---|
| 520 | }
|
---|
| 521 | }
|
---|
| 522 | return ret;
|
---|
| 523 | }
|
---|
| 524 |
|
---|
[2070] | 525 | protected Set<Node> getNodes(Stack<NodePair> pairs) {
|
---|
| 526 | HashSet<Node> nodes = new HashSet<Node>();
|
---|
| 527 | for (NodePair pair: pairs) {
|
---|
| 528 | nodes.add(pair.getA());
|
---|
| 529 | nodes.add(pair.getB());
|
---|
| 530 | }
|
---|
| 531 | return nodes;
|
---|
| 532 | }
|
---|
| 533 |
|
---|
| 534 | protected List<NodePair> getOutboundPairs(NodePair pair) {
|
---|
[2210] | 535 | return getOutboundPairs(pair.getB());
|
---|
[2070] | 536 | }
|
---|
| 537 |
|
---|
| 538 | protected List<NodePair> getOutboundPairs(Node node) {
|
---|
[2210] | 539 | List<NodePair> l = successors.get(node);
|
---|
[2356] | 540 | if (l == null)
|
---|
| 541 | return Collections.emptyList();
|
---|
| 542 | return l;
|
---|
[1169] | 543 | }
|
---|
[283] | 544 |
|
---|
[2070] | 545 | protected Set<Node> getNodes() {
|
---|
| 546 | Set<Node> nodes = new HashSet<Node>();
|
---|
| 547 | for (NodePair pair: edges) {
|
---|
| 548 | nodes.add(pair.getA());
|
---|
| 549 | nodes.add(pair.getB());
|
---|
| 550 | }
|
---|
| 551 | return nodes;
|
---|
| 552 | }
|
---|
[426] | 553 |
|
---|
[2070] | 554 | protected boolean isSpanningWay(Stack<NodePair> way) {
|
---|
| 555 | return numUndirectedEges == way.size();
|
---|
[1814] | 556 | }
|
---|
[343] | 557 |
|
---|
[2070] | 558 | protected List<Node> buildPathFromNodePairs(Stack<NodePair> path) {
|
---|
| 559 | LinkedList<Node> ret = new LinkedList<Node>();
|
---|
| 560 | for (NodePair pair: path) {
|
---|
| 561 | ret.add(pair.getA());
|
---|
[1169] | 562 | }
|
---|
[2070] | 563 | ret.add(path.peek().getB());
|
---|
| 564 | return ret;
|
---|
[1169] | 565 | }
|
---|
[343] | 566 |
|
---|
[2210] | 567 | /**
|
---|
| 568 | * Tries to find a spanning path starting from node <code>startNode</code>.
|
---|
[2512] | 569 | *
|
---|
[2210] | 570 | * Traverses the path in depth-first order.
|
---|
[2512] | 571 | *
|
---|
[2210] | 572 | * @param startNode the start node
|
---|
| 573 | * @return the spanning path; null, if no path is found
|
---|
| 574 | */
|
---|
[2070] | 575 | protected List<Node> buildSpanningPath(Node startNode) {
|
---|
| 576 | if (startNode == null)
|
---|
| 577 | return null;
|
---|
| 578 | Stack<NodePair> path = new Stack<NodePair>();
|
---|
| 579 | Stack<NodePair> nextPairs = new Stack<NodePair>();
|
---|
| 580 | nextPairs.addAll(getOutboundPairs(startNode));
|
---|
| 581 | while(!nextPairs.isEmpty()) {
|
---|
[2210] | 582 | NodePair cur= nextPairs.pop();
|
---|
| 583 | if (! path.contains(cur) && ! path.contains(cur.swap())) {
|
---|
| 584 | while(!path.isEmpty() && !path.peek().isPredecessorOf(cur)) {
|
---|
| 585 | path.pop();
|
---|
| 586 | }
|
---|
| 587 | path.push(cur);
|
---|
| 588 | if (isSpanningWay(path)) return buildPathFromNodePairs(path);
|
---|
| 589 | nextPairs.addAll(getOutboundPairs(path.peek()));
|
---|
| 590 | }
|
---|
[2070] | 591 | }
|
---|
| 592 | return null;
|
---|
[1817] | 593 | }
|
---|
| 594 |
|
---|
[2210] | 595 | /**
|
---|
| 596 | * Tries to find a path through the graph which visits each edge (i.e.
|
---|
| 597 | * the segment of a way) exactly one.
|
---|
[2512] | 598 | *
|
---|
[2210] | 599 | * @return the path; null, if no path was found
|
---|
| 600 | */
|
---|
[2070] | 601 | public List<Node> buildSpanningPath() {
|
---|
[2210] | 602 | prepare();
|
---|
| 603 | // try to find a path from each "terminal node", i.e. from a
|
---|
| 604 | // node which is connected by exactly one undirected edges (or
|
---|
| 605 | // two directed edges in opposite direction) to the graph. A
|
---|
| 606 | // graph built up from way segments is likely to include such
|
---|
| 607 | // nodes, unless all ways are closed.
|
---|
| 608 | // In the worst case this loops over all nodes which is
|
---|
| 609 | // very slow for large ways.
|
---|
| 610 | //
|
---|
| 611 | Set<Node> nodes = getTerminalNodes();
|
---|
| 612 | nodes = nodes.isEmpty() ? getNodes() : nodes;
|
---|
| 613 | for (Node n: nodes) {
|
---|
[2070] | 614 | List<Node> path = buildSpanningPath(n);
|
---|
| 615 | if (path != null)
|
---|
| 616 | return path;
|
---|
[1169] | 617 | }
|
---|
[2070] | 618 | return null;
|
---|
| 619 | }
|
---|
[1169] | 620 | }
|
---|
[283] | 621 | }
|
---|