use of org.opengis.filter.ResourceId in project geotoolkit by Geomatys.
the class AbstractReadingTests method testReaders.
/**
* test different readers.
*/
private void testReaders(final FeatureSet featureSet, final ExpectedResult candidate) throws Exception {
final FeatureType type = featureSet.getType();
final Collection<? extends PropertyType> properties = type.getProperties(true);
try (Stream<Feature> stream = featureSet.features(true)) {
stream.forEach((Feature t) -> {
// do nothing
});
throw new Exception("Asking for a reader without any query whould raise an error.");
} catch (Exception ex) {
// ok
}
// property -------------------------------------------------------------
{
// check only id query
final FeatureQuery query = new FeatureQuery();
query.setProjection(new FeatureQuery.NamedExpression(FF.property(AttributeConvention.IDENTIFIER)));
FeatureSet subset = featureSet.subset(query);
FeatureType limited = subset.getType();
assertNotNull(limited);
assertTrue(limited.getProperties(true).size() == 1);
try (Stream<Feature> stream = subset.features(false)) {
final Iterator<Feature> ite = stream.iterator();
while (ite.hasNext()) {
final Feature f = ite.next();
assertNotNull(FeatureExt.getId(f));
}
}
for (final PropertyType desc : properties) {
if (desc instanceof Operation)
continue;
final FeatureQuery sq = new FeatureQuery();
sq.setProjection(new FeatureQuery.NamedExpression(FF.property(desc.getName().tip().toString())));
subset = featureSet.subset(sq);
limited = subset.getType();
assertNotNull(limited);
assertTrue(limited.getProperties(true).size() == 1);
assertNotNull(limited.getProperty(desc.getName().toString()));
try (Stream<Feature> stream = subset.features(false)) {
final Iterator<Feature> ite = stream.iterator();
while (ite.hasNext()) {
final Feature f = ite.next();
assertNotNull(f.getProperty(desc.getName().toString()));
}
}
}
}
// sort by --------------------------------------------------------------
for (final PropertyType desc : properties) {
if (!(desc instanceof AttributeType)) {
continue;
}
final AttributeType att = (AttributeType) desc;
if (att.getMaximumOccurs() > 1) {
// do not test sort by on multi occurence properties
continue;
}
final Class clazz = att.getValueClass();
if (!Comparable.class.isAssignableFrom(clazz) || Geometry.class.isAssignableFrom(clazz)) {
// can not make a sort by on this attribut.
continue;
}
final FeatureQuery query = new FeatureQuery();
query.setSortBy(FF.sort(FF.property(desc.getName().tip().toString()), SortOrder.ASCENDING));
FeatureSet subset = featureSet.subset(query);
// count should not change with a sort by
assertEquals(candidate.size, FeatureStoreUtilities.getCount(subset, true).intValue());
try (Stream<Feature> stream = subset.features(false)) {
final Iterator<Feature> reader = stream.iterator();
Comparable last = null;
while (reader.hasNext()) {
final Feature f = reader.next();
Object obj = f.getProperty(desc.getName().toString()).getValue();
if (obj instanceof Identifier)
obj = ((Identifier) obj).getCode();
final Comparable current = (Comparable) obj;
if (current != null) {
if (last != null) {
// check we have the correct order.
assertTrue(current.compareTo(last) >= 0);
}
last = current;
} else {
// any restriction about where should be placed the feature with null values ? before ? after ?
}
}
}
query.setSortBy(FF.sort(FF.property(desc.getName().tip().toString()), SortOrder.DESCENDING));
subset = featureSet.subset(query);
// count should not change with a sort by
assertEquals(candidate.size, FeatureStoreUtilities.getCount(subset, true).intValue());
try (Stream<Feature> stream = subset.features(false)) {
final Iterator<Feature> reader = stream.iterator();
Comparable last = null;
while (reader.hasNext()) {
final Feature f = reader.next();
Object obj = f.getProperty(desc.getName().toString()).getValue();
if (obj instanceof Identifier)
obj = ((Identifier) obj).getCode();
final Comparable current = (Comparable) obj;
if (current != null) {
if (last != null) {
// check we have the correct order.
assertTrue(current.compareTo(last) <= 0);
}
last = current;
} else {
// any restriction about where should be placed the feature with null values ? before ? after ?
}
}
}
}
// start ----------------------------------------------------------------
if (candidate.size > 1) {
List<ResourceId> ids = new ArrayList<>();
try (Stream<Feature> stream = featureSet.features(false)) {
final Iterator<Feature> ite = stream.iterator();
while (ite.hasNext()) {
ids.add(FeatureExt.getId(ite.next()));
}
}
// skip the first element
final FeatureQuery query = new FeatureQuery();
query.setOffset(1);
try (Stream<Feature> stream = featureSet.subset(query).features(false)) {
final Iterator<Feature> ite = stream.iterator();
int i = 1;
while (ite.hasNext()) {
assertEquals(FeatureExt.getId(ite.next()), ids.get(i));
i++;
}
}
}
// max ------------------------------------------------------------------
if (candidate.size > 1) {
final FeatureQuery query = new FeatureQuery();
query.setLimit(1);
int i = 0;
try (Stream<Feature> stream = featureSet.subset(query).features(false)) {
final Iterator<Feature> ite = stream.iterator();
while (ite.hasNext()) {
ite.next();
i++;
}
}
assertEquals(1, i);
}
// filter ---------------------------------------------------------------
// filters are tested more deeply in the filter module
// we just make a few tests here for sanity check
// todo should we make more deep tests ?
Set<ResourceId> ids = new HashSet<>();
try (Stream<Feature> stream = featureSet.features(false)) {
final Iterator<Feature> ite = stream.iterator();
// peek only one on two ids
boolean oneOnTwo = true;
while (ite.hasNext()) {
final Feature feature = ite.next();
if (oneOnTwo) {
ids.add(FeatureExt.getId(feature));
}
oneOnTwo = !oneOnTwo;
}
}
Set<ResourceId> remaining = new HashSet<>(ids);
final FeatureQuery query = new FeatureQuery();
final Filter f;
switch(ids.size()) {
case 0:
f = Filter.exclude();
break;
case 1:
f = ids.iterator().next();
break;
default:
f = FF.or(ids);
break;
}
query.setSelection(f);
try (Stream<Feature> stream = featureSet.subset(query).features(false)) {
final Iterator<Feature> ite = stream.iterator();
while (ite.hasNext()) {
remaining.remove(FeatureExt.getId(ite.next()));
}
}
assertTrue(remaining.isEmpty());
}
use of org.opengis.filter.ResourceId in project geotoolkit by Geomatys.
the class Utils method getId.
/**
* Return a string value of the FeatureId for a given feature.
* If the feature id can not be foud, the fallback value will be returned.
*
* I the id contains the character ':', it will be replaced by '_' for xml validation purpose.
*
* @param feature The feature on which we want to extract the feature id.
* @param fallback Fallback value if we can not succeed to extract a featureId.
* @return
*/
public static String getId(Feature feature, String fallback) {
final ResourceId attId;
try {
attId = FeatureExt.getId(feature);
} catch (PropertyNotFoundException ex) {
return fallback;
}
if (attId == null)
return fallback;
final String id = attId.getIdentifier();
if (id == null)
return fallback;
if (id instanceof String) {
if (((String) id).isEmpty()) {
return fallback;
} else {
return ((String) id).replace(':', '_');
}
} else {
return String.valueOf(id).replace(':', '_');
}
}
use of org.opengis.filter.ResourceId 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.opengis.filter.ResourceId in project geotoolkit by Geomatys.
the class PostgresComplexTypeTest method testHandMadeSQLQuery.
/**
* Test hand made query.
*/
@Test
public void testHandMadeSQLQuery() throws Exception {
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 org.apache.sis.storage.Query query = new SQLQuery("SELECT * FROM \"Stop\"", "s1");
final FeatureReader ite = store.getFeatureReader(query);
final boolean[] found = new boolean[3];
try {
while (ite.hasNext()) {
final Feature feature = ite.next();
final Timestamp time = (Timestamp) feature.getPropertyValue("time");
final Point location = (Point) feature.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" + feature);
}
final Optional<Geometry> geom = FeatureExt.getDefaultGeometryValue(feature).filter(Geometry.class::isInstance).map(Geometry.class::cast);
Assert.assertTrue(geom.isPresent());
assertNotNull(JTS.findCoordinateReferenceSystem(geom.get()));
}
} finally {
ite.close();
}
for (boolean b : found) assertTrue(b);
}
use of org.opengis.filter.ResourceId 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