Ignore:
Timestamp:
2009-12-04T18:34:31+01:00 (14 years ago)
Author:
stoecker
Message:

applied #4068 - patch by mjulius - fix combine way and way reversing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/CombineWayAction.java

    r2564 r2573  
    2828import org.openstreetmap.josm.command.DeleteCommand;
    2929import org.openstreetmap.josm.command.SequenceCommand;
     30import org.openstreetmap.josm.corrector.ReverseWayTagCorrector;
     31import org.openstreetmap.josm.corrector.UserCancelException;
    3032import org.openstreetmap.josm.data.osm.Node;
    3133import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    3739import org.openstreetmap.josm.tools.Pair;
    3840import org.openstreetmap.josm.tools.Shortcut;
     41
    3942/**
    4043 * Combines multiple ways into one.
     
    109112        ways = new HashSet<Way>(ways); // remove duplicates
    110113
    111         // build the collection of tags used by the ways to combine
    112         //
    113         TagCollection wayTags = TagCollection.unionOfAllPrimitives(ways);
    114 
    115114        // try to build a new way which includes all the combined
    116115        // ways
    117116        //
    118         NodeGraph graph = NodeGraph.createDirectedGraphFromWays(ways);
     117        NodeGraph graph = NodeGraph.createUndirectedGraphFromNodeWays(ways);
    119118        List<Node> path = graph.buildSpanningPath();
    120119        if (path == null) {
    121             graph = NodeGraph.createUndirectedGraphFromNodeWays(ways);
    122             path = graph.buildSpanningPath();
    123             if (path != null) {
    124                 if (!confirmChangeDirectionOfWays())
    125                     return;
     120            warnCombiningImpossible();
     121            return;
     122        }
     123        // check whether any ways have been reversed in the process
     124        // and build the collection of tags used by the ways to combine
     125        //
     126        TagCollection wayTags = TagCollection.unionOfAllPrimitives(ways);
     127
     128        List<Way> reversedWays = new LinkedList<Way>();
     129        List<Way> unreversedWays = new LinkedList<Way>();
     130        for (Way w: ways) {
     131            if ((path.indexOf(w.getNode(0)) + 1) == path.lastIndexOf(w.getNode(1))) {
     132                unreversedWays.add(w);
    126133            } else {
    127                 warnCombiningImpossible();
    128                 return;
    129             }
    130         }
     134                reversedWays.add(w);
     135            }
     136        }
     137        // reverse path if all ways have been reversed
     138        if (unreversedWays.isEmpty()) {
     139            Collections.reverse(path);
     140            unreversedWays = reversedWays;
     141            reversedWays = null;
     142        }
     143        if ((reversedWays != null) && !reversedWays.isEmpty()) {
     144            if (!confirmChangeDirectionOfWays()) return;
     145            // filter out ways that have no direction-dependent tags
     146            unreversedWays = ReverseWayTagCorrector.irreversibleWays(unreversedWays);
     147            reversedWays = ReverseWayTagCorrector.irreversibleWays(reversedWays);
     148            // reverse path if there are more reversed than unreversed ways with direction-dependent tags
     149            if (reversedWays.size() > unreversedWays.size()) {
     150                Collections.reverse(path);
     151                List<Way> tempWays = unreversedWays;
     152                unreversedWays = reversedWays;
     153                reversedWays = tempWays;
     154            }
     155            // if there are still reversed ways with direction-dependent tags, reverse their tags
     156            if (!reversedWays.isEmpty() && Main.pref.getBoolean("tag-correction.reverse-way", true)) {
     157                List<Way> unreversedTagWays = new ArrayList<Way>(ways);
     158                unreversedTagWays.removeAll(reversedWays);
     159                ReverseWayTagCorrector reverseWayTagCorrector = new ReverseWayTagCorrector();
     160                List<Way> reversedTagWays = new ArrayList<Way>();
     161                Collection<Command> changePropertyCommands =  null;
     162                for (Way w : reversedWays) {
     163                    Way wnew = new Way(w);
     164                    reversedTagWays.add(wnew);
     165                    try {
     166                        changePropertyCommands = reverseWayTagCorrector.execute(w, wnew);
     167                    }
     168                    catch(UserCancelException ex) {
     169                        return;
     170                    }
     171                }
     172                if ((changePropertyCommands != null) && !changePropertyCommands.isEmpty()) {
     173                    for (Command c : changePropertyCommands) {
     174                        c.executeCommand();
     175                    }
     176                }
     177                wayTags = TagCollection.unionOfAllPrimitives(reversedTagWays);
     178                wayTags.add(TagCollection.unionOfAllPrimitives(unreversedTagWays));
     179            }
     180        }
     181
    131182
    132183        // create the new way and apply the new node list
Note: See TracChangeset for help on using the changeset viewer.