Search in sources :

Example 6 with FeatureStoreRuntimeException

use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException 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 7 with FeatureStoreRuntimeException

use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.

the class PostgresVersioningTest method testTrimVersioning.

@Test
public void testTrimVersioning() throws DataStoreException, VersioningException {
    reload(true);
    List<Version> versions;
    Feature feature;
    ResourceId fid;
    Version v0;
    Version v1;
    Version v2;
    FeatureIterator ite;
    Query qb = new Query();
    // create table
    final FeatureType refType = FTYPE_SIMPLE;
    store.createFeatureType(refType);
    assertEquals(1, store.getNames().size());
    // get version control
    final VersionControl vc = store.getVersioning(refType.getName().toString());
    assertNotNull(vc);
    assertTrue(vc.isEditable());
    assertFalse(vc.isVersioned());
    // start versioning /////////////////////////////////////////////////////
    vc.startVersioning();
    assertTrue(vc.isVersioned());
    versions = vc.list();
    assertTrue(versions.isEmpty());
    // make an insert ///////////////////////////////////////////////////////
    final Point firstPoint = GF.createPoint(new Coordinate(56, 45));
    feature = refType.newInstance();
    feature.setPropertyValue("id", "0");
    feature.setPropertyValue("boolean", Boolean.TRUE);
    feature.setPropertyValue("integer", 14);
    feature.setPropertyValue("point", firstPoint);
    feature.setPropertyValue("string", "someteststring");
    store.addFeatures(refType.getName().toString(), Collections.singleton(feature));
    // we should have one version
    versions = vc.list();
    assertEquals(1, versions.size());
    // get identifier
    // ensure normal reading is correct without version----------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    try {
        // wait a bit just to have some space between version dates
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        fail(ex.getMessage());
    }
    // make an update ///////////////////////////////////////////////////////
    final Point secondPoint = GF.createPoint(new Coordinate(-12, 21));
    final Map<String, Object> updates = new HashMap<>();
    updates.put("boolean", Boolean.FALSE);
    updates.put("integer", -3);
    updates.put("point", secondPoint);
    updates.put("string", "anothertextupdated");
    store.updateFeatures(refType.getName().toString(), fid, updates);
    try {
        // wait a bit just to have some space between version dates
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        fail(ex.getMessage());
    }
    // make a 2nd update ///////////////////////////////////////////////////////
    final Point thirdPoint = GF.createPoint(new Coordinate(145, -221));
    final Map<String, Object> updates2 = new HashMap<>();
    updates2.put("boolean", Boolean.FALSE);
    updates2.put("integer", 150);
    updates2.put("point", thirdPoint);
    updates2.put("string", "secondtextupdated");
    store.updateFeatures(refType.getName().toString(), fid, updates2);
    // get all versions organized in increase dates order.
    versions = vc.list();
    assertEquals(3, versions.size());
    v0 = versions.get(0);
    v1 = versions.get(1);
    v2 = versions.get(2);
    /* first trim between v1 date and v2 date (named middle date) to verify
         * deletion of first version and update v1 date at trim date.*/
    final Date middle = new Date((v1.getDate().getTime() + v2.getDate().getTime()) >> 1);
    vc.trim(middle);
    versions = vc.list();
    assertEquals(2, versions.size());
    // ensure version 0 does not exist
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v0.getLabel());
    try {
        store.createSession(true).getFeatureCollection(qb).isEmpty();
        fail("should not find version");
    } catch (FeatureStoreRuntimeException ex) {
    // ok
    }
    // ensure version v1 begin at middle date.
    assertEquals(vc.list().get(0).getDate().getTime(), middle.getTime());
    /* second trim at exactely the begining of the third version to verify,
         * deletion of second version and third version existence.*/
    vc.trim(v2);
    versions = vc.list();
    assertEquals(1, versions.size());
    // ensure version 1 does not exist
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v1.getLabel());
    try {
        store.createSession(true).getFeatureCollection(qb).isEmpty();
        fail("should not find version");
    } catch (FeatureStoreRuntimeException ex) {
    // ok
    }
    // ensure version v2 begin time doesn't change.
    assertEquals(vc.list().get(0).getDate().getTime(), v2.getDate().getTime());
    /* third trim just after v3 version date, to verify that v3
         * version date become trim date */
    final long lastDate = v2.getDate().getTime() + 400;
    vc.trim(new Date(lastDate));
    versions = vc.list();
    assertEquals(1, versions.size());
    // ensure version v2 begin time become lastDate.
    assertEquals(vc.list().get(0).getDate().getTime(), lastDate);
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) HashMap(java.util.HashMap) Point(org.locationtech.jts.geom.Point) Feature(org.opengis.feature.Feature) VersionControl(org.geotoolkit.version.VersionControl) Date(java.util.Date) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Version(org.geotoolkit.version.Version) ResourceId(org.opengis.filter.ResourceId) Coordinate(org.locationtech.jts.geom.Coordinate) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) Test(org.junit.Test)

