Search in sources :

Example 1 with EntityNotFoundException

use of de.topobyte.osm4j.core.resolve.EntityNotFoundException in project osm4j-geometry by topobyte.

the class WayBuilder method buildReturnEmptyIfNodeMissing.

public WayBuilderResult buildReturnEmptyIfNodeMissing(OsmWay way, OsmEntityProvider resolver) {
    WayBuilderResult result = new WayBuilderResult();
    int numNodes = way.getNumberOfNodes();
    if (numNodes == 0) {
        return result;
    }
    if (numNodes == 1) {
        if (!includePuntal) {
            return result;
        } else {
            try {
                OsmNode node = resolver.getNode(way.getNodeId(0));
                result.getCoordinates().add(nodeBuilder.buildCoordinate(node));
            } catch (EntityNotFoundException e) {
                return result;
            }
        }
    }
    CoordinateSequence cs = factory.getCoordinateSequenceFactory().create(numNodes, 2);
    for (int i = 0; i < way.getNumberOfNodes(); i++) {
        OsmNode node;
        try {
            node = resolver.getNode(way.getNodeId(i));
        } catch (EntityNotFoundException e) {
            result.clear();
            return result;
        }
        cs.setOrdinate(i, 0, node.getLongitude());
        cs.setOrdinate(i, 1, node.getLatitude());
    }
    createLine(result, cs, OsmModelUtil.isClosed(way));
    return result;
}
Also used : CoordinateSequence(com.vividsolutions.jts.geom.CoordinateSequence) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) EntityNotFoundException(de.topobyte.osm4j.core.resolve.EntityNotFoundException)

Example 2 with EntityNotFoundException

use of de.topobyte.osm4j.core.resolve.EntityNotFoundException in project osm4j-geometry by topobyte.

the class WayBuilder method buildOmitVertexIfNodeMissing.

public WayBuilderResult buildOmitVertexIfNodeMissing(OsmWay way, OsmEntityProvider resolver) {
    WayBuilderResult result = new WayBuilderResult();
    // Test if the way is closed, i.e. first node id == last node id
    boolean closed = OsmModelUtil.isClosed(way);
    // Remember if the first node is missing, so that we can handle closed
    // ways appropriately
    boolean firstMissing = false;
    List<Coordinate> coords = new ArrayList<>();
    for (int i = 0; i < way.getNumberOfNodes(); i++) {
        OsmNode node;
        try {
            node = resolver.getNode(way.getNodeId(i));
        } catch (EntityNotFoundException e) {
            if (log) {
                logMissingNode(way.getNodeId(i));
            }
            if (i == 0) {
                firstMissing = true;
            }
            continue;
        }
        coords.add(new Coordinate(node.getLongitude(), node.getLatitude()));
    }
    if (coords.size() == 0) {
        return result;
    }
    if (coords.size() == 1) {
        if (!includePuntal) {
            return result;
        } else {
            result.getCoordinates().add(coords.get(0));
            return result;
        }
    }
    // the way by replicating the first found coordinate at the end.
    if (closed && firstMissing && coords.size() > 2) {
        coords.add(coords.get(0));
    }
    CoordinateSequence cs = factory.getCoordinateSequenceFactory().create(coords.toArray(new Coordinate[0]));
    createLine(result, cs, closed);
    return result;
}
Also used : CoordinateSequence(com.vividsolutions.jts.geom.CoordinateSequence) Coordinate(com.vividsolutions.jts.geom.Coordinate) OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) ArrayList(java.util.ArrayList) EntityNotFoundException(de.topobyte.osm4j.core.resolve.EntityNotFoundException)

Example 3 with EntityNotFoundException

use of de.topobyte.osm4j.core.resolve.EntityNotFoundException in project osm4j-geometry by topobyte.

the class RelationUtil method checkRings.

/**
 * For each ring in this collection of rings, check whether it is closed. If
 * not, print some status information.
 *
 * @param missingEntitiesStrategy
 */
public static void checkRings(Collection<ChainOfWays> rings, OsmEntityProvider resolver, MissingEntitiesStrategy missingEntitiesStrategy) throws EntityNotFoundException {
    for (ChainOfWays ring : rings) {
        if (ring.isClosed()) {
            continue;
        }
        List<WaySegment> segments = ring.getSegments();
        int len = 0;
        for (WaySegment rs : segments) {
            len += rs.getWay().getNumberOfNodes();
        }
        WaySegment seg0 = segments.get(0);
        WaySegment segN = segments.get(segments.size() - 1);
        long nodeId1 = seg0.getNodeId(0);
        long nodeIdN = segN.getNodeId(segN.getNumberOfNodes() - 1);
        try {
            OsmNode node1 = resolver.getNode(nodeId1);
            OsmNode nodeN = resolver.getNode(nodeIdN);
            logger.debug("we have an unclosed ring of size " + len);
            logger.debug(String.format("start/end: %f,%f %f,%f", node1.getLongitude(), node1.getLatitude(), nodeN.getLongitude(), nodeN.getLatitude()));
        } catch (EntityNotFoundException e) {
            switch(missingEntitiesStrategy) {
                case BUILD_PARTIAL:
                    continue;
                default:
                case BUILD_EMPTY:
                case THROW_EXCEPTION:
                    throw (e);
            }
        }
    }
}
Also used : OsmNode(de.topobyte.osm4j.core.model.iface.OsmNode) EntityNotFoundException(de.topobyte.osm4j.core.resolve.EntityNotFoundException)

Example 4 with EntityNotFoundException

use of de.topobyte.osm4j.core.resolve.EntityNotFoundException 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 5 with EntityNotFoundException

use of de.topobyte.osm4j.core.resolve.EntityNotFoundException 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

EntityNotFoundException (de.topobyte.osm4j.core.resolve.EntityNotFoundException)7 OsmNode (de.topobyte.osm4j.core.model.iface.OsmNode)6 OsmWay (de.topobyte.osm4j.core.model.iface.OsmWay)3 HashSet (java.util.HashSet)3 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 CoordinateSequence (com.vividsolutions.jts.geom.CoordinateSequence)2 EntityFinder (de.topobyte.osm4j.core.resolve.EntityFinder)2 EntityNotFoundStrategy (de.topobyte.osm4j.core.resolve.EntityNotFoundStrategy)2 ArrayList (java.util.ArrayList)2 LinearRing (com.vividsolutions.jts.geom.LinearRing)1 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)1 ChainOfNodes (de.topobyte.osm4j.geometry.relation.ChainOfNodes)1 ChainOfWays (de.topobyte.osm4j.geometry.relation.ChainOfWays)1