Search in sources :

Example 1 with XmlFeatureWriter

use of org.geotoolkit.feature.xml.XmlFeatureWriter in project geotoolkit by Geomatys.

the class FeatureToReferenceConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Reference convert(final Feature 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 directory should be defined.");
    }
    FeatureType ft = null;
    if (source instanceof Feature) {
        ft = source.getType();
    } else {
        throw new UnconvertibleObjectException("The requested output reference data is not an instance of Feature.");
    }
    Reference reference = new Reference();
    reference.setMimeType((String) params.get(MIME));
    reference.setEncoding((String) params.get(ENCODING));
    final String namespace = NamesExt.getNamespace(ft.getName());
    final Map<String, String> schemaLocation = new HashMap<>();
    final String randomFileName = UUID.randomUUID().toString();
    final String tmpDirUrl = (String) params.get(TMP_DIR_URL);
    if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(reference.getMimeType())) {
        // create file
        final Path dataFile = buildPath(params, randomFileName + ".json");
        try {
            try (OutputStream fos = Files.newOutputStream(dataFile);
                GeoJSONStreamWriter writer = new GeoJSONStreamWriter(fos, ft, WPSConvertersUtils.FRACTION_DIGITS)) {
                Feature next = writer.next();
                FeatureExt.copy(source, next, true);
                writer.write();
            }
        } catch (DataStoreException e) {
            throw new UnconvertibleObjectException("Can't write Feature into GeoJSON output stream.", e);
        } catch (IOException e) {
            throw new UnconvertibleObjectException(e);
        }
        final String relLoc = getRelativeLocation(dataFile, params);
        reference.setHref(tmpDirUrl + "/" + relLoc);
        reference.setSchema(null);
    } else if (WPSMimeType.APP_GML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(reference.getMimeType())) {
        // Write FeatureType
        try {
            reference.setSchema(WPSConvertersUtils.writeSchema(ft, params));
            schemaLocation.put(namespace, reference.getSchema());
        } catch (JAXBException ex) {
            throw new UnconvertibleObjectException("Can't write FeatureType into xsd schema.", ex);
        } catch (IOException ex) {
            throw new UnconvertibleObjectException("Can't create xsd schema file.", ex);
        }
        // Write Feature
        final XmlFeatureWriter featureWriter = new JAXPStreamFeatureWriter(schemaLocation);
        // create file
        final Path dataFile = buildPath(params, randomFileName + ".xml");
        try (final OutputStream dataStream = Files.newOutputStream(dataFile);
            final AutoCloseable xmlCloser = () -> featureWriter.dispose()) {
            // Write feature in file
            featureWriter.write(source, dataStream);
            final String relLoc = getRelativeLocation(dataFile, params);
            reference.setHref(tmpDirUrl + "/" + relLoc);
        } catch (XMLStreamException ex) {
            throw new UnconvertibleObjectException("Stax exception while writing the feature collection", ex);
        } catch (DataStoreException ex) {
            throw new UnconvertibleObjectException("FeatureStore exception while writing the feature collection", ex);
        } catch (FeatureStoreRuntimeException ex) {
            throw new UnconvertibleObjectException("FeatureStoreRuntimeException exception while writing the feature collection", ex);
        } catch (Exception ex) {
            throw new UnconvertibleObjectException(ex);
        }
    } else
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + reference.getMimeType());
    return reference;
}
Also used : Path(java.nio.file.Path) FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) HashMap(java.util.HashMap) Reference(org.geotoolkit.wps.xml.v200.Reference) OutputStream(java.io.OutputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) JAXPStreamFeatureWriter(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureWriter) Feature(org.opengis.feature.Feature) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) XMLStreamException(javax.xml.stream.XMLStreamException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) XMLStreamException(javax.xml.stream.XMLStreamException) XmlFeatureWriter(org.geotoolkit.feature.xml.XmlFeatureWriter) GeoJSONStreamWriter(org.geotoolkit.storage.geojson.GeoJSONStreamWriter) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException)

Aggregations

IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 JAXBException (javax.xml.bind.JAXBException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)1 XmlFeatureWriter (org.geotoolkit.feature.xml.XmlFeatureWriter)1 JAXPStreamFeatureWriter (org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureWriter)1 FeatureStoreRuntimeException (org.geotoolkit.storage.feature.FeatureStoreRuntimeException)1 GeoJSONStreamWriter (org.geotoolkit.storage.geojson.GeoJSONStreamWriter)1 Reference (org.geotoolkit.wps.xml.v200.Reference)1 Feature (org.opengis.feature.Feature)1 FeatureType (org.opengis.feature.FeatureType)1