Search in sources :

Example 36 with Reference

use of com.google.api.expr.v1alpha1.Reference in project geotoolkit by Geomatys.

the class FileToReferenceConverter method convert.

@Override
public Reference convert(File source, 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));
    final Path targetDirectory = buildPath(params, null);
    final String tmpDir = getTemproraryDirectoryPath(params);
    final String tmpDirUrl = (String) params.get(TMP_DIR_URL);
    try {
        // if the source is already in the temp folder
        if (source.getAbsolutePath().startsWith(tmpDir)) {
            reference.setHref(source.getAbsolutePath().replace(tmpDir, tmpDirUrl));
        // else we create a link from the source file in temp dir
        } else {
            final Path target = targetDirectory.resolve(source.getName());
            Files.createSymbolicLink(target, source.toPath());
            // IOUtilities.copy(source.toPath(),target);
            String suffix = getRelativeLocation(target, tmpDir);
            reference.setHref(tmpDirUrl + "/" + suffix);
        }
    } catch (IOException ex) {
        throw new UnconvertibleObjectException("Error during moving file to output directory.", 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 37 with Reference

use of com.google.api.expr.v1alpha1.Reference in project geotoolkit by Geomatys.

the class GeometryArrayToReferenceConverter method convert.

@Override
public Reference convert(Geometry[] source, 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));
    final String randomFilename = UUID.randomUUID().toString() + ".json";
    final Path geometryArrayFile = buildPath(params, randomFilename);
    try {
        if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(reference.getMimeType())) {
            try (OutputStream geometryStream = Files.newOutputStream(geometryArrayFile)) {
                GeometryFactory geometryFactory = org.geotoolkit.geometry.jts.JTS.getFactory();
                Geometry toWrite = new GeometryCollection(source, geometryFactory);
                GeoJSONStreamWriter.writeSingleGeometry(geometryStream, toWrite, JsonEncoding.UTF8, WPSConvertersUtils.FRACTION_DIGITS, true);
            }
            reference.setHref(geometryArrayFile.toUri().toURL().toString());
            return reference;
        } else
            throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + reference.getMimeType());
    } catch (FileNotFoundException ex) {
        throw new UnconvertibleObjectException("Unable to find the reference file");
    } catch (IOException ex) {
        throw new UnconvertibleObjectException(ex);
    }
}
Also used : Path(java.nio.file.Path) Geometry(org.locationtech.jts.geom.Geometry) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Reference(org.geotoolkit.wps.xml.v200.Reference) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 38 with Reference

use of com.google.api.expr.v1alpha1.Reference in project geotoolkit by Geomatys.

the class RenderedImageToReferenceConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Reference convert(final RenderedImage 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.");
    }
    if (!(source instanceof BufferedImage) && !(source instanceof RenderedImage)) {
        throw new UnconvertibleObjectException("The output data is not an instance of RenderedImage.");
    }
    Reference reference = new Reference();
    final String encoding = (String) params.get(ENCODING);
    final String mime = (String) params.get(MIME) != null ? (String) params.get(MIME) : "image/png";
    final String formatName = mime.substring(mime.indexOf("/") + 1).toUpperCase();
    reference.setMimeType(mime);
    reference.setEncoding((String) params.get(ENCODING));
    reference.setSchema((String) params.get(SCHEMA));
    // try to find a suffix file
    String suffix = "";
    ImageReaderSpi spi = XImageIO.getReaderSpiByFormatName(formatName);
    if (spi != null) {
        String[] suffixes = spi.getFileSuffixes();
        if (suffixes != null && suffixes.length != 0) {
            suffix = suffixes[0];
        }
    }
    final String randomFileName = UUID.randomUUID().toString() + suffix;
    try {
        final Path imageFile = buildPath(params, randomFileName);
        if (encoding != null && encoding.equals(WPSEncoding.BASE64.getValue())) {
            try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                ImageIO.write(source, formatName, baos);
                baos.flush();
                byte[] bytesOut = baos.toByteArray();
                IOUtilities.writeString(Base64.getEncoder().encodeToString(bytesOut), imageFile);
            }
        } else {
            try (OutputStream out = Files.newOutputStream(imageFile, StandardOpenOption.CREATE, WRITE, TRUNCATE_EXISTING)) {
                ImageIO.write(source, formatName, out);
            }
        }
        final String relLoc = getRelativeLocation(imageFile, params);
        reference.setHref((String) params.get(TMP_DIR_URL) + "/" + relLoc);
    } catch (IOException ex) {
        throw new UnconvertibleObjectException("Error occured during image writing.", ex);
    }
    return reference;
}
Also used : Path(java.nio.file.Path) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) Reference(org.geotoolkit.wps.xml.v200.Reference) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RenderedImage(java.awt.image.RenderedImage) BufferedImage(java.awt.image.BufferedImage)

