Search in sources :

Example 1 with ComplexData

use of org.geotoolkit.wps.xml.v200.ComplexData in project geotoolkit by Geomatys.

the class WKTAdaptorTest method adapt.

private static String adapt(final Geometry geom) {
    final DataInput value = ADAPTOR.toWPS2Input(geom);
    assertNotNull(value);
    final List<Object> content = value.getData().getContent();
    assertNotNull(content);
    assertEquals("Number of values", 1, content.size());
    final Object innerContent = content.get(0);
    assertTrue(innerContent instanceof ComplexData);
    final ComplexData cData = (ComplexData) innerContent;
    return (String) cData.getContent().stream().filter(v -> v instanceof String).findAny().orElseThrow(() -> new AssertionError("No String content found"));
}
Also used : DataInput(org.geotoolkit.wps.xml.v200.DataInput) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Assert.assertNotNull(org.junit.Assert.assertNotNull) Coordinate(org.locationtech.jts.geom.Coordinate) Assert.assertTrue(org.junit.Assert.assertTrue) JTS(org.geotoolkit.geometry.jts.JTS) Test(org.junit.Test) Point(org.locationtech.jts.geom.Point) List(java.util.List) Geometry(org.locationtech.jts.geom.Geometry) CommonCRS(org.apache.sis.referencing.CommonCRS) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData) Assert(org.junit.Assert) DataInput(org.geotoolkit.wps.xml.v200.DataInput) Assert.assertEquals(org.junit.Assert.assertEquals) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData)

Example 2 with ComplexData

use of org.geotoolkit.wps.xml.v200.ComplexData in project geotoolkit by Geomatys.

the class WPS2ProcessDescriptor method toDescriptor.

/**
 * Convert Description to GeneralParameterDescriptor.
 *
 * @throws UnsupportedOperationException if data type could not be mapped
 */
