Search in sources :

Example 1 with Line

use of org.openstreetmap.atlas.geography.atlas.items.Line in project atlas-checks by osmlab.

the class SelfIntersectingPolylineCheck method flag.

@Override
protected Optional<CheckFlag> flag(final AtlasObject object) {
    final Optional<CheckFlag> response;
    final int localizedInstructionIndex;
    final PolyLine polyline;
    if (object instanceof Edge) {
        polyline = ((Edge) object).asPolyLine();
        // Send building instructions if building tag exists
        localizedInstructionIndex = (TagPredicates.IS_BUILDING.test(object)) ? 2 : 0;
    } else if (object instanceof Line) {
        polyline = ((Line) object).asPolyLine();
        // Send building instructions if building tag exists
        localizedInstructionIndex = (TagPredicates.IS_BUILDING.test(object)) ? 2 : 0;
    } else if (object instanceof Area) {
        polyline = ((Area) object).asPolygon();
        // Send duplicate Edge instructions if duplicate Edges exist
        localizedInstructionIndex = hasDuplicateSegments(polyline) ? THREE : 1;
    } else {
        throw new CoreException("Invalid item type {}", object.getClass().toString());
    }
    // First, find shape point intersections
    final Set<Location> selfIntersections = polyline.selfIntersections();
    if (selfIntersections.size() > 0) {
        final CheckFlag flag = new CheckFlag(Long.toString(object.getIdentifier()));
        flag.addObject(object);
        flag.addInstruction(this.getLocalizedInstruction(localizedInstructionIndex, object.getOsmIdentifier(), selfIntersections.toString()));
        selfIntersections.forEach(flag::addPoint);
        response = Optional.of(flag);
    } else {
        // Next, find intersections occurring at non-shape points using JTS verification
        boolean isJtsValid = true;
        try {
            if (object instanceof Area) {
                isJtsValid = GeometryValidator.isValidPolygon((Polygon) polyline);
            } else {
                isJtsValid = GeometryValidator.isValidPolyLine(polyline);
            }
        } catch (final IllegalArgumentException e) {
            // Invalid geometry found when converting the PolyLine/Polygon.
            // This can be a number of cases. For example, a LineString expects exactly 0 or >=2
            // points or a Polygon expects 0 or >= 4 points. This isn't self-intersecting
            // geometry, but rather inconsistent geometry, according to JTS.
            logger.error("Encountered invalid geometry for feature {}", object.getOsmIdentifier(), e);
        }
        if (!isJtsValid) {
            response = Optional.of(createFlag(object, this.getLocalizedInstruction(localizedInstructionIndex)));
        } else {
            response = Optional.empty();
        }
    }
    return response;
}
Also used : PolyLine(org.openstreetmap.atlas.geography.PolyLine) PolyLine(org.openstreetmap.atlas.geography.PolyLine) Line(org.openstreetmap.atlas.geography.atlas.items.Line) Area(org.openstreetmap.atlas.geography.atlas.items.Area) CoreException(org.openstreetmap.atlas.exception.CoreException) CheckFlag(org.openstreetmap.atlas.checks.flag.CheckFlag) Polygon(org.openstreetmap.atlas.geography.Polygon) Edge(org.openstreetmap.atlas.geography.atlas.items.Edge) Location(org.openstreetmap.atlas.geography.Location)

Aggregations

CheckFlag (org.openstreetmap.atlas.checks.flag.CheckFlag)1 CoreException (org.openstreetmap.atlas.exception.CoreException)1 Location (org.openstreetmap.atlas.geography.Location)1 PolyLine (org.openstreetmap.atlas.geography.PolyLine)1 Polygon (org.openstreetmap.atlas.geography.Polygon)1 Area (org.openstreetmap.atlas.geography.atlas.items.Area)1 Edge (org.openstreetmap.atlas.geography.atlas.items.Edge)1 Line (org.openstreetmap.atlas.geography.atlas.items.Line)1