use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.
the class FeatureStreamsTest method testTransformFeatureIterator.
@Test
public void testTransformFeatureIterator() throws DataStoreException {
FeatureType originalType = buildOriginalFT();
final FeatureTypeBuilder builder = new FeatureTypeBuilder();
final GenericName name = NamesExt.create("http://test.com", "TestSchema");
builder.setName(name);
builder.addAttribute(String.class).setName(AttributeConvention.IDENTIFIER_PROPERTY);
builder.addAttribute(LineString.class).setName("att_geom").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
final FeatureType type = builder.build();
final LineString geom = GF.createLineString(new Coordinate[] { new Coordinate(0, 0), // dx 15 , dy 12
new Coordinate(15, 12), // dx 7 , dy 16
new Coordinate(8, 28), // dx 1 , dy 3
new Coordinate(9, 31), // dx 14 , dy 20
new Coordinate(-5, 11), // dx 4 , dy 2
new Coordinate(-1, 9) });
final FeatureCollection collection = FeatureStoreUtilities.collection("id", type);
Feature sf = type.newInstance();
sf.setPropertyValue("att_geom", geom);
collection.add(sf);
// get the reader -------------------------------------------------------
Query query = new Query(originalType.getName());
FeatureReader reader = collection.getSession().getFeatureStore().getFeatureReader(query);
// create the decimate reader -------------------------------------------
final Hints hints = new Hints();
GeometryTransformer decim = new GeometryScaleTransformer(10, 10);
final TransformMapper ttype = new TransformMapper(reader.getFeatureType(), decim);
FeatureReader retyped = FeatureStreams.decorate(reader, ttype, hints);
assertTrue(retyped.hasNext());
LineString decimated = (LineString) retyped.next().getPropertyValue(AttributeConvention.GEOMETRY);
assertFalse(retyped.hasNext());
retyped.close();
assertEquals(4, decimated.getNumPoints());
assertEquals(geom.getGeometryN(0).getCoordinate(), decimated.getGeometryN(0).getCoordinate());
assertEquals(geom.getGeometryN(1).getCoordinate(), decimated.getGeometryN(1).getCoordinate());
assertEquals(geom.getGeometryN(2).getCoordinate(), decimated.getGeometryN(2).getCoordinate());
assertEquals(geom.getGeometryN(4).getCoordinate(), decimated.getGeometryN(3).getCoordinate());
// check the original geometry has not been modified
reader = collection.getSession().getFeatureStore().getFeatureReader(query);
assertTrue(reader.hasNext());
LineString notDecimated = (LineString) reader.next().getPropertyValue(AttributeConvention.GEOMETRY);
assertEquals(6, notDecimated.getNumPoints());
assertFalse(reader.hasNext());
reader.close();
// same test but with reuse hint ---------------------------------------
reader = collection.getSession().getFeatureStore().getFeatureReader(query);
decim = new GeometryScaleTransformer(10, 10);
retyped = FeatureStreams.decorate(reader, new TransformMapper(reader.getFeatureType(), decim), hints);
assertTrue(retyped.hasNext());
decimated = (LineString) retyped.next().getPropertyValue(AttributeConvention.GEOMETRY);
assertFalse(retyped.hasNext());
retyped.close();
assertEquals(4, decimated.getNumPoints());
assertEquals(geom.getGeometryN(0).getCoordinate(), decimated.getGeometryN(0).getCoordinate());
assertEquals(geom.getGeometryN(1).getCoordinate(), decimated.getGeometryN(1).getCoordinate());
assertEquals(geom.getGeometryN(2).getCoordinate(), decimated.getGeometryN(2).getCoordinate());
assertEquals(geom.getGeometryN(4).getCoordinate(), decimated.getGeometryN(3).getCoordinate());
// check the original geometry has not been modified
reader = collection.getSession().getFeatureStore().getFeatureReader(query);
assertTrue(reader.hasNext());
notDecimated = (LineString) reader.next().getPropertyValue(AttributeConvention.GEOMETRY);
assertEquals(6, notDecimated.getNumPoints());
assertFalse(reader.hasNext());
reader.close();
}
use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.
the class ReferenceToFeatureCollectionConverterTest method testJSONConversion.
@Test
public void testJSONConversion() throws IOException, DataStoreException {
final FeatureCollection featureCollection = ConvertersTestUtils.initAndRunInputConversion(Reference.class, FeatureCollection.class, "/inputs/featurecollection.json", WPSMimeType.APP_GEOJSON.val(), WPSEncoding.UTF8.getValue(), null);
// Test the feature collection
ConvertersTestUtils.assertFeatureCollectionIsValid(featureCollection);
}
use of org.geotoolkit.storage.feature.FeatureCollection 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;
}
use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.
the class PostgresComplexTypeTest method testComplexInsert.
/**
* 2 level depths feature test.
*/
@Test
public void testComplexInsert() throws DataStoreException, VersioningException {
reload(false);
final GeometryFactory gf = JTS.getFactory();
store.createFeatureType(FTYPE_COMPLEX);
final FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
final Feature voyage = resType.newInstance();
voyage.setPropertyValue("identifier", 120l);
final Feature driver = FTYPE_DRIVER.newInstance();
driver.setPropertyValue("name", "jean-michel");
driver.setPropertyValue("code", "BHF:123456");
voyage.setPropertyValue("driver", driver);
final Feature stop1 = FTYPE_STOP.newInstance();
stop1.setPropertyValue("location", gf.createPoint(new Coordinate(-10, 60)));
stop1.setPropertyValue("time", new Date(5000000));
final Feature stop2 = FTYPE_STOP.newInstance();
stop2.setPropertyValue("location", gf.createPoint(new Coordinate(30, 15)));
stop2.setPropertyValue("time", new Date(6000000));
final Feature stop3 = FTYPE_STOP.newInstance();
stop3.setPropertyValue("location", gf.createPoint(new Coordinate(40, -70)));
stop3.setPropertyValue("time", new Date(7000000));
voyage.setPropertyValue("stops", Arrays.asList(stop1, stop2, stop3));
List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(voyage));
assertEquals(1, addedIds.size());
assertEquals(FF.resourceId("Voyage.1"), addedIds.get(0));
final Session session = store.createSession(false);
final FeatureCollection col = session.getFeatureCollection(new Query(resType.getName()));
assertEquals(1, col.size());
final FeatureIterator ite = col.iterator();
try {
final Feature resFeature = ite.next();
assertNotNull(resFeature);
assertEquals(120l, resFeature.getPropertyValue("identifier"));
final Feature resDriver = (Feature) resFeature.getProperty("driver");
assertEquals("jean-michel", resDriver.getPropertyValue("name"));
assertEquals("BHF:123456", resDriver.getPropertyValue("code"));
final Collection<Feature> stops = (Collection<Feature>) resFeature.getPropertyValue("stops");
assertEquals(3, stops.size());
final boolean[] found = new boolean[3];
for (Feature stop : stops) {
final Timestamp time = (Timestamp) stop.getPropertyValue("time");
final Point location = (Point) stop.getPropertyValue("location");
if (time.getTime() == 5000000) {
assertEquals(stop1.getPropertyValue("location"), location);
found[0] = true;
} else if (time.getTime() == 6000000) {
assertEquals(stop2.getPropertyValue("location"), location);
found[1] = true;
} else if (time.getTime() == 7000000) {
assertEquals(stop3.getPropertyValue("location"), location);
found[2] = true;
} else {
fail("Unexpected property \n" + stop);
}
}
for (boolean b : found) assertTrue(b);
} finally {
ite.close();
}
}
use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.
the class PostgresSimpleTypeTest method testSimpleInsert.
@Test
public void testSimpleInsert() throws DataStoreException, VersioningException {
reload(true);
store.createFeatureType(FTYPE_SIMPLE);
FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
Feature feature = resType.newInstance();
feature.setPropertyValue("boolean", true);
// byte type do not exist, it's converted to smallint/int2
feature.setPropertyValue("byte", (short) 45);
feature.setPropertyValue("short", (short) 963);
feature.setPropertyValue("integer", 123456);
feature.setPropertyValue("long", 456789l);
feature.setPropertyValue("float", 7.3f);
feature.setPropertyValue("double", 14.5);
feature.setPropertyValue("string", "a string");
List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(feature));
assertEquals(1, addedIds.size());
assertEquals(FF.resourceId("1"), addedIds.get(0));
Session session = store.createSession(false);
FeatureCollection col = session.getFeatureCollection(new Query(resType.getName()));
assertEquals(1, col.size());
FeatureIterator ite = col.iterator();
try {
final Feature resFeature = ite.next();
assertNotNull(resFeature);
assertEquals(true, resFeature.getPropertyValue("boolean"));
assertEquals((short) 45, resFeature.getPropertyValue("byte"));
assertEquals((short) 963, resFeature.getPropertyValue("short"));
assertEquals(123456, resFeature.getPropertyValue("integer"));
assertEquals(456789l, resFeature.getPropertyValue("long"));
assertEquals(7.3f, resFeature.getPropertyValue("float"));
assertEquals(14.5d, resFeature.getPropertyValue("double"));
assertEquals("a string", resFeature.getPropertyValue("string"));
} finally {
ite.close();
}
// SECOND TEST for NAN values ------------------------------------------
reload(true);
store.createFeatureType(FTYPE_SIMPLE);
resType = store.getFeatureType(store.getNames().iterator().next().toString());
feature = resType.newInstance();
feature.setPropertyValue("fid", 0);
feature.setPropertyValue("boolean", true);
feature.setPropertyValue("byte", (short) 45);
feature.setPropertyValue("short", (short) 963);
feature.setPropertyValue("integer", 123456);
feature.setPropertyValue("long", 456789l);
feature.setPropertyValue("float", Float.NaN);
feature.setPropertyValue("double", Double.NaN);
feature.setPropertyValue("string", "a string");
addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(feature));
assertEquals(1, addedIds.size());
assertEquals(FF.resourceId("1"), addedIds.get(0));
session = store.createSession(false);
col = session.getFeatureCollection(new Query(resType.getName()));
assertEquals(1, col.size());
ite = col.iterator();
try {
final Feature resFeature = ite.next();
assertNotNull(resFeature);
assertEquals(true, resFeature.getPropertyValue("boolean"));
assertEquals((short) 45, resFeature.getPropertyValue("byte"));
assertEquals((short) 963, resFeature.getPropertyValue("short"));
assertEquals(123456, resFeature.getPropertyValue("integer"));
assertEquals(456789l, resFeature.getPropertyValue("long"));
assertEquals(Float.NaN, resFeature.getPropertyValue("float"));
assertEquals(Double.NaN, resFeature.getPropertyValue("double"));
assertEquals("a string", resFeature.getPropertyValue("string"));
} finally {
ite.close();
}
}
Aggregations