use of org.codice.ddf.libs.geo.GeoFormatException in project ddf by codice.
the class GeospatialUtil method parseDMSLongitudeWithDecimalSeconds.
/**
* Parses Longitude in the DMS format of [D]DD:MM:SS.S E/W
*
* @param dmsLon Degrees Minutes Seconds formatted longitude.
* @return Longitude in decimal degrees.
*/
public static Double parseDMSLongitudeWithDecimalSeconds(String dmsLon) throws GeoFormatException {
Double lon = null;
if (dmsLon != null) {
dmsLon = dmsLon.trim();
String hemi = dmsLon.substring(dmsLon.length() - 1);
int hemisphereMult = 1;
if (!(hemi.equalsIgnoreCase("W") || hemi.equalsIgnoreCase("E"))) {
throw new GeoFormatException(String.format("Unrecognized hemisphere, %s, should be 'E' or 'W'", hemi));
}
if (hemi.equalsIgnoreCase("w")) {
hemisphereMult = -1;
}
String numberPortion = dmsLon.substring(0, dmsLon.length() - 1);
if (dmsLon.contains(":")) {
String[] dmsArr = numberPortion.split(":");
int degrees = 0;
try {
degrees = Integer.parseInt(dmsArr[0]);
} catch (NumberFormatException nfe) {
throw new GeoFormatException(String.format("Unable to parse degrees: %s from: %s", dmsArr[0], dmsLon), nfe);
}
int minutes = 0;
double seconds = 0.0;
if (dmsArr.length >= 2) {
try {
minutes = Integer.parseInt(dmsArr[1]);
} catch (NumberFormatException nfe) {
throw new GeoFormatException(String.format("Unable to parse minutes: %s from: %s", dmsArr[1], dmsLon), nfe);
}
}
if (dmsArr.length == 3) {
try {
seconds = Double.parseDouble(dmsArr[2]);
} catch (NumberFormatException nfe) {
throw new GeoFormatException(String.format("Unable to parse seconds: %s from: %s", dmsArr[2], dmsLon), nfe);
}
}
lon = hemisphereMult * (degrees + ((double) minutes / 60) + (seconds / 3600));
if (lon < -180 || lon > 180) {
throw new GeoFormatException(String.format("Invalid longitude provided (must be between -180 and 180 degrees), converted longitude: %f", lon));
}
}
}
return lon;
}
use of org.codice.ddf.libs.geo.GeoFormatException in project ddf by codice.
the class CswRecordMapperFilterVisitor method convertGeometryExpressionToEpsg4326.
private static void convertGeometryExpressionToEpsg4326(Expression expression) {
if (expression instanceof LiteralExpressionImpl) {
LiteralExpressionImpl literalExpression = (LiteralExpressionImpl) expression;
Object valueObj = literalExpression.getValue();
if (valueObj instanceof Geometry) {
Geometry geometry = (Geometry) valueObj;
Object userDataObj = geometry.getUserData();
if (userDataObj instanceof CoordinateReferenceSystem) {
CoordinateReferenceSystem sourceCRS = (CoordinateReferenceSystem) userDataObj;
Geometry convertedGeometry = null;
try {
convertedGeometry = GeospatialUtil.transformToEPSG4326LonLatFormat(geometry, sourceCRS);
literalExpression.setValue(convertedGeometry);
} catch (GeoFormatException e) {
LOGGER.trace("Unable to convert geometry to EPSG:4326 format", e);
}
}
}
}
}
Aggregations