use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class FeatureStoreUtilitiesTest method testGetEnvelope.
@Test
public void testGetEnvelope() throws DataStoreException {
final GeometryFactory gf = org.geotoolkit.geometry.jts.JTS.getFactory();
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("Candidate");
ftb.addAttribute(Geometry.class).setName("geom").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
final FeatureType type = ftb.build();
final Feature f1 = type.newInstance();
f1.setPropertyValue("geom", gf.createPoint(new Coordinate(10, 20)));
final Feature f2 = type.newInstance();
f2.setPropertyValue("geom", gf.createPoint(new Coordinate(-30, -40)));
final FeatureSet fs = new InMemoryFeatureSet(type, Arrays.asList(f1, f2));
final Envelope envelope = FeatureStoreUtilities.getEnvelope(fs);
final GeneralEnvelope expected = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
expected.setRange(0, -30, 10);
expected.setRange(1, -40, 20);
Assert.assertEquals(expected, envelope);
}
use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class FeatureStreamsTest method testModifyIterator.
@Test
public void testModifyIterator() {
FeatureCollection collection = buildSimpleFeatureCollection();
FeatureType originalType = collection.getType();
Filter filter = Filter.include();
Map<String, Object> values = new HashMap<>();
values.put("att_string", "toto");
FeatureIterator ite = FeatureStreams.update(collection.iterator(), filter, values);
assertEquals(3, FeatureStoreUtilities.calculateCount(ite));
ite = FeatureStreams.update(collection.iterator(), filter, values);
while (ite.hasNext()) {
assertTrue(ite.next().getProperty("att_string").getValue().equals("toto"));
}
filter = FF.equal(FF.literal("aaa"), FF.property("att_string"));
ite = FeatureStreams.update(collection.iterator(), filter, values);
assertEquals(3, FeatureStoreUtilities.calculateCount(ite));
ite = FeatureStreams.update(collection.iterator(), filter, values);
while (ite.hasNext()) {
Feature f = ite.next();
if (f.getPropertyValue(AttributeConvention.IDENTIFIER).equals(id3)) {
assertTrue(f.getProperty("att_string").getValue().equals("toto"));
} else {
assertFalse(f.getProperty("att_string").getValue().equals("toto"));
}
}
ite = FeatureStreams.update(collection.iterator(), filter, values);
ite.next();
ite.next();
ite.next();
try {
ite.next();
fail("Should have raise a no such element exception.");
} catch (NoSuchElementException ex) {
// ok
}
// check has next do not iterate
ite = FeatureStreams.update(collection.iterator(), filter, values);
testIterationOnNext(ite, 3);
// check sub iterator is properly closed
CheckCloseFeatureIterator checkIte = new CheckCloseFeatureIterator(collection.iterator());
assertFalse(checkIte.isClosed());
ite = FeatureStreams.update(checkIte, filter, values);
while (ite.hasNext()) ite.next();
ite.close();
assertTrue(checkIte.isClosed());
}
use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class FeatureStreamsTest method buildSimpleFeatureCollection.
private FeatureCollection buildSimpleFeatureCollection() {
FeatureType originalType = buildOriginalFT();
FeatureCollection collection = FeatureStoreUtilities.collection("id", originalType);
Feature sf1 = originalType.newInstance();
sf1.setPropertyValue("id", 0);
sf1.setPropertyValue("att_geom", GF.createPoint(new Coordinate(3, 0)));
sf1.setPropertyValue("att_string", "bbb");
sf1.setPropertyValue("att_double", 3d);
collection.add(sf1);
Feature sf2 = originalType.newInstance();
sf2.setPropertyValue("id", 1);
sf2.setPropertyValue("att_geom", GF.createPoint(new Coordinate(1, 0)));
sf2.setPropertyValue("att_string", "ccc");
sf2.setPropertyValue("att_double", 1d);
collection.add(sf2);
Feature sf3 = originalType.newInstance();
sf3.setPropertyValue("id", 2);
sf3.setPropertyValue("att_geom", GF.createPoint(new Coordinate(2, 0)));
sf3.setPropertyValue("att_string", "aaa");
sf3.setPropertyValue("att_double", 2d);
collection.add(sf3);
return collection;
}
use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.
the class FeatureStreamsTest method buildComplexFeatureCollection.
private FeatureCollection buildComplexFeatureCollection() {
FeatureTypeBuilder builder = new FeatureTypeBuilder();
builder.setName(NAME);
builder.addAttribute(Integer.class).setName("id").addRole(AttributeRole.IDENTIFIER_COMPONENT);
builder.addAttribute(String.class).setName("att_string");
builder.addAttribute(Double.class).setName("att_double");
FeatureType ct = builder.build();
FeatureCollection collectionComplex = FeatureStoreUtilities.collection("cid", ct);
Feature f1 = ct.newInstance();
f1.setPropertyValue("id", COMPLEX_ID_1);
f1.setPropertyValue("att_string", "aaaa");
f1.setPropertyValue("att_double", 12.0);
collectionComplex.add(f1);
Feature f2 = ct.newInstance();
f2.setPropertyValue("id", COMPLEX_ID_2);
f2.setPropertyValue("att_string", "bbbb");
f2.setPropertyValue("att_double", 7.0);
collectionComplex.add(f2);
return collectionComplex;
}
use of org.opengis.feature.FeatureType 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();
}
Aggregations