Search in sources :

Example 1 with DistanceOp

use of org.locationtech.jts.operation.distance.DistanceOp in project jena by apache.

the class CoordinatePair method findNearestPair.

public static final CoordinatePair findNearestPair(GeometryWrapper sourceGeometry, GeometryWrapper targetGeometry) throws SrsException {
    // Both GeoemtryWrappers should be same SRS andn Geographic.
    SRSInfo sourceSRSInfo = sourceGeometry.getSrsInfo();
    SRSInfo targetSRSInfo = targetGeometry.getSrsInfo();
    if (!(sourceSRSInfo.isGeographic() && targetSRSInfo.isGeographic()) || !(sourceSRSInfo.getSrsURI().equals(targetSRSInfo.getSrsURI()))) {
        throw new SrsException("Expected same Geographic SRS for GeometryWrappers. " + sourceGeometry + " : " + targetGeometry);
    }
    // Find nearest points.
    Point point1 = null;
    Point point2 = null;
    Geometry sourceXYGeometry = sourceGeometry.getXYGeometry();
    Geometry targetXYGeometry = targetGeometry.getXYGeometry();
    // Check whether only dealing with Point geometries.
    if (sourceXYGeometry instanceof Point) {
        point1 = (Point) sourceXYGeometry;
    }
    if (targetXYGeometry instanceof Point) {
        point2 = (Point) targetXYGeometry;
    }
    // Exit if both are points.
    if (point1 != null && point2 != null) {
        return new CoordinatePair(point1.getCoordinate(), point2.getCoordinate());
    }
    // Both same SRS so same domain range.
    Envelope sourceEnvelope = sourceGeometry.getEnvelope();
    Envelope targetEnvelope = targetGeometry.getEnvelope();
    double domainRange = sourceSRSInfo.getDomainRangeX();
    double halfRange = domainRange / 2;
    double diff = targetEnvelope.getMaxX() - sourceEnvelope.getMinX();
    Geometry adjustedSource;
    Geometry adjustedTarget;
    if (diff > halfRange) {
        // Difference is greater than positive half range, then translate source by the range.
        adjustedSource = sourceGeometry.translateXYGeometry();
        adjustedTarget = targetXYGeometry;
    } else if (diff < -halfRange) {
        // Difference is less than negative half range, then translate target by the range.
        adjustedSource = sourceXYGeometry;
        adjustedTarget = targetGeometry.translateXYGeometry();
    } else {
        // Difference is between the ranges so don't translate.
        adjustedSource = sourceXYGeometry;
        adjustedTarget = targetXYGeometry;
    }
    DistanceOp distanceOp = new DistanceOp(adjustedSource, adjustedTarget);
    Coordinate[] nearest = distanceOp.nearestPoints();
    return new CoordinatePair(nearest[0], nearest[1]);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) SrsException(org.apache.jena.geosparql.configuration.SrsException) SRSInfo(org.apache.jena.geosparql.implementation.SRSInfo) Coordinate(org.locationtech.jts.geom.Coordinate) DistanceOp(org.locationtech.jts.operation.distance.DistanceOp) Point(org.locationtech.jts.geom.Point) Envelope(org.locationtech.jts.geom.Envelope)

Aggregations

SrsException (org.apache.jena.geosparql.configuration.SrsException)1 SRSInfo (org.apache.jena.geosparql.implementation.SRSInfo)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Envelope (org.locationtech.jts.geom.Envelope)1 Geometry (org.locationtech.jts.geom.Geometry)1 Point (org.locationtech.jts.geom.Point)1 DistanceOp (org.locationtech.jts.operation.distance.DistanceOp)1