use of org.codice.ddf.libs.geo.GeoFormatException in project ddf by codice.
the class GeospatialUtil method transformToEPSG4326LonLatFormat.
/**
* Transform a geometry to EPSG:4326 format with lon/lat coordinate ordering.
* NOTE: This method will NOT perform the transform swapping coordinates even if the sourceCrsName is
* EPSG:4326.
*
* @param geometry - Geometry to transform
* @param sourceCrs - Source geometry's coordinate reference system
* @return Geometry - Transformed geometry into EPSG:4326 lon/lat coordinate system
*/
public static Geometry transformToEPSG4326LonLatFormat(Geometry geometry, CoordinateReferenceSystem sourceCrs) throws GeoFormatException {
if (geometry == null) {
throw new GeoFormatException("Unable to convert null geometry");
}
//If we don't have source CRS just return geometry as we can't transform without that information
if (sourceCrs == null || CollectionUtils.isEmpty(sourceCrs.getIdentifiers())) {
return geometry;
}
Geometry transformedGeometry = geometry;
try {
boolean sourceCrsMatchesTarget = false;
for (ReferenceIdentifier referenceIdentifier : sourceCrs.getIdentifiers()) {
if (referenceIdentifier.toString().equalsIgnoreCase(EPSG_4326)) {
sourceCrsMatchesTarget = true;
break;
}
}
if (!sourceCrsMatchesTarget) {
Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
CoordinateReferenceSystem targetCRS = factory.createCoordinateReferenceSystem(EPSG_4326);
MathTransform transform = CRS.findMathTransform(sourceCrs, targetCRS);
transformedGeometry = JTS.transform(geometry, transform);
LOGGER.debug("Converted CRS {} into {} : {}", sourceCrs, EPSG_4326, geometry);
}
} catch (FactoryException | TransformException e) {
throw new GeoFormatException("Unable to convert coordinate to " + EPSG_4326, e);
}
return transformedGeometry;
}
use of org.codice.ddf.libs.geo.GeoFormatException in project ddf by codice.
the class GeospatialUtil method transformToEPSG4326LonLatFormat.
/**
* Transform a geometry to EPSG:4326 format with lon/lat coordinate ordering.
* NOTE: This method will perform the transform swapping coordinates even if the sourceCrsName is
* EPSG:4326
*
* @param geometry - Geometry to transform
* @param sourceCrsName - Source geometry's coordinate reference system
* @return Geometry - Transformed geometry into EPSG:4326 lon/lat coordinate system
*/
public static Geometry transformToEPSG4326LonLatFormat(Geometry geometry, String sourceCrsName) throws GeoFormatException {
if (geometry == null) {
throw new GeoFormatException("Unable to convert null geometry");
}
//If we don't have source CRS just return geometry as we can't transform without that information
if (sourceCrsName == null) {
return geometry;
}
try {
CoordinateReferenceSystem sourceCrs = CRS.decode(sourceCrsName);
Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
CoordinateReferenceSystem targetCRS = factory.createCoordinateReferenceSystem(EPSG_4326);
MathTransform transform = CRS.findMathTransform(sourceCrs, targetCRS);
return JTS.transform(geometry, transform);
} catch (FactoryException | TransformException e) {
throw new GeoFormatException("Unable to convert coordinate to " + EPSG_4326, e);
}
}
use of org.codice.ddf.libs.geo.GeoFormatException in project ddf by codice.
the class GeospatialUtil method parseDMSLatitudeWithDecimalSeconds.
/**
* Parses Latitude in the DMS format of DD:MM:SS.S N/S
*
* @param dmsLat Degrees Minutes Seconds formatted latitude.
* @return Latitude in decimal degrees
*/
public static Double parseDMSLatitudeWithDecimalSeconds(String dmsLat) throws GeoFormatException {
Double lat = null;
if (dmsLat != null) {
dmsLat = dmsLat.trim();
String hemi = dmsLat.substring(dmsLat.length() - 1);
if (!(hemi.equalsIgnoreCase("N") || hemi.equalsIgnoreCase("S"))) {
throw new GeoFormatException(String.format("Unrecognized hemisphere, %s, should be 'N' or 'S'", hemi));
}
int hemisphereMult = 1;
if (hemi.equalsIgnoreCase("s")) {
hemisphereMult = -1;
}
String numberPortion = dmsLat.substring(0, dmsLat.length() - 1);
if (dmsLat.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], dmsLat), 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], dmsLat), 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], dmsLat), nfe);
}
}
lat = hemisphereMult * (degrees + ((double) minutes / 60) + (seconds / 3600));
if (lat < -90 || lat > 90) {
throw new GeoFormatException(String.format("Invalid latitude provided (must be between -90 and 90 degrees), converted latitude: %f", lat));
}
}
}
return lat;
}
use of org.codice.ddf.libs.geo.GeoFormatException in project ddf by codice.
the class AbstractFeatureConverter method getValueForMetacardAttribute.
protected Serializable getValueForMetacardAttribute(AttributeFormat attributeFormat, HierarchicalStreamReader reader) {
Serializable ser = null;
switch(attributeFormat) {
case BOOLEAN:
ser = Boolean.valueOf(reader.getValue());
break;
case DOUBLE:
ser = Double.valueOf(reader.getValue());
break;
case FLOAT:
ser = Float.valueOf(reader.getValue());
break;
case INTEGER:
ser = Integer.valueOf(reader.getValue());
break;
case LONG:
ser = Long.valueOf(reader.getValue());
break;
case SHORT:
ser = Short.valueOf(reader.getValue());
break;
case XML:
case STRING:
ser = reader.getValue();
break;
case GEOMETRY:
XmlNode node = new XmlNode(reader);
String xml = node.toString();
GMLReader gmlReader = new GMLReader();
Geometry geo = null;
try {
geo = gmlReader.read(xml, null);
if (StringUtils.isNotBlank(srs) && !srs.equals(GeospatialUtil.EPSG_4326)) {
geo = GeospatialUtil.transformToEPSG4326LonLatFormat(geo, srs);
}
} catch (SAXException | IOException | ParserConfigurationException | GeoFormatException e) {
geo = null;
LOGGER.debug(ERROR_PARSING_MESSAGE, e);
}
if (geo != null) {
WKTWriter wktWriter = new WKTWriter();
ser = wktWriter.write(geo);
}
break;
case BINARY:
try {
ser = reader.getValue().getBytes(UTF8_ENCODING);
} catch (UnsupportedEncodingException e) {
LOGGER.debug("Error encoding the binary value into the metacard.", e);
}
break;
case DATE:
ser = parseDateFromXml(reader);
break;
default:
break;
}
return ser;
}
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