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);
}
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);
}
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();
}
};
}
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);
}
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);
}
Aggregations