Search in sources :

Example 1 with FilterFactory2

use of org.geotoolkit.filter.FilterFactory2 in project geotoolkit by Geomatys.

the class PatternSymbolizerTest method testXml.

/**
 * Test Jaxb xml support.
 */
@Test
public void testXml() throws JAXBException, IOException {
    final MutableStyleFactory SF = GO2Utilities.STYLE_FACTORY;
    final FilterFactory2 FF = GO2Utilities.FILTER_FACTORY;
    final Map<Expression, List<Symbolizer>> ranges = new LinkedHashMap<>();
    ranges.put(FF.literal(-1000), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.BLUE), null)));
    ranges.put(FF.literal(-500), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.RED), null)));
    ranges.put(FF.literal(-100), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.GREEN), null)));
    ranges.put(FF.literal(100), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.YELLOW), null)));
    ranges.put(FF.literal(1000), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.GRAY), null)));
    final PatternSymbolizer ps = new PatternSymbolizer(FF.literal(0), ranges, ThreshholdsBelongTo.PRECEDING);
    final MutableStyle style = GO2Utilities.STYLE_FACTORY.style(ps);
    final Path path = Files.createTempFile("xml", ".xml");
    IOUtilities.deleteOnExit(path);
    new StyleXmlIO().writeStyle(path, style, Specification.StyledLayerDescriptor.V_1_1_0);
}
Also used : Path(java.nio.file.Path) MutableStyle(org.geotoolkit.style.MutableStyle) Expression(org.opengis.filter.Expression) StyleXmlIO(org.geotoolkit.sld.xml.StyleXmlIO) List(java.util.List) MutableStyleFactory(org.geotoolkit.style.MutableStyleFactory) FilterFactory2(org.geotoolkit.filter.FilterFactory2) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with FilterFactory2

use of org.geotoolkit.filter.FilterFactory2 in project geotoolkit by Geomatys.

the class FilterConverterTest method buildResultFilter.

private Filter buildResultFilter() throws FactoryException {
    FilterFactory2 ff = FilterUtilities.FF;
    final Filter filter1 = ff.equal(ff.property(NamesExt.create("name")), ff.literal("Smith"));
    final Filter filter2 = ff.equal(ff.property(NamesExt.create("age")), ff.literal(30));
    return ff.and(filter1, filter2);
}
Also used : Filter(org.opengis.filter.Filter) FilterFactory2(org.geotoolkit.filter.FilterFactory2)

Example 3 with FilterFactory2

use of org.geotoolkit.filter.FilterFactory2 in project geotoolkit by Geomatys.

the class IndexedShapefileDataStoreTest method testFidFilter.

@Test
public void testFidFilter() throws Exception {
    File shpFile = copyShapefiles(STATE_POP);
    URL url = shpFile.toURI().toURL();
    IndexedShapefileFeatureStore ds = new IndexedShapefileFeatureStore(url.toURI(), true, true, IndexType.NONE, null);
    FeatureCollection features = ds.createSession(true).getFeatureCollection(new Query(ds.getName()));
    FeatureIterator indexIter = features.iterator();
    Set<String> expectedFids = new HashSet<>();
    final Filter fidFilter;
    try {
        FilterFactory2 ff = FilterUtilities.FF;
        Set<Filter<Object>> fids = new HashSet<>();
        while (indexIter.hasNext()) {
            Feature newFeature = indexIter.next();
            String id = FeatureExt.getId(newFeature).getIdentifier();
            expectedFids.add(id);
            fids.add(ff.resourceId(id));
        }
        switch(fids.size()) {
            case 0:
                fidFilter = Filter.exclude();
                break;
            case 1:
                fidFilter = fids.iterator().next();
                break;
            default:
                fidFilter = ff.or(fids);
                break;
        }
    } finally {
        indexIter.close();
    }
    Set<String> actualFids = new HashSet<>();
    {
        indexIter = ds.getFeatureReader(Query.filtered(ds.getName().toString(), fidFilter));
        while (indexIter.hasNext()) {
            Feature next = indexIter.next();
            String id = FeatureExt.getId(next).getIdentifier();
            actualFids.add(id);
        }
        indexIter.close();
    }
    TreeSet<String> lackingFids = new TreeSet<>(expectedFids);
    lackingFids.removeAll(actualFids);
    TreeSet<String> unexpectedFids = new TreeSet<>(actualFids);
    unexpectedFids.removeAll(expectedFids);
    String lacking = String.valueOf(lackingFids);
    String unexpected = String.valueOf(unexpectedFids);
    String failureMsg = "lacking fids: " + lacking + ". Unexpected ones: " + unexpected;
    assertEquals(failureMsg, expectedFids.size(), actualFids.size());
    assertEquals(failureMsg, expectedFids, actualFids);
}
Also used : Query(org.geotoolkit.storage.feature.query.Query) Feature(org.opengis.feature.Feature) URL(java.net.URL) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Filter(org.opengis.filter.Filter) TreeSet(java.util.TreeSet) File(java.io.File) FilterFactory2(org.geotoolkit.filter.FilterFactory2) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with FilterFactory2

