Search in sources :

Example 1 with CoordinateOperationException

use of org.openforis.idm.geospatial.CoordinateOperationException in project collect by openforis.

the class GeoFunctions method distance.

/**
 * Calculates the orthodromic distance between 2 coordinates (in meters)
 */
protected static Double distance(ExpressionContext context, Object from, Object to) {
    if (from == null || to == null) {
        return null;
    }
    Coordinate fromC = from instanceof Coordinate ? (Coordinate) from : Coordinate.parseCoordinate(from);
    if (fromC == null || !fromC.isComplete()) {
        return null;
    }
    Coordinate toC = to instanceof Coordinate ? (Coordinate) to : Coordinate.parseCoordinate(to);
    if (toC == null || !toC.isComplete()) {
        return null;
    }
    CoordinateOperations coordinateOperations = getSurvey(context).getContext().getCoordinateOperations();
    if (coordinateOperations == null) {
        return null;
    } else {
        try {
            double distance = coordinateOperations.orthodromicDistance(fromC, toC);
            return distance;
        } catch (CoordinateOperationException e) {
            return null;
        }
    }
}
Also used : CoordinateOperations(org.openforis.idm.geospatial.CoordinateOperations) Coordinate(org.openforis.idm.model.Coordinate) CoordinateOperationException(org.openforis.idm.geospatial.CoordinateOperationException)

Example 2 with CoordinateOperationException

use of org.openforis.idm.geospatial.CoordinateOperationException in project collect by openforis.

the class GeoToolsCoordinateOperations method orthodromicDistance.

/**
 * Returns the orthodromic distance between two points
 *
 * @param startingPosition
 * @param destinationPosition
 * @return
 * @throws TransformException
 */
public double orthodromicDistance(Position startingPosition, Position destinationPosition) throws CoordinateOperationException {
    try {
        GeodeticCalculator calculator = new GeodeticCalculator();
        calculator.setStartingPosition(startingPosition);
        calculator.setDestinationPosition(destinationPosition);
        double result = calculator.getOrthodromicDistance();
        return result;
    } catch (Exception e) {
        throw new CoordinateOperationException("Failed to determine distance from " + startingPosition + " to " + destinationPosition, e);
    }
}
Also used : CoordinateOperationException(org.openforis.idm.geospatial.CoordinateOperationException) FactoryException(org.opengis.referencing.FactoryException) CoordinateOperationException(org.openforis.idm.geospatial.CoordinateOperationException) TransformException(org.opengis.referencing.operation.TransformException) GeodeticCalculator(org.geotools.referencing.GeodeticCalculator)

Example 3 with CoordinateOperationException

use of org.openforis.idm.geospatial.CoordinateOperationException in project collect by openforis.

the class GeoToolsCoordinateOperations method registerSRS.

@Override
public void registerSRS(SpatialReferenceSystem srs) {
    String srsId = srs.getId();
    if (!CRS_BY_SRS_ID.containsKey(srsId)) {
        String wkt = srs.getWellKnownText();
        try {
            MathTransform latLonTransform = transformCache.get(SpatialReferenceSystem.WGS84_SRS_ID, srsId);
            CoordinateReferenceSystem crs = parseWKT(wkt);
            latLonTransform = findToWGS84MathTransform(crs);
            transformCache.put(srsId, SpatialReferenceSystem.WGS84_SRS_ID, latLonTransform);
            CoordinateReferenceSystem webMercatorCrs = CRS_BY_SRS_ID.get(SpatialReferenceSystem.WEB_MERCATOR_SRS_ID);
            MathTransform webMarcatorTransform = CRS.findMathTransform(crs, webMercatorCrs);
            transformCache.put(srsId, SpatialReferenceSystem.WEB_MERCATOR_SRS_ID, webMarcatorTransform);
            CRS_BY_SRS_ID.put(srsId, crs);
        } catch (Exception e) {
            // throw new CoordinateOperationException(String.format("Error parsing SpatialRefernceSystem with id %s and Well Known Text %s", srsId, wkt), e);
            if (LOG.isErrorEnabled()) {
                LOG.error(String.format("Error parsing SpatialRefernceSystem with id %s and Well Known Text %s", srsId, wkt), e);
            }
            CRS_BY_SRS_ID.put(srsId, null);
        }
    }
}
Also used : MathTransform(org.opengis.referencing.operation.MathTransform) InternationalString(org.opengis.util.InternationalString) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FactoryException(org.opengis.referencing.FactoryException) CoordinateOperationException(org.openforis.idm.geospatial.CoordinateOperationException) TransformException(org.opengis.referencing.operation.TransformException)

Aggregations

CoordinateOperationException (org.openforis.idm.geospatial.CoordinateOperationException)3 FactoryException (org.opengis.referencing.FactoryException)2 TransformException (org.opengis.referencing.operation.TransformException)2 GeodeticCalculator (org.geotools.referencing.GeodeticCalculator)1 CoordinateOperations (org.openforis.idm.geospatial.CoordinateOperations)1 Coordinate (org.openforis.idm.model.Coordinate)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1 MathTransform (org.opengis.referencing.operation.MathTransform)1 InternationalString (org.opengis.util.InternationalString)1