Search in sources :

Example 21 with Data

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

the class NumberToReferenceConverter method convert.

@Override
public Reference convert(final Number source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (params.get(TMP_DIR_PATH) == null) {
        throw new UnconvertibleObjectException("The output directory should be defined.");
    }
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    }
    Reference reference = new Reference();
    reference.setMimeType((String) params.get(MIME));
    reference.setEncoding((String) params.get(ENCODING));
    reference.setSchema((String) params.get(SCHEMA));
    reference.setMimeType("text/plain");
    reference.setEncoding("UTF-8");
    reference.setSchema(null);
    final String randomFileName = UUID.randomUUID().toString();
    try {
        // create file
        final Path literalFile = buildPath(params, randomFileName);
        IOUtilities.writeString(String.valueOf(source), literalFile);
        final String relLoc = getRelativeLocation(literalFile, params);
        reference.setHref((String) params.get(TMP_DIR_URL) + "/" + relLoc);
    } catch (IOException ex) {
        throw new UnconvertibleObjectException("Error occurs during string writing.", ex);
    }
    return reference;
}
Also used : Path(java.nio.file.Path) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Reference(org.geotoolkit.wps.xml.v200.Reference) IOException(java.io.IOException)

Example 22 with Data

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

the class CoverageToComplexConverter method convert.

@Override
public Data convert(GridCoverage source, Map<String, Object> params) throws UnconvertibleObjectException {
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    } else if (params == null) {
        throw new UnconvertibleObjectException("Not enough information about data format");
    }
    final Object tmpMime = params.get(MIME);
    final String mime;
    if (tmpMime instanceof String) {
        mime = (String) tmpMime;
    } else {
        throw new UnconvertibleObjectException("No valid mime type given. We cannot determine output image format");
    }
    final WPSMimeType wpsMime = WPSMimeType.customValueOf((String) tmpMime);
    if (!wpsMime.equals(WPSMimeType.IMG_GEOTIFF) && !wpsMime.equals(WPSMimeType.IMG_GEOTIFF_BIS)) {
        throw new UnconvertibleObjectException("Only support GeoTiff Base64 encoding.");
    }
    final Object tmpEncoding = params.get(ENCODING);
    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        CoverageIO.write(source, "GEOTIFF", baos);
        baos.flush();
        byte[] bytesOut = baos.toByteArray();
        return new Data(new Format((String) ((tmpEncoding instanceof String) ? tmpEncoding : null), mime, null, null), Base64.getEncoder().encodeToString(bytesOut));
    } catch (DataStoreException ex) {
        throw new UnconvertibleObjectException(ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new UnconvertibleObjectException(ex.getMessage(), ex);
    }
}
Also used : UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) DataStoreException(org.apache.sis.storage.DataStoreException) Format(org.geotoolkit.wps.xml.v200.Format) WPSMimeType(org.geotoolkit.wps.io.WPSMimeType) Data(org.geotoolkit.wps.xml.v200.Data) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 23 with Data

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

