Search in sources :

Example 6 with WKTWriter

use of org.locationtech.jts.io.WKTWriter in project ddf by codice.

the class Gml3ToWktImpl method convert.

@Override
public String convert(InputStream xml) throws ValidationException {
    Object parsedObject = parseXml(xml);
    if (parsedObject instanceof Envelope) {
        parsedObject = JTS.toGeometry((Envelope) parsedObject);
    }
    if (parsedObject instanceof Geometry) {
        try {
            Geometry geometry = convertCRS((Geometry) parsedObject);
            return new WKTWriter().write(geometry);
        } catch (TransformException e) {
            LOGGER.debug("Failed to transform geometry to lon/lat", e);
            throw new ValidationExceptionImpl(e, Collections.singletonList("Cannot transform geometry to lon/lat"), new ArrayList<>());
        }
    }
    LOGGER.debug("Unknown object parsed from GML and unable to convert to WKT");
    throw new ValidationExceptionImpl("", Collections.singletonList("Couldn't not convert GML to WKT"), new ArrayList<>());
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) WKTWriter(org.locationtech.jts.io.WKTWriter) ValidationExceptionImpl(ddf.catalog.validation.impl.ValidationExceptionImpl) TransformException(org.opengis.referencing.operation.TransformException) ArrayList(java.util.ArrayList) Envelope(org.locationtech.jts.geom.Envelope)

Example 7 with WKTWriter

use of org.locationtech.jts.io.WKTWriter in project ddf by codice.

the class CswFilterDelegate method bufferGeometry.

private String bufferGeometry(String wkt, double distance) {
    LOGGER.debug("Buffering WKT {} by distance {} meter(s).", wkt, distance);
    Geometry geometry = getGeometryFromWkt(wkt);
    double bufferInDegrees = metersToDegrees(distance);
    LOGGER.debug("Buffering {} by {} degree(s).", geometry.getClass().getSimpleName(), bufferInDegrees);
    Geometry bufferedGeometry = geometry.buffer(bufferInDegrees);
    String bufferedWkt = new WKTWriter().write(bufferedGeometry);
    LOGGER.debug("Buffered WKT: {}.", bufferedWkt);
    return bufferedWkt;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) WKTWriter(org.locationtech.jts.io.WKTWriter)

Example 8 with WKTWriter

use of org.locationtech.jts.io.WKTWriter in project ddf by codice.

the class AbstractFeatureConverterWfs20 method getValueForMetacardAttribute.

@Override
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 geometryXml = node.toString();
            Geometry geo = null;
            geo = (Geometry) readGml(geometryXml);
            LOGGER.debug("coordinateOrder = {}", coordinateOrder);
            if (GeospatialUtil.LAT_LON_ORDER.equals(coordinateOrder)) {
                swapCoordinates(geo);
            }
            if (geo != null) {
                WKTWriter wktWriter = new WKTWriter();
                ser = wktWriter.write(geo);
                LOGGER.debug("wkt = {}", ser);
            }
            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 : Geometry(org.locationtech.jts.geom.Geometry) WKTWriter(org.locationtech.jts.io.WKTWriter) Serializable(java.io.Serializable) XmlNode(org.codice.ddf.spatial.ogc.catalog.common.converter.XmlNode) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 9 with WKTWriter

use of org.locationtech.jts.io.WKTWriter in project ddf by codice.

the class GeometryAdapter method unmarshalFrom.

public static Attribute unmarshalFrom(GeometryElement element) throws ConversionFailedException {
    AttributeImpl attribute = null;
    GML311ToJTSGeometryConverter converter = new GML311ToJTSGeometryConverter();
    WKTWriter wktWriter = new WKTWriter();
    for (Value xmlValue : element.getValue()) {
        JAXBElement<AbstractGeometryType> xmlGeometry = xmlValue.getGeometry();
        Geometry geometry = null;
        if (xmlGeometry != null && xmlGeometry.getValue() != null) {
            try {
                geometry = converter.createGeometry(new DefaultRootObjectLocator(xmlValue), xmlGeometry.getValue());
            } catch (ConversionFailedException e) {
                LOGGER.debug("Unable to adapt goemetry. ", e);
            }
        }
        if (geometry != null && !geometry.isEmpty()) {
            String wkt = wktWriter.write(geometry);
            if (attribute == null) {
                attribute = new AttributeImpl(element.getName(), wkt);
            } else {
                attribute.addValue(wkt);
            }
        }
    }
    return attribute;
}
Also used : GML311ToJTSGeometryConverter(org.jvnet.ogc.gml.v_3_1_1.jts.GML311ToJTSGeometryConverter) Geometry(org.locationtech.jts.geom.Geometry) WKTWriter(org.locationtech.jts.io.WKTWriter) DefaultRootObjectLocator(org.jvnet.jaxb2_commons.locator.DefaultRootObjectLocator) ConversionFailedException(org.jvnet.ogc.gml.v_3_1_1.jts.ConversionFailedException) AbstractGeometryType(net.opengis.gml.v_3_1_1.AbstractGeometryType) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) Value(ddf.catalog.transformer.xml.binding.GeometryElement.Value)

Aggregations

WKTWriter (org.locationtech.jts.io.WKTWriter)9 Geometry (org.locationtech.jts.geom.Geometry)7 Serializable (java.io.Serializable)3 XmlNode (org.codice.ddf.spatial.ogc.catalog.common.converter.XmlNode)3 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 GMLReader (org.locationtech.jts.io.gml2.GMLReader)2 SAXException (org.xml.sax.SAXException)2 Metacard (ddf.catalog.data.Metacard)1 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 Value (ddf.catalog.transformer.xml.binding.GeometryElement.Value)1 ValidationExceptionImpl (ddf.catalog.validation.impl.ValidationExceptionImpl)1 ArrayList (java.util.ArrayList)1 AbstractGeometryType (net.opengis.gml.v_3_1_1.AbstractGeometryType)1 GeoFormatException (org.codice.ddf.libs.geo.GeoFormatException)1 Before (org.junit.Before)1 DefaultRootObjectLocator (org.jvnet.jaxb2_commons.locator.DefaultRootObjectLocator)1 ConversionFailedException (org.jvnet.ogc.gml.v_3_1_1.jts.ConversionFailedException)1