Search in sources :

Example 1 with DisjointSet

use of org.opentripplanner.common.DisjointSet in project OpenTripPlanner by opentripplanner.

the class AreaGroup method groupAreas.

public static List<AreaGroup> groupAreas(Map<Area, OSMLevel> areasLevels) {
    DisjointSet<Area> groups = new DisjointSet<Area>();
    Multimap<OSMNode, Area> areasForNode = LinkedListMultimap.create();
    for (Area area : areasLevels.keySet()) {
        for (Ring ring : area.outermostRings) {
            for (Ring inner : ring.holes) {
                for (OSMNode node : inner.nodes) {
                    areasForNode.put(node, area);
                }
            }
            for (OSMNode node : ring.nodes) {
                areasForNode.put(node, area);
            }
        }
    }
    // areas that can be joined must share nodes and levels
    for (OSMNode osmNode : areasForNode.keySet()) {
        for (Area area1 : areasForNode.get(osmNode)) {
            OSMLevel level1 = areasLevels.get(area1);
            for (Area area2 : areasForNode.get(osmNode)) {
                OSMLevel level2 = areasLevels.get(area2);
                if ((level1 == null && level2 == null) || (level1 != null && level1.equals(level2))) {
                    groups.union(area1, area2);
                }
            }
        }
    }
    List<AreaGroup> out = new ArrayList<AreaGroup>();
    for (Set<Area> areaSet : groups.sets()) {
        try {
            out.add(new AreaGroup(areaSet));
        } catch (RingConstructionException e) {
            for (Area area : areaSet) {
                LOG.debug("Failed to create merged area for " + area + ".  This area might not be at fault; it might be one of the other areas in this list.");
                out.add(new AreaGroup(Arrays.asList(area)));
            }
        }
    }
    return out;
}
Also used : RingConstructionException(org.opentripplanner.graph_builder.module.osm.Ring.RingConstructionException) ArrayList(java.util.ArrayList) DisjointSet(org.opentripplanner.common.DisjointSet) OSMNode(org.opentripplanner.openstreetmap.model.OSMNode) OSMLevel(org.opentripplanner.openstreetmap.model.OSMLevel)

Example 2 with DisjointSet

use of org.opentripplanner.common.DisjointSet in project OpenTripPlanner by opentripplanner.

the class TestDisjointSet method testRandom.

public void testRandom() {
    DisjointSet<Integer> set = new DisjointSet<Integer>();
    Random random = new Random(1);
    for (int i = 0; i < 150; ++i) {
        set.union(Math.abs(random.nextInt() % 700), Math.abs(random.nextInt() % 700));
    }
    HashMap<Integer, Integer> seen = new HashMap<Integer, Integer>();
    int sizeSum = 0;
    for (int i = 0; i < 700; ++i) {
        int key = set.find(i);
        int size = set.size(key);
        Integer lastSize = seen.get(key);
        assertTrue(lastSize == null || size == lastSize);
        if (lastSize == null) {
            seen.put(key, size);
            sizeSum += size;
        }
        assertTrue(size >= 1 && size <= 150);
    }
    assertEquals(700, sizeSum);
}
Also used : Random(java.util.Random) HashMap(java.util.HashMap) DisjointSet(org.opentripplanner.common.DisjointSet)

Aggregations

DisjointSet (org.opentripplanner.common.DisjointSet)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 RingConstructionException (org.opentripplanner.graph_builder.module.osm.Ring.RingConstructionException)1 OSMLevel (org.opentripplanner.openstreetmap.model.OSMLevel)1 OSMNode (org.opentripplanner.openstreetmap.model.OSMNode)1