Example 8 with FeatureStoreRuntimeException

use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.

the class PostgresVersioningTest method testRevertVersioning.

@Test
public void testRevertVersioning() throws DataStoreException, VersioningException {
    reload(true);
    List<Version> versions;
    Feature feature;
    ResourceId fid;
    Version v0;
    Version v1;
    Version v2;
    FeatureIterator ite;
    Query qb = new Query();
    // create table
    final FeatureType refType = FTYPE_SIMPLE;
    store.createFeatureType(refType);
    assertEquals(1, store.getNames().size());
    // get version control
    final VersionControl vc = store.getVersioning(refType.getName().toString());
    assertNotNull(vc);
    assertTrue(vc.isEditable());
    assertFalse(vc.isVersioned());
    // start versioning /////////////////////////////////////////////////////
    vc.startVersioning();
    assertTrue(vc.isVersioned());
    versions = vc.list();
    assertTrue(versions.isEmpty());
    // make an insert ///////////////////////////////////////////////////////
    final Point firstPoint = GF.createPoint(new Coordinate(56, 45));
    feature = refType.newInstance();
    feature.setPropertyValue("id", "0");
    feature.setPropertyValue("boolean", Boolean.TRUE);
    feature.setPropertyValue("integer", 14);
    feature.setPropertyValue("point", firstPoint);
    feature.setPropertyValue("string", "someteststring");
    store.addFeatures(refType.getName().toString(), Collections.singleton(feature));
    // we should have one version
    versions = vc.list();
    assertEquals(1, versions.size());
    // get identifier
    // ensure normal reading is correct without version----------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    try {
        // wait a bit just to have some space between version dates
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        fail(ex.getMessage());
    }
    // make an update ///////////////////////////////////////////////////////
    final Point secondPoint = GF.createPoint(new Coordinate(-12, 21));
    final Map<String, Object> updates = new HashMap<>();
    updates.put("boolean", Boolean.FALSE);
    updates.put("integer", -3);
    updates.put("point", secondPoint);
    updates.put("string", "anothertextupdated");
    store.updateFeatures(refType.getName().toString(), fid, updates);
    try {
        // wait a bit just to have some space between version dates
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        fail(ex.getMessage());
    }
    // make a remove ///////////////////////////////////////////////////////
    store.removeFeatures(refType.getName().toString(), fid);
    // ensure test table is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store.createSession(true).getFeatureCollection(qb).isEmpty());
    // get all versions organized in increase dates order.
    versions = vc.list();
    assertEquals(3, versions.size());
    v0 = versions.get(0);
    v1 = versions.get(1);
    v2 = versions.get(2);
    /* first revert between v1 date and v2 date (named middle date) to verify
         * re - insertion of feature in the original base and update v2 ending date become null.*/
    // =/2
    final Date middle = new Date((v1.getDate().getTime() + v2.getDate().getTime()) >> 1);
    vc.revert(middle);
    versions = vc.list();
    assertEquals(2, versions.size());
    // ensure version v2 does not exist
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v2.getLabel());
    try {
        store.createSession(true).getFeatureCollection(qb).isEmpty();
        fail("should not find version");
    } catch (FeatureStoreRuntimeException ex) {
    // ok
    }
    // ensure test table contain feature from version 1
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertFalse(store.createSession(true).getFeatureCollection(qb).isEmpty());
    Feature featV1;
    Feature feat;
    // feature from test base result.
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feat = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    // feature from version v1.
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v1.getLabel());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        featV1 = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    assertTrue(feat.getProperty("boolean").equals(featV1.getProperty("boolean")));
    assertTrue(feat.getProperty("integer").equals(featV1.getProperty("integer")));
    assertTrue(feat.getProperty("point").equals(featV1.getProperty("point")));
    assertTrue(feat.getProperty("string").equals(featV1.getProperty("string")));
    /* second revert at v0 begin date to verify update roll back, and verify
         * feature update from history table into original base.*/
    vc.revert(v0.getDate());
    versions = vc.list();
    assertEquals(1, versions.size());
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v1.getLabel());
    try {
        store.createSession(true).getFeatureCollection(qb).isEmpty();
        fail("should not find version");
    } catch (FeatureStoreRuntimeException ex) {
    // ok
    }
    // ensure test table contain feature from version 1
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertFalse(store.createSession(true).getFeatureCollection(qb).isEmpty());
    // feature from test base result.
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feat = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    // feature from version v1.
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v0.getLabel());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        featV1 = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    assertTrue(feat.getProperty("boolean").equals(featV1.getProperty("boolean")));
    assertTrue(feat.getProperty("integer").equals(featV1.getProperty("integer")));
    assertTrue(feat.getProperty("point").equals(featV1.getProperty("point")));
    assertTrue(feat.getProperty("string").equals(featV1.getProperty("string")));
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) HashMap(java.util.HashMap) Point(org.locationtech.jts.geom.Point) Feature(org.opengis.feature.Feature) VersionControl(org.geotoolkit.version.VersionControl) Date(java.util.Date) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Version(org.geotoolkit.version.Version) ResourceId(org.opengis.filter.ResourceId) Coordinate(org.locationtech.jts.geom.Coordinate) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) Test(org.junit.Test)

