Search in sources :

Example 6 with GeoFormatException

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;
}
Also used : GeoFormatException(org.codice.ddf.libs.geo.GeoFormatException)

Example 7 with GeoFormatException

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);
                }
            }
        }
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) LiteralExpressionImpl(org.geotools.filter.LiteralExpressionImpl) GeoFormatException(org.codice.ddf.libs.geo.GeoFormatException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Aggregations

GeoFormatException (org.codice.ddf.libs.geo.GeoFormatException)7 Geometry (com.vividsolutions.jts.geom.Geometry)4 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)3 Hints (org.geotools.factory.Hints)2 FactoryException (org.opengis.referencing.FactoryException)2 CRSAuthorityFactory (org.opengis.referencing.crs.CRSAuthorityFactory)2 MathTransform (org.opengis.referencing.operation.MathTransform)2 TransformException (org.opengis.referencing.operation.TransformException)2 ParseException (com.vividsolutions.jts.io.ParseException)1 WKTWriter (com.vividsolutions.jts.io.WKTWriter)1 GMLReader (com.vividsolutions.jts.io.gml2.GMLReader)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XmlNode (org.codice.ddf.spatial.ogc.catalog.common.converter.XmlNode)1 LiteralExpressionImpl (org.geotools.filter.LiteralExpressionImpl)1 ReferenceIdentifier (org.opengis.referencing.ReferenceIdentifier)1 SAXException (org.xml.sax.SAXException)1