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