use of org.geotoolkit.filter.FilterFactory2 in project geotoolkit by Geomatys.

the class RegroupFeatureCollection method filter.

private Query filter(final Object attributeValue) throws FactoryException, MismatchedDimensionException, TransformException {
    final FilterFactory2 ff = FilterUtilities.FF;
    final Filter filter = ff.equal(ff.property(regroupAttribute), ff.literal(attributeValue));
    return Query.filtered("filter", filter);
}
Also used : Filter(org.opengis.filter.Filter) FilterFactory2(org.geotoolkit.filter.FilterFactory2)

Example 5 with FilterFactory2

use of org.geotoolkit.filter.FilterFactory2 in project geotoolkit by Geomatys.

the class IndexedShapefileDataStoreTest method testWipesOutInvalidFidsFromFilters.

/**
 * Issueing a request, whether its a query, update or delete, with a fid filter where feature
 * ids match the {@code <typeName>.<number>} structure but the {@code <typeName>} part does not
 * match the actual typeName, shoud ensure the invalid fids are ignored
 *
 * @throws java.lang.Exception
 */
@Test
public void testWipesOutInvalidFidsFromFilters() throws Exception {
    final IndexedShapefileFeatureStore ds = createDataStore();
    final Session session = ds.createSession(true);
    final String validFid1, validFid2, invalidFid1, invalidFid2;
    try (FeatureIterator features = ds.getFeatureReader(new Query(ds.getName()))) {
        validFid1 = FeatureExt.getId(features.next()).getIdentifier();
        validFid2 = FeatureExt.getId(features.next()).getIdentifier();
        invalidFid1 = "_" + FeatureExt.getId(features.next()).getIdentifier();
        invalidFid2 = FeatureExt.getId(features.next()).getIdentifier() + "abc";
    }
    FilterFactory2 ff = FilterUtilities.FF;
    Set<Filter<Object>> ids = new HashSet<>();
    ids.add(ff.resourceId(validFid1));
    ids.add(ff.resourceId(validFid2));
    ids.add(ff.resourceId(invalidFid1));
    ids.add(ff.resourceId(invalidFid2));
    Filter fidFilter = ff.or(ids);
    final FeatureType schema = ds.getFeatureType();
    final String typeName = schema.getName().tip().toString();
    // get a property of type String to update its value by the given filter
    assertEquals(2, count(ds, typeName, fidFilter));
    session.updateFeatures(ds.getName().toString(), fidFilter, Collections.singletonMap("f", "modified"));
    session.commit();
    Filter modifiedFilter = ff.equal(ff.property("f"), ff.literal("modified"));
    assertEquals(2, count(ds, typeName, modifiedFilter));
    final long initialCount = ds.getCount(new Query(ds.getName()));
    session.removeFeatures(ds.getName().toString(), fidFilter);
    session.commit();
    final long afterCount = ds.getCount(new Query(ds.getName()));
    assertEquals(initialCount - 2, afterCount);
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) Filter(org.opengis.filter.Filter) FilterFactory2(org.geotoolkit.filter.FilterFactory2) Session(org.geotoolkit.storage.feature.session.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

FilterFactory2 (org.geotoolkit.filter.FilterFactory2)8 Filter (org.opengis.filter.Filter)5 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)4 Test (org.junit.Test)4 Query (org.geotoolkit.storage.feature.query.Query)3 File (java.io.File)2 HashSet (java.util.HashSet)2 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)2 Expression (org.opengis.filter.Expression)2 URL (java.net.URL)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)1 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)1 CQLException (org.apache.sis.cql.CQLException)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 WritableGridCoverageResource (org.apache.sis.storage.WritableGridCoverageResource)1