Search in sources :

Example 1 with GeoFormatException

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;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) Hints(org.geotools.factory.Hints) MathTransform(org.opengis.referencing.operation.MathTransform) FactoryException(org.opengis.referencing.FactoryException) GeoFormatException(org.codice.ddf.libs.geo.GeoFormatException) TransformException(org.opengis.referencing.operation.TransformException) CRSAuthorityFactory(org.opengis.referencing.crs.CRSAuthorityFactory) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 2 with GeoFormatException

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);
    }
}
Also used : Hints(org.geotools.factory.Hints) MathTransform(org.opengis.referencing.operation.MathTransform) FactoryException(org.opengis.referencing.FactoryException) GeoFormatException(org.codice.ddf.libs.geo.GeoFormatException) TransformException(org.opengis.referencing.operation.TransformException) CRSAuthorityFactory(org.opengis.referencing.crs.CRSAuthorityFactory) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 3 with GeoFormatException

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

Example 4 with GeoFormatException

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;
}
Also used : GMLReader(com.vividsolutions.jts.io.gml2.GMLReader) Geometry(com.vividsolutions.jts.geom.Geometry) WKTWriter(com.vividsolutions.jts.io.WKTWriter) Serializable(java.io.Serializable) XmlNode(org.codice.ddf.spatial.ogc.catalog.common.converter.XmlNode) GeoFormatException(org.codice.ddf.libs.geo.GeoFormatException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 5 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