src/main/java/de/blau/android/util/Util.java
/**
     * Sort a list of ways in the order they are connected
     * 
     * Note: there is likely a far better algorithm than this, assumes that ways could be reversed. Further this will in
     * general not work for closed ways and assumes that the ways can actually be arranged as a single sorted sequence.
     * 
     * @param list List of ways
     * @return null if not connected or not all ways connected or the sorted list of ways
     */
Sort a list of ways in the order they are connected

there is likely a far better algorithm than this, assumes that ways could be reversed. Further this will in
general not work for closed ways and assumes that the ways can actually be arranged as a single sorted sequence.

@param list List of ways
@return null if not connected or not all ways connected or the sorted list of ways
Sort a list of ways in the order they are connected there is likely a far better algorithm than this assumes that ways could be reversed . Further this will in general not work for closed ways and assumes that the ways can actually be arranged as a single sorted sequence . @param list List of ways @return null if not connected or not all ways connected or the sorted list of ways
public static List<OsmElement> sortWays(@NonNull List<OsmElement> list) {
        List<OsmElement> result = new ArrayList<>();
        List<OsmElement> unconnected = new ArrayList<>(list);
        OsmElement e = unconnected.get(0);
        unconnected.remove(0);
        if (!e.getName().equals(Way.NAME)) {
            return null; 
        }
        result.add(e);
        while (true) {
            boolean found = false;
            for (OsmElement w : unconnected) {
                if (!Way.NAME.equals(w.getName())) {
                    return null; 
                }
                Node firstNode1 = ((Way) result.get(0)).getFirstNode();
                Node firstNode2 = ((Way) result.get(0)).getLastNode();
                Node lastNode1 = ((Way) result.get(result.size() - 1)).getFirstNode();
                Node lastNode2 = ((Way) result.get(result.size() - 1)).getLastNode();
                Node wFirstNode = ((Way) w).getFirstNode();
                Node wLastNode = ((Way) w).getLastNode();
                if (wFirstNode.equals(firstNode1) || wFirstNode.equals(firstNode2) || wLastNode.equals(firstNode1) || wLastNode.equals(firstNode2)) {
                    result.add(0, w);
                    unconnected.remove(w);
                    found = true;
                    break;
                } else if (wFirstNode.equals(lastNode1) || wFirstNode.equals(lastNode2) || wLastNode.equals(lastNode1) || wLastNode.equals(lastNode2)) {
                    result.add(w);
                    unconnected.remove(w);
                    found = true;
                    break;
                }
            }
            if (!found && !unconnected.isEmpty()) {
                return null;
            } else if (unconnected.isEmpty()) {
                return result;
            }
        }
    }
public static List < OsmElement > sortWays ( @ NonNull List < OsmElement > list ) { List < OsmElement > result = new ArrayList < > ( ) ; List < OsmElement > unconnected = new ArrayList < > ( list ) ; OsmElement e = unconnected . get ( 0 ) ; unconnected . remove ( 0 ) ; if ( ! e . getName ( ) . equals ( Way . NAME ) ) { return null ; } result . add ( e ) ; while ( true ) { boolean found = false ; for ( OsmElement w : unconnected ) { if ( ! Way . NAME . equals ( w . getName ( ) ) ) { return null ; } Node firstNode1 = ( ( Way ) result . get ( 0 ) ) . getFirstNode ( ) ; Node firstNode2 = ( ( Way ) result . get ( 0 ) ) . getLastNode ( ) ; Node lastNode1 = ( ( Way ) result . get ( result . size ( ) - 1 ) ) . getFirstNode ( ) ; Node lastNode2 = ( ( Way ) result . get ( result . size ( ) - 1 ) ) . getLastNode ( ) ; Node wFirstNode = ( ( Way ) w ) . getFirstNode ( ) ; Node wLastNode = ( ( Way ) w ) . getLastNode ( ) ; if ( wFirstNode . equals ( firstNode1 ) || wFirstNode . equals ( firstNode2 ) || wLastNode . equals ( firstNode1 ) || wLastNode . equals ( firstNode2 ) ) { result . add ( 0 , w ) ; unconnected . remove ( w ) ; found = true ; break ; } else if ( wFirstNode . equals ( lastNode1 ) || wFirstNode . equals ( lastNode2 ) || wLastNode . equals ( lastNode1 ) || wLastNode . equals ( lastNode2 ) ) { result . add ( w ) ; unconnected . remove ( w ) ; found = true ; break ; } } if ( ! found && ! unconnected . isEmpty ( ) ) { return null ; } else if ( unconnected . isEmpty ( ) ) { return result ; } } }
Sort a list of ways in the order they are connected
Sort a list of ways in the order they are connected
// not all are ways // not all are ways // this is a bit complicated because we don't want to reverse ways just yet