private static GeneralParameterDescriptor toDescriptor(String processId, Description input) throws UnsupportedParameterException {
    final List<? extends Description> subInputs;
    final DataDescription dataDescType;
    final int min;
    final int max;
    if (input instanceof InputDescription) {
        final InputDescription id = (InputDescription) input;
        subInputs = id.getInput();
        dataDescType = id.getDataDescription();
        max = id.getMaxOccurs();
        min = id.getMinOccurs();
    } else if (input instanceof OutputDescription) {
        final OutputDescription od = (OutputDescription) input;
        subInputs = od.getOutput();
        dataDescType = od.getDataDescription();
        min = 1;
        max = 1;
    } else {
        throw new IllegalArgumentException("Unexpected description type " + input.getClass());
    }
    final String inputName = input.getIdentifier().getValue();
    final String title = input.getFirstTitle();
    final String remarks = input.getFirstAbstract();
    Map userObject = new HashMap();
    for (MetadataType meta : input.getMetadata()) {
        if (meta instanceof AdditionalParametersType) {
            AdditionalParametersType params = (AdditionalParametersType) meta;
            for (AdditionalParameter param : params.getAdditionalParameter()) {
                userObject.put(param.getName().getValue(), param.getValue());
            }
        }
    }
    if (dataDescType instanceof LiteralData) {
        final LiteralData cd = (LiteralData) dataDescType;
        for (LiteralDataDomain domain : cd.getLiteralDataDomain()) {
            final LiteralAdaptor adaptor = LiteralAdaptor.create(domain);
            if (adaptor == null)
                continue;
            String defaultValueValue = null;
            final ValueType defaultValue = domain.getDefaultValue();
            if (defaultValue != null)
                defaultValueValue = defaultValue.getValue();
            final Unit unit = getUnit(domain.getUOM());
            Object[] allowedValues = null;
            if (domain.getAllowedValues() != null && domain.getAllowedValues().getStringValues() != null) {
                allowedValues = new Object[domain.getAllowedValues().getStringValues().size()];
                int i = 0;
                for (String value : domain.getAllowedValues().getStringValues()) {
                    allowedValues[i] = adaptor.convert(value);
                    i++;
                }
            }
            try {
                userObject.put(DataAdaptor.USE_ADAPTOR, adaptor);
                return new ExtendedParameterDescriptor(inputName, title, remarks, min, max, adaptor.getValueClass(), adaptor.convert(defaultValueValue), allowedValues, userObject);
            } catch (UnconvertibleObjectException ex2) {
                throw new UnsupportedParameterException(processId, inputName, "Can't convert the default literal input value.", ex2);
            }
        }
        throw new UnsupportedParameterException(processId, inputName, "Unidentifiable literal input " + inputName);
    } else if (dataDescType instanceof ComplexData) {
        final ComplexData cdt = (ComplexData) dataDescType;
        // ensure default format is first in the list
        Collections.sort(cdt.getFormat(), (Format o1, Format o2) -> {
            boolean d1 = Boolean.TRUE.equals(o1.isDefault());
            boolean d2 = Boolean.TRUE.equals(o2.isDefault());
            if (d1 == d2)
                return 0;
            return d1 ? -1 : +1;
        });
        // find a complexe type adaptor
        DataAdaptor adaptor = null;
        for (Format format : cdt.getFormat()) {
            adaptor = ComplexAdaptor.getAdaptor(format);
            if (adaptor != null)
                break;
        }
        if (adaptor == null) {
            final StringBuilder sb = new StringBuilder();
            for (Format format : cdt.getFormat()) {
                if (sb.length() != 0)
                    sb.append(", ");
                sb.append(format.getMimeType()).append(' ');
                sb.append(format.getEncoding()).append(' ');
                sb.append(format.getSchema());
            }
            throw new UnsupportedParameterException(processId, inputName, "No compatible format found for parameter " + inputName + " formats : " + sb);
        }
        userObject.put(DataAdaptor.USE_ADAPTOR, adaptor);
        return new ExtendedParameterDescriptor(inputName, title, remarks, min, max, adaptor.getValueClass(), null, null, userObject);
    } else if (dataDescType instanceof BoundingBoxData) {
        final BboxAdaptor adaptor = BboxAdaptor.create((BoundingBoxData) dataDescType);
        userObject.put(DataAdaptor.USE_ADAPTOR, adaptor);
        return new ExtendedParameterDescriptor(inputName, title, remarks, min, max, Envelope.class, null, null, userObject);
    } else if (!subInputs.isEmpty()) {
        // sub group type
        final List<GeneralParameterDescriptor> params = new ArrayList<>();
        for (Description dt : subInputs) {
            params.add(toDescriptor(processId, dt));
        }
        return new ParameterBuilder().addName(inputName).addName(title).setRemarks(remarks).createGroup(params.toArray(new GeneralParameterDescriptor[0]));
    } else {
        throw new UnsupportedParameterException(processId, inputName, "Unidentifiable input " + inputName + " " + dataDescType);
    }
}
Also used : AdditionalParametersType(org.geotoolkit.ows.xml.v200.AdditionalParametersType) DataDescription(org.geotoolkit.wps.xml.v200.DataDescription) Description(org.geotoolkit.wps.xml.v200.Description) InputDescription(org.geotoolkit.wps.xml.v200.InputDescription) OutputDescription(org.geotoolkit.wps.xml.v200.OutputDescription) ProcessDescription(org.geotoolkit.wps.xml.v200.ProcessDescription) HashMap(java.util.HashMap) InternationalString(org.opengis.util.InternationalString) DefaultInternationalString(org.apache.sis.util.DefaultInternationalString) Unit(javax.measure.Unit) DataDescription(org.geotoolkit.wps.xml.v200.DataDescription) BboxAdaptor(org.geotoolkit.wps.adaptor.BboxAdaptor) OutputDescription(org.geotoolkit.wps.xml.v200.OutputDescription) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Format(org.geotoolkit.wps.xml.v200.Format) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData) DataAdaptor(org.geotoolkit.wps.adaptor.DataAdaptor) BoundingBoxData(org.geotoolkit.wps.xml.v200.BoundingBoxData) InputDescription(org.geotoolkit.wps.xml.v200.InputDescription) ArrayList(java.util.ArrayList) List(java.util.List) ParameterBuilder(org.apache.sis.parameter.ParameterBuilder) LiteralDataDomain(org.geotoolkit.wps.xml.v200.LiteralDataDomain) LiteralAdaptor(org.geotoolkit.wps.adaptor.LiteralAdaptor) LiteralData(org.geotoolkit.wps.xml.v200.LiteralData) ValueType(org.geotoolkit.ows.xml.v200.ValueType) MetadataType(org.geotoolkit.ows.xml.v200.MetadataType) DomainMetadataType(org.geotoolkit.ows.xml.v200.DomainMetadataType) AdditionalParameter(org.geotoolkit.ows.xml.v200.AdditionalParameter) ExtendedParameterDescriptor(org.geotoolkit.utility.parameter.ExtendedParameterDescriptor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ComplexData

use of org.geotoolkit.wps.xml.v200.ComplexData in project geotoolkit by Geomatys.

the class GMLAdaptor method toWPS2Input.

@Override
public DataInput toWPS2Input(Object candidate) throws UnconvertibleObjectException {
    if (candidate instanceof ReferenceProxy)
        return super.toWPS2Input(candidate);
    final ComplexData cdt = new ComplexData();
    cdt.getContent().add(new org.geotoolkit.wps.xml.v200.Format(encoding, mimeType, schema, null));
    final JAXPStreamFeatureWriter writer = new JAXPStreamFeatureWriter(gmlVersion, null, null);
    try {
        if (ENC_BASE64.equalsIgnoreCase(encoding)) {
            final ByteArrayOutputStream out = new ByteArrayOutputStream();
            writer.write(candidate, out);
            out.flush();
            String base64 = Base64.getEncoder().encodeToString(out.toByteArray());
            cdt.getContent().add(base64);
        } else if (ENC_UTF8.equalsIgnoreCase(encoding)) {
            final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
            final DocumentBuilder constructeur = fabrique.newDocumentBuilder();
            final Document document = constructeur.newDocument();
            final DOMResult domResult = new DOMResult(document);
            writer.write(candidate, domResult);
            cdt.getContent().add(document.getDocumentElement());
        }
    } catch (IOException | XMLStreamException | DataStoreException | ParserConfigurationException ex) {
        throw new UnconvertibleObjectException(ex.getMessage(), ex);
    }
    final Data data = new Data();
    data.getContent().add(cdt);
    final DataInput dit = new DataInput();
    dit.setData(data);
    return dit;
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DOMResult(javax.xml.transform.dom.DOMResult) Data(org.geotoolkit.wps.xml.v200.Data) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JAXPStreamFeatureWriter(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureWriter) Document(org.w3c.dom.Document) ReferenceProxy(org.geotoolkit.wps.xml.ReferenceProxy) Format(org.geotoolkit.wps.xml.v200.Format) DataInput(org.geotoolkit.wps.xml.v200.DataInput) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) XMLStreamException(javax.xml.stream.XMLStreamException) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 4 with ComplexData

use of org.geotoolkit.wps.xml.v200.ComplexData in project geotoolkit by Geomatys.

the class WKTAdaptor method toWPS2Input.

@Override
public DataInput toWPS2Input(Object candidate) throws UnconvertibleObjectException {
    if (candidate instanceof ReferenceProxy)
        return super.toWPS2Input(candidate);
    final ComplexData cdt = new ComplexData();
    cdt.getContent().add(new org.geotoolkit.wps.xml.v200.Format(encoding, mimeType, null, null));
    Geometry geom = (Geometry) candidate;
    int srid = 0;
    int dimension = 2;
    try {
        CoordinateReferenceSystem crs = Geometries.wrap(geom).get().getCoordinateReferenceSystem();
        if (crs != null) {
            dimension = crs.getCoordinateSystem().getDimension();
            final IdentifiedObjectFinder finder = IdentifiedObjects.newFinder("EPSG");
            // TODO: Ensure no project strongly rely on that, then remove. It's pure non-sense/madness.
            // Note: If you read this after march 2020: do not ask : delete.
            finder.setIgnoringAxes(true);
            final CoordinateReferenceSystem epsgcrs = (CoordinateReferenceSystem) finder.findSingleton(crs);
            if (epsgcrs != null) {
                srid = IdentifiedObjects.lookupEPSG(epsgcrs);
                // force geometry in longitude first
                final CoordinateReferenceSystem crs2 = ((AbstractCRS) crs).forConvention(AxesConvention.RIGHT_HANDED);
                if (crs2 != crs) {
                    geom = org.apache.sis.internal.feature.jts.JTS.transform(geom, crs2);
                }
                if (crs2 != null)
                    dimension = crs2.getCoordinateSystem().getDimension();
            }
        }
    } catch (FactoryException | MismatchedDimensionException | TransformException ex) {
        throw new UnconvertibleObjectException(ex.getMessage(), ex);
    }
    // String wkt = geom.toText();
    WKTWriter writer = new WKTWriter(dimension);
    String wkt = writer.write(geom);
    if (srid > 0) {
        wkt = "SRID=" + srid + ";" + wkt;
    }
    cdt.getContent().add(wkt);
    final Data data = new Data();
    data.getContent().add(cdt);
    final DataInput dit = new DataInput();
    dit.setData(data);
    return dit;
}
Also used : IdentifiedObjectFinder(org.apache.sis.referencing.factory.IdentifiedObjectFinder) WKTWriter(org.locationtech.jts.io.WKTWriter) FactoryException(org.opengis.util.FactoryException) AbstractCRS(org.apache.sis.referencing.crs.AbstractCRS) TransformException(org.opengis.referencing.operation.TransformException) Data(org.geotoolkit.wps.xml.v200.Data) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) ReferenceProxy(org.geotoolkit.wps.xml.ReferenceProxy) Format(org.geotoolkit.wps.xml.v200.Format) Geometry(org.locationtech.jts.geom.Geometry) DataInput(org.geotoolkit.wps.xml.v200.DataInput) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 5 with ComplexData

