Search in sources :

Example 1 with WPSMimeType

use of org.geotoolkit.wps.io.WPSMimeType 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 2 with WPSMimeType

use of org.geotoolkit.wps.io.WPSMimeType 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)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 DataStoreException (org.apache.sis.storage.DataStoreException)2 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)2 WPSMimeType (org.geotoolkit.wps.io.WPSMimeType)2 Path (java.nio.file.Path)1 Data (org.geotoolkit.wps.xml.v200.Data)1 Format (org.geotoolkit.wps.xml.v200.Format)1 Reference (org.geotoolkit.wps.xml.v200.Reference)1