the class CoverageToReferenceConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Reference convert(final GridCoverage source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (params.get(TMP_DIR_PATH) == null) {
        throw new UnconvertibleObjectException("The output directory should be defined.");
    }
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    }
    Reference reference = new Reference();
    final String encodingStr = (String) params.get(ENCODING);
    final String mimeStr = (String) params.get(MIME) != null ? (String) params.get(MIME) : WPSMimeType.IMG_GEOTIFF.val();
    final WPSMimeType mime = WPSMimeType.customValueOf(mimeStr);
    reference.setMimeType(mimeStr);
    reference.setEncoding(encodingStr);
    reference.setSchema((String) params.get(SCHEMA));
    final String formatName;
    final String[] formatNames = XImageIO.getFormatNamesByMimeType(mimeStr, true, true);
    formatName = (formatNames.length < 1) ? "GEOTIFF" : formatNames[0];
    final String randomFileName = UUID.randomUUID().toString();
    try {
        final Path imageFile = buildPath(params, randomFileName);
        if (encodingStr != null && encodingStr.equals(WPSEncoding.BASE64.getValue())) {
            try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                CoverageIO.write(source, formatName, baos);
                baos.flush();
                byte[] bytesOut = baos.toByteArray();
                IOUtilities.writeString(Base64.getEncoder().encodeToString(bytesOut), imageFile);
            }
        } else {
            // Note : do not do OutputStream out = Files.newOutputStream(imageFile, StandardOpenOption.CREATE, WRITE, TRUNCATE_EXISTING)
            // Most coverage writer do not support stream writing properly, it is better to work with a file.
            // This also avoid keeping large files in memory if byte buffer seeking is needed by the writer.
            Files.deleteIfExists(imageFile);
            CoverageIO.write(source, formatName, imageFile);
        }
        final String relLoc = getRelativeLocation(imageFile, params);
        reference.setHref((String) params.get(TMP_DIR_URL) + "/" + relLoc);
    } catch (IOException | DataStoreException ex) {
        throw new UnconvertibleObjectException("Error during writing the coverage in the output file.", ex);
    }
    return reference;
}
Also used : Path(java.nio.file.Path) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) DataStoreException(org.apache.sis.storage.DataStoreException) WPSMimeType(org.geotoolkit.wps.io.WPSMimeType) Reference(org.geotoolkit.wps.xml.v200.Reference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 24 with Data

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

the class FeatureTypeToComplexConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Data convert(final FeatureType source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    }
    final Data complex = new Data();
    final Object tmpEncoding = params == null ? null : params.get(ENCODING);
    if (tmpEncoding instanceof String) {
        complex.setEncoding((String) tmpEncoding);
    }
    complex.setMimeType(WPSMimeType.TEXT_GML.val());
    try {
        final JAXBFeatureTypeWriter xmlWriter = new JAXBFeatureTypeWriter();
        complex.getContent().add(xmlWriter.writeToElement(source));
    } catch (JAXBException | ParserConfigurationException ex) {
        throw new UnconvertibleObjectException("Can't write FeatureType into ResponseDocument.", ex);
    }
    return complex;
}
Also used : UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) JAXBException(javax.xml.bind.JAXBException) Data(org.geotoolkit.wps.xml.v200.Data) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) JAXBFeatureTypeWriter(org.geotoolkit.feature.xml.jaxb.JAXBFeatureTypeWriter)

Example 25 with Data

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

the class RenderedImageToComplexConverter method convert.

@Override
public Data convert(RenderedImage source, Map<String, Object> params) throws UnconvertibleObjectException {
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    } else if (params == null) {
        throw new UnconvertibleObjectException("Not enough information about data format");
    }
    final Object tmpMime = params.get(MIME);
    final String mime;
    if (tmpMime instanceof String) {
        mime = (String) tmpMime;
    } else {
        throw new UnconvertibleObjectException("No valid mime type given. We cannot determine output image format");
    }
    final Data complex = new Data();
    complex.setMimeType(mime);
    final Object tmpEncoding = params.get(ENCODING);
    if (tmpEncoding instanceof String) {
        complex.setEncoding((String) tmpEncoding);
    }
    final String formatName = mime.substring(mime.indexOf("/") + 1).toUpperCase();
    try {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(source, formatName, baos);
        baos.flush();
        byte[] bytesOut = baos.toByteArray();
        complex.getContent().add(Base64.getEncoder().encodeToString(bytesOut));
        baos.close();
    } catch (IOException ex) {
        throw new UnconvertibleObjectException(ex.getMessage(), ex);
    }
    return complex;
}
Also used : UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Data(org.geotoolkit.wps.xml.v200.Data) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

Data (org.geotoolkit.wps.xml.v200.Data)42 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)33 IOException (java.io.IOException)25 Test (org.junit.Test)19 Reference (org.geotoolkit.wps.xml.v200.Reference)16 Path (java.nio.file.Path)15 DataInput (org.geotoolkit.wps.xml.v200.DataInput)15 Format (org.geotoolkit.wps.xml.v200.Format)14 DataStoreException (org.apache.sis.storage.DataStoreException)12 HashMap (java.util.HashMap)11 ComplexData (org.geotoolkit.wps.xml.v200.ComplexData)11 JAXBException (javax.xml.bind.JAXBException)9 ReferenceProxy (org.geotoolkit.wps.xml.ReferenceProxy)9 Data (com.microsoft.applicationinsights.smoketest.schemav2.Data)8 Envelope (com.microsoft.applicationinsights.smoketest.schemav2.Envelope)8 Feature (org.opengis.feature.Feature)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 LiteralValue (org.geotoolkit.wps.xml.v200.LiteralValue)7 Geometry (org.locationtech.jts.geom.Geometry)7 DataOutput (org.geotoolkit.wps.xml.v200.DataOutput)6