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;
}
}
}
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);
}
}
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);
}
}
}
Aggregations