Example 9 with FeatureStoreRuntimeException

use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.

the class ShapefileFeatureWriter method next.

/**
 * {@inheritDoc }
 */
@Override
public Feature next() throws FeatureStoreRuntimeException {
    // closed already, error!
    if (featureReader == null) {
        throw new FeatureStoreRuntimeException("Writer closed");
    }
    // we have to write the current feature back into the stream
    if (currentFeature != null) {
        write();
    }
    // is there another? If so, return it
    if (featureReader.hasNext()) {
        try {
            currentFeature = featureReader.next();
            originalFeature = FeatureExt.copy(currentFeature);
            return currentFeature;
        } catch (IllegalArgumentException iae) {
            throw new FeatureStoreRuntimeException("Error in reading", iae);
        }
    }
    // so return an empty feature
    try {
        final String featureID = getFeatureType().getName().tip() + "." + (records + 1);
        originalFeature = null;
        currentFeature = getFeatureType().newInstance();
        currentFeature.setPropertyValue(AttributeConvention.IDENTIFIER, featureID);
        return currentFeature;
    } catch (IllegalArgumentException iae) {
        throw new FeatureStoreRuntimeException("Error creating empty Feature", iae);
    }
}
Also used : FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException)

Example 10 with FeatureStoreRuntimeException

use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.

the class ShapefileFeatureWriter method write.

/**
 * {@inheritDoc }
 */
