Search in sources :

Example 16 with Query

use of org.geotools.data.Query in project GeoGig by boundlessgeo.

the class GeoGigFeatureSourceTest method testGetFeaturesFilter.

@Test
public void testGetFeaturesFilter() throws Exception {
    SimpleFeatureCollection collection;
    Set<List<Object>> actual;
    Set<List<Object>> expected;
    Filter filter;
    filter = ff.id(Collections.singleton(ff.featureId(RepositoryTestCase.idP2)));
    collection = pointsSource.getFeatures(new Query(pointsName, filter));
    actual = Sets.newHashSet();
    for (SimpleFeature f : toList(collection)) {
        actual.add(f.getAttributes());
    }
    expected = Collections.singleton(((SimpleFeature) points2).getAttributes());
    assertEquals(expected, actual);
    ReferencedEnvelope queryBounds = boundsOf(points1, points2);
    Polygon geometry = JTS.toGeometry(queryBounds);
    filter = ff.intersects(ff.property(pointsType.getGeometryDescriptor().getLocalName()), ff.literal(geometry));
    collection = pointsSource.getFeatures(new Query(pointsName, filter));
    actual = Sets.newHashSet();
    for (SimpleFeature f : toList(collection)) {
        actual.add(f.getAttributes());
    }
    expected = ImmutableSet.of(((SimpleFeature) points1).getAttributes(), ((SimpleFeature) points2).getAttributes());
    assertEquals(expected, actual);
    ReferencedEnvelope transformedQueryBounds;
    CoordinateReferenceSystem queryCrs = CRS.decode("EPSG:3857");
    transformedQueryBounds = queryBounds.transform(queryCrs, true);
    geometry = JTS.toGeometry(transformedQueryBounds);
    geometry.setUserData(queryCrs);
    filter = ff.intersects(ff.property(pointsType.getGeometryDescriptor().getLocalName()), ff.literal(geometry));
    collection = pointsSource.getFeatures(new Query(pointsName, filter));
    actual = Sets.newHashSet();
    for (SimpleFeature f : toList(collection)) {
        actual.add(f.getAttributes());
    }
    expected = ImmutableSet.of(((SimpleFeature) points1).getAttributes(), ((SimpleFeature) points2).getAttributes());
    assertEquals(expected.size(), actual.size());
    assertEquals(expected, actual);
    filter = ECQL.toFilter("sp = 'StringProp2_3' OR ip = 2000");
    collection = linesSource.getFeatures(new Query(linesName, filter));
    actual = Sets.newHashSet();
    for (SimpleFeature f : toList(collection)) {
        actual.add(f.getAttributes());
    }
    expected = ImmutableSet.of(((SimpleFeature) lines2).getAttributes(), ((SimpleFeature) lines3).getAttributes());
    assertEquals(expected, actual);
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Query(org.geotools.data.Query) Filter(org.opengis.filter.Filter) List(java.util.List) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Polygon(com.vividsolutions.jts.geom.Polygon) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Test(org.junit.Test)

Example 17 with Query

use of org.geotools.data.Query in project GeoGig by boundlessgeo.

the class ImportOp method insert.

private void insert(final WorkingTree workTree, final String path, @SuppressWarnings("rawtypes") final FeatureSource featureSource, final ProgressListener taskProgress) {
    final Query query = new Query();
    CoordinateSequenceFactory coordSeq = new PackedCoordinateSequenceFactory();
    query.getHints().add(new Hints(Hints.JTS_COORDINATE_SEQUENCE_FACTORY, coordSeq));
    workTree.insert(path, featureSource, query, taskProgress);
}
Also used : Query(org.geotools.data.Query) Hints(org.geotools.factory.Hints) CoordinateSequenceFactory(com.vividsolutions.jts.geom.CoordinateSequenceFactory) PackedCoordinateSequenceFactory(com.vividsolutions.jts.geom.impl.PackedCoordinateSequenceFactory) PackedCoordinateSequenceFactory(com.vividsolutions.jts.geom.impl.PackedCoordinateSequenceFactory)

Example 18 with Query

use of org.geotools.data.Query in project incubator-rya by apache.

the class GeoMesaGeoIndexer method getIteratorWrapper.

private CloseableIteration<Statement, QueryEvaluationException> getIteratorWrapper(final String filterString) {
    return new CloseableIteration<Statement, QueryEvaluationException>() {

        private FeatureIterator<SimpleFeature> featureIterator = null;

        FeatureIterator<SimpleFeature> getIterator() throws QueryEvaluationException {
            if (featureIterator == null) {
                Filter cqlFilter;
                try {
                    cqlFilter = ECQL.toFilter(filterString);
                } catch (final CQLException e) {
                    logger.error("Error parsing query: " + LogUtils.clean(filterString), e);
                    throw new QueryEvaluationException(e);
                }
                final Query query = new Query(featureType.getTypeName(), cqlFilter);
                try {
                    featureIterator = featureSource.getFeatures(query).features();
                } catch (final IOException e) {
                    logger.error("Error performing query: " + LogUtils.clean(filterString), e);
                    throw new QueryEvaluationException(e);
                }
            }
            return featureIterator;
        }

        @Override
        public boolean hasNext() throws QueryEvaluationException {
            return getIterator().hasNext();
        }

        @Override
        public Statement next() throws QueryEvaluationException {
            final SimpleFeature feature = getIterator().next();
            final String subjectString = feature.getAttribute(SUBJECT_ATTRIBUTE).toString();
            final String predicateString = feature.getAttribute(PREDICATE_ATTRIBUTE).toString();
            final String objectString = feature.getAttribute(OBJECT_ATTRIBUTE).toString();
            final String contextString = feature.getAttribute(CONTEXT_ATTRIBUTE).toString();
            final Statement statement = StatementSerializer.readStatement(subjectString, predicateString, objectString, contextString);
            return statement;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Remove not implemented");
        }

        @Override
        public void close() throws QueryEvaluationException {
            getIterator().close();
        }
    };
}
Also used : CloseableIteration(info.aduna.iteration.CloseableIteration) FeatureIterator(org.geotools.feature.FeatureIterator) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Query(org.geotools.data.Query) NearQuery(org.apache.rya.indexing.accumulo.geo.GeoTupleSet.GeoSearchFunctionFactory.NearQuery) Filter(org.opengis.filter.Filter) RyaStatement(org.apache.rya.api.domain.RyaStatement) Statement(org.openrdf.model.Statement) CQLException(org.geotools.filter.text.cql2.CQLException) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

Example 19 with Query

use of org.geotools.data.Query in project incubator-rya by apache.

the class GeoWaveFeatureReaderTest method testBBOX.

@Test
public void testBBOX() throws IllegalArgumentException, NoSuchElementException, IOException {
    final FilterFactoryImpl factory = new FilterFactoryImpl();
    final Query query = new Query("GeoWaveFeatureReaderTest", factory.bbox("", -180, -90, 180, 90, "EPSG:4326"), new String[] { "geometry", "pid" });
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader = dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertTrue(count > 0);
}
Also used : Query(org.geotools.data.Query) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Example 20 with Query

use of org.geotools.data.Query in project incubator-rya by apache.

the class GeoWaveFeatureReaderTest method testRemoveFeature.

@Test
public void testRemoveFeature() throws IllegalArgumentException, NoSuchElementException, IOException, CQLException {
    final Query query = new Query("GeoWaveFeatureReaderTest", ECQL.toFilter("pid like '" + pids.get(0).substring(0, 1) + "%'"), new String[] { "geometry", "pid" });
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader = dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertEquals(1, count);
    // Remove
    final FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriter(type.getTypeName(), Transaction.AUTO_COMMIT);
    try {
        while (writer.hasNext()) {
            writer.next();
            writer.remove();
        }
    } finally {
        writer.close();
    }
    // Re-query
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader2 = dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int recount = 0;
    while (reader2.hasNext()) {
        reader2.next();
        recount++;
    }
    assertEquals(0, recount);
}
Also used : Query(org.geotools.data.Query) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Aggregations

Query (org.geotools.data.Query)34 SimpleFeature (org.opengis.feature.simple.SimpleFeature)18 Test (org.junit.Test)16 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)13 HashMap (java.util.HashMap)6 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)6 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)6 Filter (org.opengis.filter.Filter)6 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)6 Geometry (com.vividsolutions.jts.geom.Geometry)5 DefaultTransaction (org.geotools.data.DefaultTransaction)5 Transaction (org.geotools.data.Transaction)5 Polygon (com.vividsolutions.jts.geom.Polygon)4 IOException (java.io.IOException)4 Map (java.util.Map)4 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)4 ArrayList (java.util.ArrayList)3 FileDataStoreFactorySpi (org.geotools.data.FileDataStoreFactorySpi)3 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)3 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)3