Ticket #4120: OrthogonalizeGroups.patch

File OrthogonalizeGroups.patch, 2.7 KB (added by mjulius, 16 years ago)
  • src/org/openstreetmap/josm/actions/OrthogonalizeAction.java

     
    1010import java.util.ArrayList;
    1111import java.util.Arrays;
    1212import java.util.Collection;
     13import java.util.Collections;
    1314import java.util.HashMap;
    1415import java.util.HashSet;
    1516import java.util.LinkedList;
     
    141142                }
    142143                else if (p instanceof Way) {
    143144                    wayDataList.add(new WayData((Way) p));
    144                 }
    145                 else {      // maybe a relation got selected...
     145                } else
    146146                    throw new InvalidUserInputException("Selection must consist only of ways and nodes.");
    147                 }
    148147            }
    149             if (wayDataList.isEmpty()) {
     148            if (wayDataList.isEmpty())
    150149                throw new InvalidUserInputException("usage");
    151             }
    152150            else  {
    153151                if (nodeList.size() == 2) {
    154152                    orthogonalize(wayDataList, nodeList);
    155153                }
    156154                else if (nodeList.isEmpty()) {
    157                     orthogonalize(wayDataList, nodeList);
    158                 }
    159                 else {
     155                    ArrayList<ArrayList<WayData>> groups = new ArrayList<ArrayList<WayData>>();
     156                    for (WayData w: wayDataList) {
     157                        boolean add = false;
     158                        for (ArrayList<WayData> g: groups) {
     159                            for (WayData groupedWay: g) {
     160                                if (!Collections.disjoint(w.way.getNodes(), groupedWay.way.getNodes())) {
     161                                    add = true;
     162                                    break;
     163                                }
     164                            }
     165                            if (add) {
     166                                g.add(w);
     167                            }
     168                        }
     169                        if (!add) {
     170                            ArrayList<WayData> newGroup = new ArrayList<WayData>();
     171                            newGroup.add(w);
     172                            groups.add(newGroup);
     173                        }
     174                    }
     175                    for (ArrayList<WayData> g: groups) {
     176                        orthogonalize(g, nodeList);
     177                    }
     178                } else
    160179                    throw new InvalidUserInputException("usage");
    161                 }
    162180            }
    163181        } catch (InvalidUserInputException ex) {
    164182            if (ex.getMessage().equals("usage")) {