@Override
public void write() throws FeatureStoreRuntimeException {
    if (currentFeature == null) {
        throw new FeatureStoreRuntimeException("Current feature is null");
    }
    if (featureReader == null) {
        throw new FeatureStoreRuntimeException("Writer closed");
    }
    // writing of Geometry
    Geometry g = FeatureExt.getDefaultGeometryValue(currentFeature).filter(Geometry.class::isInstance).map(Geometry.class::cast).orElse(null);
    // if this is the first Geometry, find the shapeType and handler
    if (shapeType == null) {
        int dims = 2;
        if (g != null) {
            dims = JTSUtilities.guessCoorinateDims(g.getCoordinates());
        }
        try {
            shapeType = JTSUtilities.getShapeType(g, dims);
            // we must go back and annotate this after writing
            shpWriter.writeHeaders(new Envelope(), shapeType, 0, 0);
            handler = shapeType.getShapeHandler(true);
        } catch (Exception se) {
            throw new FeatureStoreRuntimeException("Unexpected Error", se);
        }
    }
    // convert geometry
    g = JTSUtilities.convertToCollection(g, shapeType);
    // bounds calculations
    Envelope b = g.getEnvelopeInternal();
    if (!b.isNull()) {
        bounds.expandToInclude(b);
    }
    // file length update
    shapefileLength += (handler.getLength(g) + 8);
    try {
        // write it
        shpWriter.writeGeometry(g);
    } catch (IOException ex) {
        throw new FeatureStoreRuntimeException(ex);
    }
    // writing of attributes
    int idx = 0;
    List<AttributeType> attributes = parent.getAttributes(featureType, false);
    for (int i = 0, ii = attributes.size(); i < ii; i++) {
        // skip geometries
        if (writeFlags[i] > 0) {
            transferCache[idx++] = currentFeature.getPropertyValue(attributes.get(i).getName().toString());
        }
    }
    try {
        dbfWriter.write(transferCache);
    } catch (IOException ex) {
        throw new FeatureStoreRuntimeException(ex);
    } catch (DbaseFileException ex) {
        throw new FeatureStoreRuntimeException(ex);
    }
    // one more down...
    records++;
    if (originalFeature == null) {
        addedIds.add(FeatureExt.getId(currentFeature));
    } else if (!originalFeature.equals(currentFeature)) {
        updatedIds.add(FeatureExt.getId(currentFeature));
    }
    // clear the currentFeature
    currentFeature = null;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) AttributeType(org.opengis.feature.AttributeType) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) DbaseFileException(org.geotoolkit.data.dbf.DbaseFileException) IOException(java.io.IOException) Envelope(org.locationtech.jts.geom.Envelope) DbaseFileException(org.geotoolkit.data.dbf.DbaseFileException) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) IOException(java.io.IOException) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException)

Aggregations

FeatureStoreRuntimeException (org.geotoolkit.storage.feature.FeatureStoreRuntimeException)35 DataStoreException (org.apache.sis.storage.DataStoreException)21 IOException (java.io.IOException)17 Feature (org.opengis.feature.Feature)16 HashMap (java.util.HashMap)8 FeatureType (org.opengis.feature.FeatureType)8 PropertyNotFoundException (org.opengis.feature.PropertyNotFoundException)5 PropertyType (org.opengis.feature.PropertyType)5 Path (java.nio.file.Path)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 FeatureSet (org.apache.sis.storage.FeatureSet)4 JAXPStreamFeatureWriter (org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureWriter)4 Query (org.geotoolkit.storage.feature.query.Query)4 AttributeType (org.opengis.feature.AttributeType)4 ResourceId (org.opengis.filter.ResourceId)4 Date (java.util.Date)3 JAXBException (javax.xml.bind.JAXBException)3 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)3 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)3 GeoJSONStreamWriter (org.geotoolkit.storage.geojson.GeoJSONStreamWriter)3