use of org.geotoolkit.wps.xml.v200.ComplexData in project geotoolkit by Geomatys.

the class XMLAdaptor method toWPS2Input.

@Override
public DataInput toWPS2Input(Node candidate) throws UnconvertibleObjectException {
    if (candidate instanceof ReferenceProxy) {
        return super.toWPS2Input(candidate);
    }
    final ComplexData cdt = new ComplexData();
    cdt.getContent().add(new org.geotoolkit.wps.xml.v200.Format(encoding, mimeType, schema, null));
    if (candidate instanceof Document) {
        candidate = ((Document) candidate).getDocumentElement();
    }
    cdt.getContent().add(candidate);
    final Data data = new Data();
    data.getContent().add(cdt);
    final DataInput dit = new DataInput();
    dit.setData(data);
    return dit;
}
Also used : ReferenceProxy(org.geotoolkit.wps.xml.ReferenceProxy) Format(org.geotoolkit.wps.xml.v200.Format) DataInput(org.geotoolkit.wps.xml.v200.DataInput) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData) Data(org.geotoolkit.wps.xml.v200.Data) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData) Document(org.w3c.dom.Document)

Aggregations

ComplexData (org.geotoolkit.wps.xml.v200.ComplexData)10 Format (org.geotoolkit.wps.xml.v200.Format)10 DataInput (org.geotoolkit.wps.xml.v200.DataInput)8 Data (org.geotoolkit.wps.xml.v200.Data)7 ReferenceProxy (org.geotoolkit.wps.xml.ReferenceProxy)5 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)4 IOException (java.io.IOException)2 List (java.util.List)2 DataStoreException (org.apache.sis.storage.DataStoreException)2 Geometry (org.locationtech.jts.geom.Geometry)2 Document (org.w3c.dom.Document)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1