Search in sources :

Example 1 with TopologyException

use of com.vividsolutions.jts.geom.TopologyException in project Osmand by osmandapp.

the class JtsAdapter method flatIntersection.

/**
 * JTS 1.14 does not support intersection on a {@link GeometryCollection}. This function works around this
 * by performing intersection on a flat list of geometry. The resulting list is pre-filtered for invalid
 * or empty geometry (outside of bounds). Invalid geometry are logged as errors.
 *
 * @param envelope non-list geometry defines bounding area
 * @param dataGeoms geometry pre-passed through {@link #flatFeatureList(Geometry)}
 * @return list of geometry from {@code data} intersecting with {@code envelope}.
 */
private static List<Geometry> flatIntersection(Geometry envelope, List<Geometry> dataGeoms) {
    final List<Geometry> intersectedGeoms = new ArrayList<>(dataGeoms.size());
    Geometry nextIntersected;
    for (Geometry nextGeom : dataGeoms) {
        try {
            // AABB intersection culling
            if (envelope.getEnvelopeInternal().intersects(nextGeom.getEnvelopeInternal())) {
                nextIntersected = envelope.intersection(nextGeom);
                if (!nextIntersected.isEmpty()) {
                    nextIntersected.setUserData(nextGeom.getUserData());
                    intersectedGeoms.add(nextIntersected);
                }
            }
        } catch (TopologyException e) {
        // LoggerFactory.getLogger(JtsAdapter.class).error(e.getMessage(), e);
        }
    }
    return intersectedGeoms;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) ArrayList(java.util.ArrayList) TopologyException(com.vividsolutions.jts.geom.TopologyException)

Example 2 with TopologyException

use of com.vividsolutions.jts.geom.TopologyException in project hale by halestudio.

the class InteriorPoint method calculateInteriorPoint.

/**
 * Calculate an interior point for a given geometry or object holding a
 * geometry.
 *
 * @param geometryHolder {@link Geometry}, {@link GeometryProperty} or
 *            {@link Instance} holding a geometry
 * @return an interior point of the geometry
 * @throws TransformationException if the interior point could not be
 *             calculated
 */
public static GeometryProperty<?> calculateInteriorPoint(Object geometryHolder) throws TransformationException {
    // depth first traverser that on cancel continues traversal but w/o the
    // children of the current object
    InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
    GeometryFinder geoFind = new GeometryFinder(null);
    traverser.traverse(geometryHolder, geoFind);
    List<GeometryProperty<?>> geoms = geoFind.getGeometries();
    Geometry result;
    CRSDefinition oldCRS = null;
    // use the first geometry encountered
    int index = 0;
    Geometry geom = null;
    while (geom == null && index < geoms.size()) {
        geom = geoms.get(index).getGeometry();
        oldCRS = geoms.get(index).getCRSDefinition();
        index++;
    }
    if (geom != null) {
        try {
            result = geom.getInteriorPoint();
        } catch (TopologyException e) {
            // calculate the point for a geometry with a small buffer to
            // avoid error with polygons that have overlapping lines
            result = geom.buffer(0.000001).getInteriorPoint();
            if (!result.within(geom)) {
                // geometry
                throw new TransformationException("Could not determine interior point for geometry");
            }
        }
    } else {
        return null;
    }
    return new DefaultGeometryProperty<Geometry>(oldCRS, result);
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) TopologyException(com.vividsolutions.jts.geom.TopologyException) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)

Example 3 with TopologyException

use of com.vividsolutions.jts.geom.TopologyException in project atlas-checks by osmlab.

the class IntersectingBuildingsCheck method findIntersectionType.

/**
 * Find {@link IntersectionType} for given {@link Polygon}s. There are some edge cases where
 * there are minor boundary intersections. So we do additional area check to filter off the
 * false positives.
 *
 * @param polygon
 *            {@link Polygon} to check for intersection
 * @param otherPolygon
 *            Another {@link Polygon} to check against for intersection
 * @param areaSizeToCheck
 *            Area size to decide if intersection area is big enough for overlap
 * @return {@link IntersectionType} between given {@link Polygon}s
 */
private IntersectionType findIntersectionType(final Polygon polygon, final Polygon otherPolygon) {
    Clip clip = null;
    try {
        clip = polygon.clip(otherPolygon, ClipType.AND);
    } catch (final TopologyException e) {
        logger.warn(String.format("Skipping intersection check. Error clipping [%s] and [%s].", polygon, otherPolygon), e);
    }
    // Skip if nothing is returned
    if (clip == null) {
        return IntersectionType.NONE;
    }
    // Sum intersection area
    long intersectionArea = 0;
    for (final PolyLine polyline : clip.getClip()) {
        if (polyline != null && polyline instanceof Polygon) {
            final Polygon clippedPolygon = (Polygon) polyline;
            intersectionArea += clippedPolygon.surface().asDm7Squared();
        }
    }
    // Avoid division by zero
    if (intersectionArea == 0) {
        return IntersectionType.NONE;
    }
    // Pick the smaller building's area as baseline
    final long baselineArea = Math.min(polygon.surface().asDm7Squared(), otherPolygon.surface().asDm7Squared());
    final double proportion = (double) intersectionArea / baselineArea;
    if (proportion >= this.overlapLowerLimit) {
        return IntersectionType.OVERLAP;
    } else if (proportion >= this.intersectionLowerLimit) {
        return IntersectionType.INTERSECT;
    }
    return IntersectionType.NONE;
}
Also used : Clip(org.openstreetmap.atlas.geography.clipping.Clip) PolyLine(org.openstreetmap.atlas.geography.PolyLine) Polygon(org.openstreetmap.atlas.geography.Polygon) TopologyException(com.vividsolutions.jts.geom.TopologyException)

Aggregations

TopologyException (com.vividsolutions.jts.geom.TopologyException)3 Geometry (com.vividsolutions.jts.geom.Geometry)2 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)1 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)1 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)1 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)1 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)1 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)1 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)1 ArrayList (java.util.ArrayList)1 PolyLine (org.openstreetmap.atlas.geography.PolyLine)1 Polygon (org.openstreetmap.atlas.geography.Polygon)1 Clip (org.openstreetmap.atlas.geography.clipping.Clip)1