/** Returns a GeoTools query build with the provided attributes and filters */ private Query buildQuery(List<PropertyName> attributes, Filter filter) { if (attributes == null && (filter == null || filter == Filter.INCLUDE)) { return Query.ALL; } else { Query q = new Query(); q.setFilter(filter); // TODO: switch this to property names when possible q.setPropertyNames(flattenNames(attributes)); return q; } }
/** * Constructor. * * @param typeName the name of the featureType to retrieve. * @param namespace Namespace for provided typeName, or null if unspecified * @param filter the OGC filter to constrain the request. * @param maxFeatures the maximum number of features to be returned. * @param propNames an array of the properties to fetch. * @param handle the name to associate with the query. */ public Query( String typeName, URI namespace, Filter filter, int maxFeatures, String[] propNames, String handle) { this.typeName = typeName; this.filter = filter; this.namespace = namespace; this.maxFeatures = maxFeatures; this.handle = handle; setPropertyNames(propNames); }
defQuery.setPropertyNames(propNames);
query.setFilter(rangeFilter); query.setMaxFeatures(maxEntries); query.setPropertyNames(new String[] {descriptor.getStartAttribute()}); query.setHints(new Hints(StructuredCoverageViewReader.QUERY_FIRST_BAND, true));
@Override public int getCount(Query query) throws IOException { // transforming does not change count, but we have to transform the filter Query txQuery = transformer.transformQuery(query); txQuery.setPropertyNames(Query.ALL_NAMES); // let the world know if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log( Level.FINE, "The original query for count computation{0} has been transformed to {1}", new Object[] {query, txQuery}); } return source.getCount(txQuery); }
/** * @param metadataName * @param attributeName * @return * @throws IOException */ FeatureCalc createExtremaQuery(String metadataName, String attributeName) throws IOException { final Query query = new Query(typeName); query.setPropertyNames(Arrays.asList(attributeName)); final FeatureCalc visitor = metadataName.toLowerCase().endsWith("maximum") ? new MaxVisitor(attributeName) : new MinVisitor(attributeName); granuleCatalog.computeAggregateFunction(query, visitor); return visitor; }
/** * Extract the domain of a dimension as a set of unique values. * * <p>It retrieves a comma separated list of values as a Set of {@link String}. * * @return a comma separated list of values as a {@link String}. * @throws IOException */ Set extractDomain(final String attribute) throws IOException { Query query = new Query(typeName); query.setPropertyNames(Arrays.asList(attribute)); final UniqueVisitor visitor = new UniqueVisitor(attribute); granuleCatalog.computeAggregateFunction(query, visitor); return visitor.getUnique(); }
public boolean isEmpty() { // build a minimal query Query notEmptyQuery = new Query(query); notEmptyQuery.setMaxFeatures(1); AttributeDescriptor smallAttribute = getSmallAttributeInSchema(); if (smallAttribute != null) { notEmptyQuery.setPropertyNames( Collections.singletonList(smallAttribute.getLocalName())); } try { FeatureReader<?, ?> fr = featureSource.getReader(notEmptyQuery); try { return !fr.hasNext(); } finally { fr.close(); } } catch (IOException e) { throw new RuntimeException(e); } }
private List<String> collectFilesFromTable(DataStore sourceStore, String table) throws IOException { final SimpleFeatureSource featureSource = sourceStore.getFeatureSource(table); final Properties properties = getCoverageConfiguration(table); String locationAttribute = getProperty( properties, Utils.Prop.LOCATION_ATTRIBUTE, Utils.DEFAULT_LOCATION_ATTRIBUTE); PathType pathType = PathType.RELATIVE.valueOf( getProperty(properties, Utils.Prop.PATH_TYPE, PathType.ABSOLUTE.name())); // query the location attribute Query q = new Query(table); q.setPropertyNames(new String[] {locationAttribute}); // extract unique values UniqueVisitor uniqueLocations = new UniqueVisitor(locationAttribute); featureSource.getFeatures(q).accepts(uniqueLocations, null); Set<String> locations = uniqueLocations.getUnique(); // map via pathtype and return return locations .stream() .map(l -> pathType.resolvePath(configuration.getMosaicDirectory().getPath(), l)) .map(url -> URLs.urlToFile(url).getAbsolutePath()) .collect(Collectors.toList()); }
@Test public void testRetypeCannotSortFullyCovered() throws Exception { Query q = new Query(); q.setPropertyNames( new String[] { "name", }); q.setSortBy(new SortBy[] {ff.sort("z", SortOrder.ASCENDING)}); Query expected = new Query(q); expected.setPropertyNames(new String[] {"name", "z"}); checkRetypeCannotSort(q, expected); }
@Test public void testRetypeCannotSortPartiallyCovered() throws Exception { Query q = new Query(); q.setPropertyNames( new String[] { "name", }); q.setSortBy( new SortBy[] { ff.sort("name", SortOrder.ASCENDING), ff.sort("z", SortOrder.ASCENDING) }); Query expected = new Query(q); expected.setPropertyNames(new String[] {"name", "z"}); checkRetypeCannotSort(q, expected); }
@Test public void testRetypeCannotSortCovered() throws Exception { Query q = new Query(); q.setPropertyNames(new String[] {"name", "z"}); q.setSortBy(new SortBy[] {ff.sort("z", SortOrder.ASCENDING)}); checkRetypeCannotSort(q, q); }
/** Test of retrieveAllProperties method, of class org.geotools.data.Query. */ public void testRetrieveAllProperties() { // System.out.println("testRetrieveAllProperties"); Query query = new Query(); assertTrue(query.retrieveAllProperties()); query.setPropertyNames(new String[] {"foo", "bar"}); assertFalse(query.retrieveAllProperties()); query.setPropertyNames(Query.ALL_NAMES); assertTrue(query.retrieveAllProperties()); query.setProperties(Query.ALL_PROPERTIES); assertTrue(query.retrieveAllProperties()); query.setPropertyNames(new String[] {"foo", "bar"}); query.setProperties(Query.ALL_PROPERTIES); assertTrue(query.retrieveAllProperties()); }
/** Test of getPropertyNames method, of class org.geotools.data.Query. */ public void testPropertyNames() { // System.out.println("testPropertyNames"); Query query = new Query(); assertNull(query.getPropertyNames()); query.setPropertyNames(new String[] {"foo", "bar"}); String names[] = query.getPropertyNames(); assertNotNull(names); assertEquals("foo", names[0]); List list = Arrays.asList(names); query.setPropertyNames(list); names = query.getPropertyNames(); assertEquals("bar", names[1]); // test compatibility with getProperties method List<PropertyName> properties2 = query.getProperties(); assertNotNull(properties2); assertEquals("foo", properties2.get(0).getPropertyName()); assertEquals("bar", properties2.get(1).getPropertyName()); query.setPropertyNames(Query.ALL_NAMES); assertNull(query.getPropertyNames()); query = new Query("Test", Filter.INCLUDE, new String[] {"foo", "wibble"}); assertNotNull(query.getPropertyNames()); }
public void testGetFeatureInvalidFilter() throws Exception { FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); PropertyIsEqualTo f = ff.equals(ff.property("invalidAttribute"), ff.literal(5)); Query q = new Query(tname("river")); q.setPropertyNames(new String[] {aname("geom")}); q.setFilter(f); // make sure a complaint related to the invalid filter is thrown here try (FeatureReader<SimpleFeatureType, SimpleFeature> reader = dataStore.getFeatureReader(q, Transaction.AUTO_COMMIT)) { fail("This query should have failed, it contains an invalid filter"); } catch (Exception e) { // good, it's supposed to fail } }
/** Test of toString method, of class org.geotools.data.Query. */ public void testToString() { // System.out.println("testToString"); Query query = new Query(); assertNotNull(query.toString()); query.setHandle("myquery"); assertNotNull(query.toString()); query.setFilter(Filter.EXCLUDE); assertNotNull(query.toString()); query.setPropertyNames(new String[] {"foo", "bar"}); assertNotNull(query.toString()); query = new Query(); query.setSortBy(new SortBy[] {SortBy.NATURAL_ORDER}); assertTrue(query.toString().contains("[sort by: NATURAL]")); query.setSortBy(new SortBy[] {SortBy.REVERSE_ORDER}); assertTrue(query.toString().contains("[sort by: REVERSE]")); } }
public void testGetFeatureReaderFilterPrePostWithNoGeometry() throws IOException, IllegalFilterException { // GEOT-1069, make sure the post filter is run even if the geom property is not requested try (Transaction t = new DefaultTransaction()) { FilterFactory factory = CommonFactoryFinder.getFilterFactory(null); FilterFunction_geometryType geomTypeExpr = new FilterFunction_geometryType(); geomTypeExpr.setParameters( (List) Collections.singletonList(factory.property(aname("geom")))); PropertyIsEqualTo filter = factory.equals(geomTypeExpr, factory.literal("Polygon")); Query query = new Query(tname("road"), filter); query.setPropertyNames((List) Collections.singletonList(aname("id"))); try (FeatureReader<SimpleFeatureType, SimpleFeature> reader = dataStore.getFeatureReader(query, t)) { // if the above statement didn't throw an exception, we're content assertNotNull(reader); } filter = factory.equals(geomTypeExpr, factory.literal("LineString")); query.setFilter(filter); try (FeatureReader<SimpleFeatureType, SimpleFeature> reader = dataStore.getFeatureReader(query, t)) { assertTrue(reader.hasNext()); } } }
public void testGetFeatureReaderFilterWithAttributesNotRequested2() throws Exception { SimpleFeatureType type = dataStore.getSchema(tname("river")); FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); FilterFunction_ceil ceil = new FilterFunction_ceil(); ceil.setParameters((List) Collections.singletonList(ff.property(aname("flow")))); PropertyIsEqualTo f = ff.equals(ceil, ff.literal(5)); Query q = new Query(tname("river")); q.setPropertyNames(new String[] {aname("geom")}); q.setFilter(f); // with GEOT-1069 an exception is thrown here try (Transaction t = new DefaultTransaction(); FeatureReader<SimpleFeatureType, SimpleFeature> reader = dataStore.getFeatureReader(q, t)) { assertTrue(reader.hasNext()); assertEquals(1, reader.getFeatureType().getAttributeCount()); reader.next(); assertFalse(reader.hasNext()); } }
public void testGetFeaturesWithQuery() throws Exception { FilterFactory ff = dataStore.getFilterFactory(); PropertyIsEqualTo filter = ff.equals(ff.property(aname("stringProperty")), ff.literal("one")); Query query = new Query(); query.setPropertyNames(new String[] {aname("doubleProperty"), aname("intProperty")}); query.setFilter(filter); SimpleFeatureCollection features = featureSource.getFeatures(query); assertEquals(1, features.size()); try (SimpleFeatureIterator iterator = features.features()) { assertTrue(iterator.hasNext()); SimpleFeature feature = (SimpleFeature) iterator.next(); assertEquals(2, feature.getAttributeCount()); assertEquals(new Double(1.1), feature.getAttribute(aname("doubleProperty"))); assertNotNull(feature.getAttribute(aname("intProperty"))); } }
public void testGetFeatureReaderFilterWithAttributesNotRequested() throws Exception { // this is here to avoid http://jira.codehaus.org/browse/GEOT-1069 // to come up again SimpleFeatureType type = dataStore.getSchema(tname("river")); FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); PropertyIsEqualTo f = ff.equals(ff.property(aname("flow")), ff.literal(4.5)); Query q = new Query(tname("river")); q.setPropertyNames(new String[] {aname("geom")}); q.setFilter(f); // with GEOT-1069 an exception is thrown here try (Transaction t = new DefaultTransaction(); FeatureReader<SimpleFeatureType, SimpleFeature> reader = dataStore.getFeatureReader(q, t)) { assertTrue(reader.hasNext()); assertEquals(1, reader.getFeatureType().getAttributeCount()); reader.next(); assertFalse(reader.hasNext()); } }