Search in sources :

Example 1 with DbaseFileException

use of org.geotoolkit.data.dbf.DbaseFileException 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

IOException (java.io.IOException)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 DbaseFileException (org.geotoolkit.data.dbf.DbaseFileException)1 FeatureStoreRuntimeException (org.geotoolkit.storage.feature.FeatureStoreRuntimeException)1 Envelope (org.locationtech.jts.geom.Envelope)1 Geometry (org.locationtech.jts.geom.Geometry)1 AttributeType (org.opengis.feature.AttributeType)1 PropertyNotFoundException (org.opengis.feature.PropertyNotFoundException)1