Changeset 15732 in josm


Ignore:
Timestamp:
2020-01-19T18:57:26+01:00 (5 years ago)
Author:
simon04
Message:

see #18503 - Relation editor: compute wayConnection for type=superroute

This basic implementation only considers route segments without a role
(as specified for public_transport:version=2; no forward/backward)
and does not take roundabouts into account.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/sort/WayConnectionType.java

    r15696 r15732  
    8080     */
    8181    public WayConnectionType(boolean invalid) {
     82        this.linkPrev = false;
     83        this.linkNext = false;
     84        this.isLoop = false;
     85        this.direction = NONE;
    8286        this.invalid = invalid;
    8387    }
     
    8589    /** construct invalid instance */
    8690    public WayConnectionType() {
    87         this.linkPrev = false;
    88         this.linkNext = false;
    89         this.isLoop = false;
    90         this.direction = NONE;
    91         invalid = true;
     91        this(true);
    9292    }
    9393
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/sort/WayConnectionTypeCalculator.java

    r15700 r15732  
    88import java.util.ArrayList;
    99import java.util.List;
     10import java.util.stream.Collectors;
    1011
    1112import org.openstreetmap.josm.data.osm.Node;
     
    1415import org.openstreetmap.josm.data.osm.Way;
    1516import org.openstreetmap.josm.gui.dialogs.relation.sort.WayConnectionType.Direction;
     17import org.openstreetmap.josm.tools.StreamUtils;
    1618import org.openstreetmap.josm.tools.bugreport.BugReport;
    1719
     
    4345    public List<WayConnectionType> updateLinks(Relation r, List<RelationMember> members) {
    4446        this.members = members;
    45         final List<WayConnectionType> con = new ArrayList<>();
    46 
    47         for (int i = 0; i < members.size(); ++i) {
    48             con.add(null);
    49         }
     47        final List<WayConnectionType> con = members.stream()
     48                .map(ignore -> (WayConnectionType) null)
     49                .collect(Collectors.toList());
    5050
    5151        firstGroupIdx = 0;
     
    6565            }
    6666        }
    67         makeLoopIfNeeded(con, members.size()-1);
     67        if (!isSuperRoute(r)) {
     68            makeLoopIfNeeded(con, members.size() - 1);
     69        }
    6870
    6971        return con;
     
    7274    private WayConnectionType updateLinksFor(Relation r, List<WayConnectionType> con, WayConnectionType lastWct, int i) {
    7375        final RelationMember m = members.get(i);
    74         if (isNoHandleableWay(m)) {
     76        if (isSuperRoute(r)) {
     77            final WayConnectionType wct;
     78            if (!members.get(i).isRelation()) {
     79                return new WayConnectionType(true);
     80            } else if (i == 0) {
     81                return new WayConnectionType(false);
     82            } else {
     83                final List<RelationMember> previousMembers = members.get(i - 1).getRelation().getMembers();
     84                final Way previousLastWay = StreamUtils.reversedStream(previousMembers)
     85                        .filter(x -> x.isWay() && !x.hasRole())
     86                        .map(RelationMember::getWay)
     87                        .findFirst().orElse(null);
     88                final Way currentFirstWay = m.getRelation().getMembers().stream()
     89                        .filter(x -> x.isWay() && !x.hasRole())
     90                        .map(RelationMember::getWay)
     91                        .findFirst().orElse(null);
     92                final boolean isConnected = isConnected(previousLastWay, currentFirstWay);
     93                wct = new WayConnectionType(false);
     94                lastWct.linkNext = wct.linkPrev = isConnected;
     95            }
     96            con.set(i, wct);
     97            return wct;
     98        } else if (isNoHandleableWay(m)) {
    7599            if (i > 0) {
    76100                makeLoopIfNeeded(con, i-1);
     
    139163        con.set(i, wct);
    140164        return wct;
     165    }
     166
     167    private boolean isSuperRoute(Relation r) {
     168        return r != null && r.hasTag("type", "superroute");
    141169    }
    142170
     
    372400        members = null;
    373401    }
     402
     403    private boolean isConnected(Way way1, Way way2) {
     404        return way1 != null && way2 != null && way1.isUsable() && way2.isUsable()
     405                && (way1.isFirstLastNode(way2.firstNode()) || way1.isFirstLastNode(way2.lastNode()));
     406    }
    374407}
  • trunk/src/org/openstreetmap/josm/tools/StreamUtils.java

    r11921 r15732  
    22package org.openstreetmap.josm.tools;
    33
     4import java.util.List;
     5import java.util.Objects;
    46import java.util.StringJoiner;
    57import java.util.stream.Collector;
     8import java.util.stream.IntStream;
    69import java.util.stream.Stream;
    710import java.util.stream.StreamSupport;
     
    3235
    3336    /**
     37     * Creqates a stream iterating the list in reversed order
     38     * @param list the list to iterate over
     39     * @param <T> the type of elements in the list
     40     * @return a stream iterating the list in reversed order
     41     * @since 15732
     42     */
     43    public static <T> Stream<T> reversedStream(List<T> list) {
     44        Objects.requireNonNull(list, "list");
     45        final int size = list.size();
     46        return IntStream.range(0, size).mapToObj(i -> list.get(size - i - 1));
     47    }
     48
     49    /**
    3450     * Creates a new Collector that collects the items and returns them as HTML unordered list.
    3551     * @return The collector.
  • trunk/test/unit/org/openstreetmap/josm/tools/StreamUtilsTest.java

    r11921 r15732  
    55import org.junit.Test;
    66import org.openstreetmap.josm.testutils.JOSMTestRules;
     7
     8import static org.junit.Assert.assertEquals;
     9
     10import java.util.Arrays;
     11import java.util.stream.Collectors;
    712
    813import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    2934        UtilityClassTestUtil.assertUtilityClassWellDefined(StreamUtils.class);
    3035    }
     36
     37    /**
     38     * Tests {@link StreamUtils#reversedStream(java.util.List)}
     39     */
     40    @Test
     41    public void testReverseStream() {
     42        assertEquals("baz/bar/foo",
     43                StreamUtils.reversedStream(Arrays.asList("foo", "bar", "baz")).collect(Collectors.joining("/")));
     44    }
    3145}
Note: See TracChangeset for help on using the changeset viewer.