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;
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations