Search in sources :

Example 1 with ChainOfNodes

use of de.topobyte.osm4j.geometry.relation.ChainOfNodes in project osm4j-geometry by topobyte.

the class RegionBuilder method convert.

private void convert(Collection<ChainOfNodes> rings, Collection<ChainOfNodes> nonRings, OsmEntityProvider resolver, Collection<Coordinate> coordinates, Collection<LineString> lineStrings, Collection<LinearRing> linearRings) throws EntityNotFoundException {
    for (ChainOfNodes ring : rings) {
        Way way = new Way(-1, ring.getNodes());
        WayBuilderResult result = wayBuilder.build(way, resolver);
        add(result, coordinates, lineStrings, linearRings);
    }
    for (ChainOfNodes ring : nonRings) {
        Way way = new Way(-1, ring.getNodes());
        WayBuilderResult result = wayBuilder.build(way, resolver);
        add(result, coordinates, lineStrings, linearRings);
    }
}
Also used : ChainOfNodes(de.topobyte.osm4j.geometry.relation.ChainOfNodes) OsmWay(de.topobyte.osm4j.core.model.iface.OsmWay) Way(de.topobyte.osm4j.core.model.impl.Way)

Example 2 with ChainOfNodes

use of de.topobyte.osm4j.geometry.relation.ChainOfNodes 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)

Aggregations

OsmWay (de.topobyte.osm4j.core.model.iface.OsmWay)2 ChainOfNodes (de.topobyte.osm4j.geometry.relation.ChainOfNodes)2 LinearRing (com.vividsolutions.jts.geom.LinearRing)1 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)1 Way (de.topobyte.osm4j.core.model.impl.Way)1 EntityNotFoundException (de.topobyte.osm4j.core.resolve.EntityNotFoundException)1 ChainOfWays (de.topobyte.osm4j.geometry.relation.ChainOfWays)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1