Example 39 with Reference

use of com.google.api.expr.v1alpha1.Reference in project geotoolkit by Geomatys.

the class StringToReferenceConverter method convert.

@Override
public Reference convert(final String 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 mime = (params.get(MIME) == null) ? "text/plain" : (String) params.get(MIME);
    final String encoding = (params.get(ENCODING) == null) ? "UTF-8" : (String) params.get(ENCODING);
    reference.setMimeType(mime);
    reference.setEncoding(encoding);
    reference.setSchema((String) params.get(SCHEMA));
    final String randomFileName = UUID.randomUUID().toString();
    try {
        // create file
        final Path literalFile = buildPath(params, randomFileName);
        IOUtilities.writeString(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 image 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 40 with Reference

use of com.google.api.expr.v1alpha1.Reference in project geotoolkit by Geomatys.

the class StyledLayerDescriptorToReferenceConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Reference convert(final StyledLayerDescriptor 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 source is not defined.");
    }
    Reference reference = new Reference();
    reference.setMimeType((String) params.get(MIME));
    reference.setEncoding((String) params.get(ENCODING));
    reference.setSchema((String) params.get(SCHEMA));
    if (WPSMimeType.APP_SLD.val().equalsIgnoreCase(reference.getMimeType())) {
        // create file
        final Path dataFile = buildPath(params, UUID.randomUUID().toString() + ".sld");
        try {
            StyleXmlIO xmlio = new StyleXmlIO();
            xmlio.writeSLD(dataFile, source, Specification.StyledLayerDescriptor.V_1_1_0);
        } catch (JAXBException e) {
            throw new UnconvertibleObjectException(e.getMessage(), e);
        }
        final String relLoc = getRelativeLocation(dataFile, params);
        reference.setHref(params.get(TMP_DIR_URL) + "/" + relLoc);
    } else {
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + reference.getMimeType());
    }
    return reference;
}
Also used : Path(java.nio.file.Path) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Reference(org.geotoolkit.wps.xml.v200.Reference) JAXBException(javax.xml.bind.JAXBException) StyleXmlIO(org.geotoolkit.sld.xml.StyleXmlIO)

Aggregations

Reference (org.geotoolkit.wps.xml.v200.Reference)34 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)17 Path (java.nio.file.Path)14 Test (org.junit.Test)11 IOException (java.io.IOException)9 URL (java.net.URL)9 HashMap (java.util.HashMap)9 JAXBException (javax.xml.bind.JAXBException)6 AbstractWPSConverterTest (org.geotoolkit.wps.converters.AbstractWPSConverterTest)6 ArrayList (java.util.ArrayList)5 Feature (org.opengis.feature.Feature)5 BufferedReader (java.io.BufferedReader)4 Geometry (org.locationtech.jts.geom.Geometry)4 Type (com.google.api.expr.v1alpha1.Type)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 DataStoreException (org.apache.sis.storage.DataStoreException)3 Reference (org.dishevelled.bio.assembly.gfa1.Reference)3 Traversal (org.dishevelled.bio.assembly.gfa1.Traversal)3 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)2 Decl (com.google.api.expr.v1alpha1.Decl)2