Search in sources :

Example 1 with CoordinatePair

use of org.apache.jena.geosparql.implementation.great_circle.CoordinatePair in project jena by apache.

the class GeometryWrapper method distanceGreatCircle.

/**
 * Distance (Great Circle) in the Units of Measure stated in URI.
 *
 * @param targetGeometry
 * @param targetDistanceUnitsURI
 * @return Distance
 * @throws org.opengis.util.FactoryException
 * @throws org.opengis.referencing.operation.TransformException
 */
public double distanceGreatCircle(GeometryWrapper targetGeometry, String targetDistanceUnitsURI) throws FactoryException, MismatchedDimensionException, TransformException {
    GeometryWrapper transformedSourceGeometry;
    if (srsInfo.isGeographic()) {
        // Already a geographic SRS.
        transformedSourceGeometry = this;
    } else {
        // Use WGS84 and not CRS84 as assuming WGS8 is more prevalent.
        transformedSourceGeometry = this.transform(SRS_URI.WGS84_CRS);
    }
    GeometryWrapper transformedTargetGeometry = transformedSourceGeometry.checkTransformSRS(targetGeometry);
    // Find the nearest pair of coordinates from each Geometry using Euclidean distance (adjusting for wrap around).
    // These are then used for Great Circle distance.
    CoordinatePair coordinatePair = CoordinatePair.findNearestPair(transformedSourceGeometry, transformedTargetGeometry);
    // Check whether the nearest pair are the same, i.e. the overlap or within each other.
    if (coordinatePair.isEqual()) {
        // Exit early as the distance is zero.
        return 0.0;
    }
    Coordinate coord1 = coordinatePair.getCoord1();
    Coordinate coord2 = coordinatePair.getCoord2();
    double distance = GreatCircleDistance.haversineFormula(coord1.getY(), coord1.getX(), coord2.getY(), coord2.getX());
    // Convert the Great Circle distance from metres into the requested units.
    Boolean isTargetUnitsLinear = UnitsRegistry.isLinearUnits(targetDistanceUnitsURI);
    double targetDistance;
    if (isTargetUnitsLinear) {
        // Target units are linear so straight conversion. Distance is in metres already.
        targetDistance = UnitsOfMeasure.conversion(distance, Unit_URI.METRE_URL, targetDistanceUnitsURI);
    } else {
        targetDistance = UnitsOfMeasure.convertBetween(distance, Unit_URI.METRE_URL, targetDistanceUnitsURI, isTargetUnitsLinear, transformedSourceGeometry.getLatitude());
    }
    return targetDistance;
}
Also used : CoordinatePair(org.apache.jena.geosparql.implementation.great_circle.CoordinatePair) Coordinate(org.locationtech.jts.geom.Coordinate)

Aggregations

CoordinatePair (org.apache.jena.geosparql.implementation.great_circle.CoordinatePair)1 Coordinate (org.locationtech.jts.geom.Coordinate)1