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