Search in sources :

Example 21 with OsmWay

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

the class RegionBuilder method build.

/**
 * Build a MultiPolygon from a Set of Ways.
 *
 * @param ways
 *            the ways to use for geometry construction.
 * @return the constructed MultiPolygon.
 * @throws EntityNotFoundException
 *             when a required entity cannot be obtained.
 */
public RegionBuilderResult build(MultiSet<OsmWay> ways, OsmEntityProvider resolver, Set<OsmNode> nodes) throws EntityNotFoundException {
    RegionBuilderResult result = new RegionBuilderResult();
    logger.debug("Have " + ways.keySet().size() + " ways");
    // Only keep ways with 2 or more nodes. Also don't keep ways with just 2
    // times the same node.
    List<OsmWay> invalidWays = new ArrayList<>();
    for (OsmWay way : ways.keySet()) {
        int numNodes = way.getNumberOfNodes();
        if (numNodes == 0 || numNodes == 1) {
            invalidWays.add(way);
        } else if (numNodes == 2 && OsmModelUtil.isClosed(way)) {
            invalidWays.add(way);
        }
    }
    ways.removeAllOccurences(invalidWays);
    logger.debug("Removed " + invalidWays.size() + " invalid ways");
    CountingMultiValMap<Long, OsmWay> wayTailMap = RelationUtil.buildWayTailMap(ways);
    List<ChainOfWays> chains = RelationUtil.buildRings(ways, wayTailMap);
    List<ChainOfNodes> rings = new ArrayList<>();
    List<ChainOfNodes> nonRings = new ArrayList<>();
    RelationUtil.convertToSegmentChainsAndResolveNodeIntersections(chains, rings, nonRings);
    try {
        RelationUtil.checkRings(chains, resolver, missingEntitiesStrategy);
    } catch (EntityNotFoundException e) {
        switch(missingEntitiesStrategy) {
            case BUILD_PARTIAL:
                // exception with BUILD_PARTIAL
                break;
            case BUILD_EMPTY:
                return new RegionBuilderResult();
            default:
            case THROW_EXCEPTION:
                throw (e);
        }
    }
    // This could be used to close non-closed chains
    // RelationUtil.closeUnclosedRingWithStraightLine(rings);
    List<LinearRing> linearRings = new ArrayList<>();
    convert(rings, nonRings, resolver, result.getCoordinates(), result.getLineStrings(), linearRings);
    Set<LinearRing> validRings = new HashSet<>();
    for (LinearRing r : linearRings) {
        Set<LinearRing> repaired = SelfIntersectionUtil.repair(r);
        for (LinearRing ring : repaired) {
            validRings.add(ring);
        }
    }
    MultiPolygon mp = PolygonHelper.multipolygonFromRings(validRings, false);
    result.setMultiPolygon(mp);
    if (includePuntal) {
        GeometryUtil.buildNodes(nodeBuilder, nodes, result.getCoordinates());
    }
    return result;
}
Also used : ChainOfWays(de.topobyte.osm4j.geometry.relation.ChainOfWays) ArrayList(java.util.ArrayList) EntityNotFoundException(de.topobyte.osm4j.core.resolve.EntityNotFoundException) OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) LinearRing(com.vividsolutions.jts.geom.LinearRing) ChainOfNodes(de.topobyte.osm4j.geometry.relation.ChainOfNodes) HashSet(java.util.HashSet)

Example 22 with OsmWay

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

the class RegionBuilder method build.

/**
 * Build a MultiPolygon from a Relation.
 *
 * @param relation
 *            a relation to construct the region for.
 * @return the constructed MultiPolygon.
 * @throws EntityNotFoundException
 *             when a required entity cannot be obtained.
 */
public RegionBuilderResult build(OsmRelation relation, OsmEntityProvider resolver) throws EntityNotFoundException {
    EntityNotFoundStrategy enfs = Util.strategy(missingEntitiesStrategy, log, logLevel);
    logger.debug("building region id:" + relation.getId());
    Set<OsmRelation> relations = new HashSet<>();
    MultiSet<OsmWay> ways = new HashMultiSet<>();
    EntityFinder finder = EntityFinders.create(resolver, enfs);
    relations.add(relation);
    finder.findMemberRelationsRecursively(relation, relations);
    finder.findMemberWays(relations, ways);
    Set<OsmNode> nodes = new HashSet<>();
    if (includePuntal) {
        finder.findMemberNodes(relations, nodes);
    }
    return build(ways, resolver, nodes);
}
Also used : OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) OsmRelation(de.topobyte.osm4j.core.model.iface.OsmRelation) HashMultiSet(de.topobyte.adt.multicollections.HashMultiSet) EntityNotFoundStrategy(de.topobyte.osm4j.core.resolve.EntityNotFoundStrategy) EntityFinder(de.topobyte.osm4j.core.resolve.EntityFinder) HashSet(java.util.HashSet)

Example 23 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(Collection<OsmRelation> relations, 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(relations, 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 24 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(Collection<OsmNode> nodes, Collection<OsmWay> ways, OsmEntityProvider provider) throws EntityNotFoundException {
    LineworkBuilderResult result = new LineworkBuilderResult();
    GeometryUtil.buildNodes(nodeBuilder, nodes, result.getCoordinates());
    for (OsmWay way : ways) {
        WayBuilderResult wbr = wayBuilder.build(way, provider);
        result.getCoordinates().addAll(wbr.getCoordinates());
        result.getLineStrings().addAll(wbr.getLineStrings());
        if (wbr.getLinearRing() != null) {
            result.getLineStrings().add(wbr.getLinearRing());
        }
    }
    return result;
}
Also used : OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay)

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