Search in sources :

Example 1 with JTSToGML311GeometryConverter

use of org.jvnet.ogc.gml.v_3_1_1.jts.JTSToGML311GeometryConverter in project ddf by codice.

the class GeometryAdapter method marshalFrom.

public static GeometryElement marshalFrom(Attribute attribute) throws CatalogTransformerException {
    GeometryElement element = new GeometryElement();
    element.setName(attribute.getName());
    if (attribute.getValue() != null) {
        for (Serializable value : attribute.getValues()) {
            if (!(value instanceof String)) {
                continue;
            }
            String wkt = (String) value;
            WKTReader wktReader = new WKTReader(geometryFactory);
            Geometry jtsGeometry = null;
            try {
                jtsGeometry = wktReader.read(wkt);
            } catch (ParseException e) {
                throw new CatalogTransformerException("Could not transform Metacard to XML.  Invalid WKT.", e);
            }
            JTSToGML311GeometryConverter converter = new JTSToGML311GeometryConverter();
            @SuppressWarnings("unchecked") JAXBElement<AbstractGeometryType> gmlElement = (JAXBElement<AbstractGeometryType>) converter.createElement(jtsGeometry);
            GeometryElement.Value geoValue = new GeometryElement.Value();
            geoValue.setGeometry(gmlElement);
            ((GeometryElement) element).getValue().add(geoValue);
        }
    }
    return element;
}
Also used : Serializable(java.io.Serializable) JTSToGML311GeometryConverter(org.jvnet.ogc.gml.v_3_1_1.jts.JTSToGML311GeometryConverter) AbstractGeometryType(net.opengis.gml.v_3_1_1.AbstractGeometryType) Value(ddf.catalog.transformer.xml.binding.GeometryElement.Value) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) JAXBElement(javax.xml.bind.JAXBElement) WKTReader(com.vividsolutions.jts.io.WKTReader) Geometry(com.vividsolutions.jts.geom.Geometry) Value(ddf.catalog.transformer.xml.binding.GeometryElement.Value) GeometryElement(ddf.catalog.transformer.xml.binding.GeometryElement) ParseException(com.vividsolutions.jts.io.ParseException)

Example 2 with JTSToGML311GeometryConverter

use of org.jvnet.ogc.gml.v_3_1_1.jts.JTSToGML311GeometryConverter in project ddf by codice.

the class CswFilterFactory method convertGeometry.

@SuppressWarnings("unchecked")
private JAXBElement<? extends AbstractGeometryType> convertGeometry(Geometry geometry) {
    geometry.setUserData(CswConstants.SRS_NAME);
    JAXBElement<? extends AbstractGeometryType> abstractGeometry = null;
    try {
        Map<String, String> geoConverterProps = new HashMap<String, String>();
        geoConverterProps.put(CswJTSToGML311GeometryConverter.USE_POS_LIST_GEO_CONVERTER_PROP_KEY, String.valueOf(this.isSetUsePosList));
        JTSToGML311GeometryConverter converter = new CswJTSToGML311GeometryConverter(geoConverterProps);
        Marshaller marshaller = new MarshallerImpl(JAXB_CONTEXT.createMarshaller(), converter);
        StringWriter writer = new StringWriter();
        marshaller.marshal(geometry, writer);
        String xmlGeo = writer.toString();
        LOGGER.debug("Geometry as XML: {}", xmlGeo);
        XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
        XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(xmlGeo));
        Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
        Object object = unmarshaller.unmarshal(xmlStreamReader);
        LOGGER.debug("Unmarshalled as => {}", object);
        if (object instanceof JAXBElement) {
            abstractGeometry = (JAXBElement<? extends AbstractGeometryType>) object;
        } else {
            LOGGER.debug("Unable to cast to JAXBElement<? extends AbstractGeometryType>.  Object is of type [{}].", object.getClass().getName());
        }
    } catch (JAXBException | XMLStreamException e) {
        LOGGER.debug("Unable to unmarshal geometry [{}]", geometry.getClass().getName(), e);
    }
    return abstractGeometry;
}
Also used : JTSToGML311GeometryConverter(org.jvnet.ogc.gml.v_3_1_1.jts.JTSToGML311GeometryConverter) Marshaller(javax.xml.bind.Marshaller) MarshallerImpl(org.jvnet.ogc.gml.v_3_1_1.jts.MarshallerImpl) XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) JAXBException(javax.xml.bind.JAXBException) JAXBElement(javax.xml.bind.JAXBElement) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

JAXBElement (javax.xml.bind.JAXBElement)2 JTSToGML311GeometryConverter (org.jvnet.ogc.gml.v_3_1_1.jts.JTSToGML311GeometryConverter)2 Geometry (com.vividsolutions.jts.geom.Geometry)1 ParseException (com.vividsolutions.jts.io.ParseException)1 WKTReader (com.vividsolutions.jts.io.WKTReader)1 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)1 GeometryElement (ddf.catalog.transformer.xml.binding.GeometryElement)1 Value (ddf.catalog.transformer.xml.binding.GeometryElement.Value)1 Serializable (java.io.Serializable)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 AbstractGeometryType (net.opengis.gml.v_3_1_1.AbstractGeometryType)1 MarshallerImpl (org.jvnet.ogc.gml.v_3_1_1.jts.MarshallerImpl)1