Search in sources :

Example 16 with Data

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

the class FeatureToComplexConverterTest method testJSONConversion.

@Test
public void testJSONConversion() throws DataStoreException, IOException, URISyntaxException, FactoryException {
    // Get test resource
    final Object testResource = ConvertersTestUtils.loadTestResource("/inputs/feature.json");
    final Data complex = ConvertersTestUtils.initAndRunOutputConversion(Feature.class, Data.class, testResource, WPSMimeType.APP_GEOJSON.val(), WPSEncoding.UTF8.getValue());
    ConvertersTestUtils.assertFormatMatch(complex, WPSEncoding.UTF8.getValue(), WPSMimeType.APP_GEOJSON.val(), null);
    ConvertersTestUtils.useDataContentAsFile(complex, file -> {
        try {
            final Feature readFeature = WPSConvertersUtils.readFeatureFromJson(file.toUri());
            ConvertersTestUtils.assertFeatureIsValid(readFeature);
        } catch (DataStoreException | URISyntaxException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    });
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) Data(org.geotoolkit.wps.xml.v200.Data) UncheckedIOException(java.io.UncheckedIOException) URISyntaxException(java.net.URISyntaxException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 17 with Data

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

the class GeometryToComplexConverterTest method testJSONConversion.

@Test
public void testJSONConversion() throws DataStoreException, IOException, FactoryException, URISyntaxException {
    // Get test resource
    Geometry geometryResource = (Geometry) ConvertersTestUtils.loadTestResource("/inputs/geometry.json");
    ConvertersTestUtils.assertGeometryIsValid(geometryResource);
    Data complex = ConvertersTestUtils.initAndRunOutputConversion(Geometry.class, Data.class, geometryResource, WPSMimeType.APP_GEOJSON.val(), WPSEncoding.UTF8.getValue());
    // Test complex
    ConvertersTestUtils.assertFormatMatch(complex, WPSEncoding.UTF8.getValue(), WPSMimeType.APP_GEOJSON.val(), null);
    ConvertersTestUtils.useDataContentAsFile(complex, file -> {
        try {
            Geometry geometry = getGeometry(file);
            ConvertersTestUtils.assertGeometryIsValid(geometry);
        } catch (FactoryException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    });
}
Also used : GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) Geometry(org.locationtech.jts.geom.Geometry) FactoryException(org.opengis.util.FactoryException) Data(org.geotoolkit.wps.xml.v200.Data) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Test(org.junit.Test)

Example 18 with Data

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

the class FeatureCollectionToReferenceConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Reference convert(final FeatureCollection 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 (params.get(TMP_DIR_URL) == null) {
        throw new UnconvertibleObjectException("The output directory URL should be defined.");
    }
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    }
    // TODO : useless test, null test above is all we need, fix this and other converters
    if (!(source instanceof FeatureCollection)) {
        throw new UnconvertibleObjectException("The requested output data is not an instance of FeatureCollection.");
    }
    Reference reference = new Reference();
    reference.setMimeType((String) params.get(MIME));
    reference.setEncoding((String) params.get(ENCODING));
    final FeatureType ft = source.getType();
    final String namespace = NamesExt.getNamespace(ft.getName());
    final Map<String, String> schemaLocation = new HashMap<>();
    final String randomFileName = UUID.randomUUID().toString();
    if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(reference.getMimeType())) {
        // create file
        final Path dataFile = buildPath(params, randomFileName + ".json");
        try (OutputStream fos = Files.newOutputStream(dataFile, CREATE, TRUNCATE_EXISTING, WRITE);
            GeoJSONStreamWriter writer = new GeoJSONStreamWriter(fos, ft, WPSConvertersUtils.FRACTION_DIGITS);
            Stream<Feature> st = source.features(false)) {
            Iterator<Feature> iterator = st.iterator();
            while (iterator.hasNext()) {
                Feature next = iterator.next();
                Feature neww = writer.next();
                FeatureExt.copy(next, neww, false);
                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(params.get(TMP_DIR_URL) + "/" + 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())) {
        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 JAXPStreamFeatureWriter 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(params.get(TMP_DIR_URL) + "/" + 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) JAXBException(javax.xml.bind.JAXBException) JAXPStreamFeatureWriter(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureWriter) Feature(org.opengis.feature.Feature) XMLStreamException(javax.xml.stream.XMLStreamException) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) JAXBException(javax.xml.bind.JAXBException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) XMLStreamException(javax.xml.stream.XMLStreamException) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) GeoJSONStreamWriter(org.geotoolkit.storage.geojson.GeoJSONStreamWriter) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException)

Example 19 with Data

use of org.geotoolkit.wps.xml.v200.Data 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)

Example 20 with Data

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

the class GeometryToReferenceConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Reference convert(final Geometry 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 Geometry)) {
        throw new UnconvertibleObjectException("The geometry is not an JTS geometry.");
    }
    Reference reference = new Reference();
    reference.setMimeType((String) params.get(MIME));
    reference.setEncoding((String) params.get(ENCODING));
    reference.setSchema((String) params.get(SCHEMA));
    String gmlVersion = (String) params.get(GMLVERSION);
    if (gmlVersion == null) {
        gmlVersion = "3.1.1";
    }
    final String randomPathName = UUID.randomUUID().toString() + getFileExtension(reference.getMimeType());
    final Path geometryFile = buildPath(params, randomPathName);
    final String tmpDirUrl = (String) params.get(TMP_DIR_URL);
    OutputStream geometryStream = null;
    try {
        geometryStream = Files.newOutputStream(geometryFile);
        if (WPSMimeType.APP_GML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(reference.getMimeType()) || reference.getMimeType() == null) {
            // default to XML
            final Marshaller m = WPSMarshallerPool.getInstance().acquireMarshaller();
            m.marshal(JTStoGeometry.toGML(gmlVersion, source), geometryStream);
            final String relLoc = getRelativeLocation(geometryFile, params);
            reference.setHref(tmpDirUrl + '/' + relLoc.replace('\\', '/'));
            WPSMarshallerPool.getInstance().recycle(m);
        } else if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(reference.getMimeType())) {
            GeoJSONStreamWriter.writeSingleGeometry(geometryStream, source, JsonEncoding.UTF8, WPSConvertersUtils.FRACTION_DIGITS, true);
            final String relLoc = getRelativeLocation(geometryFile, params);
            reference.setHref(tmpDirUrl + '/' + relLoc.replace('\\', '/'));
        } else {
            throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + reference.getMimeType());
        }
    } catch (FactoryException ex) {
        throw new UnconvertibleObjectException("Can't convert the JTS geometry to OpenGIS.", ex);
    } catch (FileNotFoundException ex) {
        throw new UnconvertibleObjectException("Can't create output reference file.", ex);
    } catch (JAXBException ex) {
        throw new UnconvertibleObjectException("JAXB exception while writing the geometry", ex);
    } catch (IOException ex) {
        throw new UnconvertibleObjectException(ex);
    } finally {
        try {
            if (geometryStream != null)
                geometryStream.close();
        } catch (IOException ex) {
            throw new UnconvertibleObjectException("Can't close the output reference file stream.", ex);
        }
    }
    return reference;
}
Also used : JTStoGeometry(org.geotoolkit.gml.JTStoGeometry) Geometry(org.locationtech.jts.geom.Geometry) Path(java.nio.file.Path) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Marshaller(javax.xml.bind.Marshaller) FactoryException(org.opengis.util.FactoryException) Reference(org.geotoolkit.wps.xml.v200.Reference) JAXBException(javax.xml.bind.JAXBException)

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