Search in sources :

Example 11 with OsmWay

use of de.topobyte.osm4j.core.model.iface.OsmWay in project osm4j-geometry by topobyte.

the class RelationUtil method buildRings.

/**
 * Given a set of ways and their head's and tail's in the wayTailMap,
 * construct valid rings by combining ways with same tail-ids.
 */
public static List<ChainOfWays> buildRings(MultiSet<OsmWay> ways, CountingMultiValMap<Long, OsmWay> wayTailMap) {
    List<ChainOfWays> rings = new ArrayList<>();
    while (ways.keySet().size() > 0) {
        // Pick a way as part of a new ring
        OsmWay way = ways.keySet().iterator().next();
        ChainOfWays ring = new ChainOfWays(way);
        rings.add(ring);
        // Remove it from the set of ways...
        ways.remove(way);
        // ...and from the tail map.
        long id0 = way.getNodeId(0);
        long idN = way.getNodeId(way.getNumberOfNodes() - 1);
        wayTailMap.remove(id0, way);
        wayTailMap.remove(idN, way);
        // Try to assemble a ring for the way
        while (!ring.isClosed()) {
            long last = ring.getLast();
            Set<OsmWay> waySet = wayTailMap.getForKey(last);
            boolean accept = false;
            Iterator<OsmWay> iter = waySet.iterator();
            OsmWay take = null;
            while (iter.hasNext()) {
                take = iter.next();
                if (ring.getWaySet().contains(take)) {
                    // do not allow the same way to be included twice.
                    continue;
                }
                accept = true;
                break;
            }
            if (!accept) {
                // System.out.println(ring.segments.get(ring.segments.size()-1).getWay().getId());
                break;
            }
            if (take == null) {
                // can't be null, but avoid warning.
                break;
            }
            // Extend the current ring with the found way..
            ring.addWay(take);
            // And remove it from the set of ways...
            ways.remove(take);
            // ...and from the tail map.
            long tid0 = take.getNodeId(0);
            long tidN = take.getNodeId(take.getNumberOfNodes() - 1);
            wayTailMap.remove(tid0, take);
            wayTailMap.remove(tidN, take);
        }
    }
    return rings;
}
Also used : OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) ArrayList(java.util.ArrayList) TLongArrayList(com.slimjars.dist.gnu.trove.list.array.TLongArrayList)

Example 12 with OsmWay

use of de.topobyte.osm4j.core.model.iface.OsmWay in project osm4j-geometry by topobyte.

the class LineworkBuilder method build.

public LineworkBuilderResult build(OsmRelation relation, OsmEntityProvider provider) throws EntityNotFoundException {
    EntityNotFoundStrategy enfs = Util.strategy(missingEntitiesStrategy, log, logLevel);
    EntityFinder finder = EntityFinders.create(provider, enfs);
    Set<OsmNode> nodes = new HashSet<>();
    Set<OsmWay> ways = new HashSet<>();
    try {
        finder.findMemberNodesAndWays(relation, nodes, ways);
    } catch (EntityNotFoundException e) {
        switch(missingEntitiesStrategy) {
            default:
            case THROW_EXCEPTION:
                throw (e);
            case BUILD_EMPTY:
                return new LineworkBuilderResult();
            case BUILD_PARTIAL:
                // case
                break;
        }
    }
    return build(nodes, ways, provider);
}
Also used : OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) EntityNotFoundException(de.topobyte.osm4j.core.resolve.EntityNotFoundException) EntityNotFoundStrategy(de.topobyte.osm4j.core.resolve.EntityNotFoundStrategy) EntityFinder(de.topobyte.osm4j.core.resolve.EntityFinder) HashSet(java.util.HashSet)

Example 13 with OsmWay

use of de.topobyte.osm4j.core.model.iface.OsmWay in project osm4j-core by topobyte.

the class TestDataSetGetters method getWaySuccessful.

@Test
public void getWaySuccessful() throws EntityNotFoundException {
    OsmWay way = data.getWay(1);
    assertEquals(way.getId(), 1);
}
Also used : OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) Test(org.junit.Test)

Example 14 with OsmWay

use of de.topobyte.osm4j.core.model.iface.OsmWay in project osm4j-core by topobyte.

the class EntityFinderIgnoreMissing method findWays.

@Override
public List<OsmWay> findWays(TLongCollection ids) throws EntityNotFoundException {
    List<OsmWay> ways = new ArrayList<>();
    TLongIterator idIterator = ids.iterator();
    while (idIterator.hasNext()) {
        try {
            ways.add(entityProvider.getWay(idIterator.next()));
        } catch (EntityNotFoundException e) {
        // ignore silently
        }
    }
    return ways;
}
Also used : OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) ArrayList(java.util.ArrayList) TLongIterator(com.slimjars.dist.gnu.trove.iterator.TLongIterator)

Example 15 with OsmWay

use of de.topobyte.osm4j.core.model.iface.OsmWay in project osm4j-core by topobyte.

the class EntityFinderThrowMissing method findWays.

@Override
public List<OsmWay> findWays(TLongCollection ids) throws EntityNotFoundException {
    List<OsmWay> ways = new ArrayList<>();
    TLongIterator idIterator = ids.iterator();
    while (idIterator.hasNext()) {
        ways.add(entityProvider.getWay(idIterator.next()));
    }
    return ways;
}
Also used : OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) ArrayList(java.util.ArrayList) TLongIterator(com.slimjars.dist.gnu.trove.iterator.TLongIterator)

Aggregations

OsmWay (de.topobyte.osm4j.core.model.iface.OsmWay)24 OsmNode (de.topobyte.osm4j.core.model.iface.OsmNode)10 ArrayList (java.util.ArrayList)10 TLongArrayList (com.slimjars.dist.gnu.trove.list.array.TLongArrayList)9 OsmRelation (de.topobyte.osm4j.core.model.iface.OsmRelation)7 Way (de.topobyte.osm4j.core.model.impl.Way)7 OsmHandler (de.topobyte.osm4j.core.access.OsmHandler)4 OsmBounds (de.topobyte.osm4j.core.model.iface.OsmBounds)4 Node (de.topobyte.osm4j.core.model.impl.Node)4 Relation (de.topobyte.osm4j.core.model.impl.Relation)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 TLongIterator (com.slimjars.dist.gnu.trove.iterator.TLongIterator)3 EntityFinder (de.topobyte.osm4j.core.resolve.EntityFinder)3 EntityNotFoundException (de.topobyte.osm4j.core.resolve.EntityNotFoundException)3 EntityNotFoundStrategy (de.topobyte.osm4j.core.resolve.EntityNotFoundStrategy)3 TLongList (com.slimjars.dist.gnu.trove.list.TLongList)2 EntityContainer (de.topobyte.osm4j.core.model.iface.EntityContainer)2 OsmMetadata (de.topobyte.osm4j.core.model.iface.OsmMetadata)2 OsmRelationMember (de.topobyte.osm4j.core.model.iface.OsmRelationMember)2