Search in sources :

Example 91 with Geometry

use of com.vividsolutions.jts.geom.Geometry 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)

Example 92 with Geometry

use of com.vividsolutions.jts.geom.Geometry in project ddf by codice.

the class RegistryPackageConverter method getWKTFromGeometry.

private static String getWKTFromGeometry(AbstractGeometryType geometry, JAXBElement jaxbElement) throws RegistryConversionException {
    String convertedGeometry = null;
    if (geometry != null) {
        try {
            GML311ToJTSGeometryConverter geometryConverter = new GML311ToJTSGeometryConverter();
            Geometry jtsGeometry = geometryConverter.createGeometry(new DefaultRootObjectLocator(jaxbElement), geometry);
            if (jtsGeometry != null) {
                WKTWriter writer = new WKTWriter();
                convertedGeometry = writer.write(jtsGeometry);
            }
        } catch (ConversionFailedException e) {
            String message = "Error converting geometry. Caught an exception.";
            throw new RegistryConversionException(message, e);
        }
    }
    return convertedGeometry;
}
Also used : GML311ToJTSGeometryConverter(org.jvnet.ogc.gml.v_3_1_1.jts.GML311ToJTSGeometryConverter) Geometry(com.vividsolutions.jts.geom.Geometry) DefaultRootObjectLocator(org.jvnet.jaxb2_commons.locator.DefaultRootObjectLocator) WKTWriter(com.vividsolutions.jts.io.WKTWriter) ConversionFailedException(org.jvnet.ogc.gml.v_3_1_1.jts.ConversionFailedException)

Example 93 with Geometry

use of com.vividsolutions.jts.geom.Geometry in project ddf by codice.

the class Gml3ToWktImpl method convert.

@SuppressWarnings("unchecked")
public String convert(InputStream xml) throws ValidationExceptionImpl {
    AbstractGeometryType geometry = null;
    try {
        JAXBElement<AbstractGeometryType> jaxbElement = parser.unmarshal(configurator, JAXBElement.class, xml);
        geometry = jaxbElement.getValue();
        GML311ToJTSGeometryConverter geometryConverter = new GML311ToJTSGeometryConverter();
        Geometry jtsGeo = geometryConverter.createGeometry(new DefaultRootObjectLocator(jaxbElement), geometry);
        WKTWriter wktWriter = new WKTWriter();
        return wktWriter.write(jtsGeo);
    } catch (ParserException e) {
        LOGGER.debug("Cannot parse gml", e);
        throw new ValidationExceptionImpl(e, Collections.singletonList("Cannot parse gml"), new ArrayList<String>());
    } catch (ConversionFailedException e) {
        LOGGER.debug("Cannot convert gml311 geo object {} to jts", geometry, e);
        throw new ValidationExceptionImpl(e, Collections.singletonList("Cannot convert geo object"), new ArrayList<String>());
    }
}
Also used : GML311ToJTSGeometryConverter(org.jvnet.ogc.gml.v_3_1_1.jts.GML311ToJTSGeometryConverter) Geometry(com.vividsolutions.jts.geom.Geometry) DefaultRootObjectLocator(org.jvnet.jaxb2_commons.locator.DefaultRootObjectLocator) WKTWriter(com.vividsolutions.jts.io.WKTWriter) ParserException(org.codice.ddf.parser.ParserException) ConversionFailedException(org.jvnet.ogc.gml.v_3_1_1.jts.ConversionFailedException) AbstractGeometryType(net.opengis.gml.v_3_1_1.AbstractGeometryType) ValidationExceptionImpl(ddf.catalog.validation.impl.ValidationExceptionImpl) ArrayList(java.util.ArrayList)

Example 94 with Geometry

use of com.vividsolutions.jts.geom.Geometry in project ddf by codice.

the class GmlHandler method endElement.

/**
     * Takes in a sax event from {@link org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate}, and if it is a gml:Point or a sub-element of a gml:Point, it will
     * * hand the event off to the GML handler for further parsing.
     * If it is a gml:Point, the point is stored as a WKT in the attributes list
     *
     * @param namespaceURI the namespaceURI that is passed in by {@link org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate}
     * @param localName    the localName that is passed in by {@link org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate}
     * @param qName        the qName that is passed in by {@link org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate}
     */
@Override
public void endElement(String namespaceURI, String localName, String qName) {
    if (readingGml) {
        try {
            gml3Element.toElement(namespaceURI, localName);
        } catch (XMLStreamException e) {
            LOGGER.debug("Error writing to element in SaxEventToXmlConverter()", e);
        }
        if (!readingGml3) {
            try {
                gh.endElement(namespaceURI, localName, qName);
            } catch (SAXException e) {
                LOGGER.debug("GML threw a SAX exception", e);
            }
        }
        state.pop();
        if (state.size() == 0) {
            readingGml = false;
            if (!readingGml3) {
                Geometry geo = gh.getGeometry();
                attributes.add(new AttributeImpl(Metacard.GEOGRAPHY, wktWriter.write(geo)));
            } else {
                try {
                    attributes.add(new AttributeImpl(Metacard.GEOGRAPHY, gml3Converter.convert(gml3Element.toString())));
                } catch (ValidationException e) {
                    this.attributes.add(new AttributeImpl(Validation.VALIDATION_ERRORS, "geospatial-handler"));
                }
            }
            readingGml3 = false;
            gml3Element.reset();
        }
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) ValidationException(ddf.catalog.validation.ValidationException) XMLStreamException(javax.xml.stream.XMLStreamException) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) SAXException(org.xml.sax.SAXException)

Example 95 with Geometry

use of com.vividsolutions.jts.geom.Geometry in project ddf by codice.

the class XmlNode method readGeometry.

public static Geometry readGeometry(String value) {
    WKTReader wktReader = new WKTReader();
    Geometry geo = null;
    try {
        geo = wktReader.read(value);
    } catch (ParseException e) {
        LOGGER.debug("Failed to parse geometry information.", e);
    }
    return geo;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) ParseException(com.vividsolutions.jts.io.ParseException) WKTReader(com.vividsolutions.jts.io.WKTReader)

Aggregations

Geometry (com.vividsolutions.jts.geom.Geometry)123 WKTReader (com.vividsolutions.jts.io.WKTReader)35 Test (org.junit.Test)31 Coordinate (com.vividsolutions.jts.geom.Coordinate)23 Point (com.vividsolutions.jts.geom.Point)21 ParseException (com.vividsolutions.jts.io.ParseException)19 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)14 Envelope (com.vividsolutions.jts.geom.Envelope)13 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)13 WKTWriter (com.vividsolutions.jts.io.WKTWriter)13 ArrayList (java.util.ArrayList)12 LineString (com.vividsolutions.jts.geom.LineString)10 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)9 Metacard (ddf.catalog.data.Metacard)9 SimpleFeature (org.opengis.feature.simple.SimpleFeature)7 Optional (com.google.common.base.Optional)6 IOException (java.io.IOException)6 RevFeature (org.locationtech.geogig.api.RevFeature)6 JtsGeometry (org.locationtech.spatial4j.shape.jts.JtsGeometry)6 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)5