@Test public void testModify() throws Exception { final Query queryAll = new Query(RENAMED); SimpleFeatureStore store; store = (SimpleFeatureStore) rts.getFeatureSource(RENAMED); SimpleFeature original = store.getFeatures(fidFilter).features().next(); String newAddress = ((String) original.getAttribute("ADDRESS")) + " xxx"; store.modifyFeatures( original.getFeatureType().getDescriptor("ADDRESS"), newAddress, fidFilter); SimpleFeature modified = store.getFeatures(fidFilter).features().next(); assertEquals(newAddress, modified.getAttribute("ADDRESS")); }
if ((query == Query.ALL) || query.equals(Query.ALL)) { return query; Filter filter = query.getFilter(); filter = makeDefinitionFilter(filter); Query defQuery = new Query(query); defQuery.setFilter(filter); defQuery.setPropertyNames(propNames); if (query.getSortBy() != null) { defQuery.setSortBy(query.getSortBy()); query.getHints().put(Hints.LINEARIZATION_TOLERANCE, linearizationTolerance);
if (writeQuery == Query.ALL) { ((SimpleFeatureStore) storeDelegate).modifyFeatures(names, values, filter); } else if (writeQuery.getFilter() == Filter.EXCLUDE || writeQuery.getPropertyNames() == Query.NO_NAMES) { throw unsupportedOperation(); final Query local = new Query(null, filter); Query mixed = mixQueries(local, writeQuery); if (writeQuery.getPropertyNames() == Query.ALL_NAMES) { ((SimpleFeatureStore) storeDelegate).modifyFeatures(names, values, mixed.getFilter()); } else { new HashSet<String>(Arrays.asList(writeQuery.getPropertyNames())); String typeName = getSchema().getName().getLocalPart(); if (policy.getResponse() == org.geoserver.security.Response.CHALLENGE) { throw SecureCatalogImpl.unauthorizedAccess(typeName); ((SimpleFeatureStore) storeDelegate).modifyFeatures(names, values, mixed.getFilter());
/** 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; } }
new Query( DataUtilities.mixQueries(userQuery, securityQuery, userQuery.getHandle())); List<PropertyName> securityProperties = securityQuery.getProperties(); if (securityProperties != null && securityProperties.size() > 0) { List<PropertyName> userProperties = userQuery.getProperties(); if (userProperties == null) { result.setProperties(securityProperties); } else { for (PropertyName pn : userProperties) { result.setProperties(userProperties); if (userQuery.getHints() == null) { result.setHints(securityQuery.getHints()); } else if (securityQuery.getHints() == null) { result.setHints(userQuery.getHints()); } else { Hints mix = userQuery.getHints(); mix.putAll(securityQuery.getHints()); result.setHints(mix); result.setCoordinateSystem(userQuery.getCoordinateSystem()); result.setCoordinateSystemReproject(userQuery.getCoordinateSystemReproject()); result.setStartIndex(userQuery.getStartIndex()); result.setSortBy(userQuery.getSortBy());
Query query = new Query("__BlobInfo__"); query.addFilter("filename", FilterOperator.EQUAL, filename); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); PreparedQuery pq = datastore.prepare(query); List<Entity> entList = pq.asList(FetchOptions.Builder.withLimit(1)); String name = entList.get(0).getKey().getName();
public void testGetFeatureSourceRoad() throws Exception { SimpleFeatureSource road = dataStore.getFeatureSource(tname("road")); assertEquals(3, all.size()); assertTrue(areReferencedEnvelopesEqual(td.roadBounds, all.getBounds())); assertTrue(areReferencedEnvelopesEqual(td.roadBounds, all.getBounds())); assertEquals(2, some.size()); ReferencedEnvelope e = new ReferencedEnvelope(CRS.decode("EPSG:4326", forceLongitudeFirst)); e.include(td.roadFeatures[0].getBounds()); e.include(td.roadFeatures[1].getBounds()); assertTrue(areReferencedEnvelopesEqual(e, some.getBounds())); assertEquals(some.getSchema(), road.getSchema()); Query query = new Query(tname("road"), td.rd12Filter, new String[] {aname("name")}); assertEquals(2, half.size()); assertEquals(1, half.getSchema().getAttributeCount()); assertEquals(type.getName(), actual.getName()); assertEquals(type.getAttributeCount(), actual.getAttributeCount());
public void testGetFeaturesWithMax() throws Exception { Query q = new Query(featureSource.getSchema().getTypeName()); q.setMaxFeatures(2); SimpleFeatureCollection features = featureSource.getFeatures(q); // check size assertEquals(2, features.size()); // check actual iteration try (SimpleFeatureIterator it = features.features()) { int count = 0; ReferencedEnvelope env = new ReferencedEnvelope(features.getSchema().getCoordinateReferenceSystem()); while (it.hasNext()) { SimpleFeature f = it.next(); env.expandToInclude(ReferencedEnvelope.reference(f.getBounds())); count++; } assertEquals(2, count); assertTrue(areReferencedEnvelopesEqual(env, features.getBounds())); } }
public void testGetFeaturesWithOffsetLimitAndPostFilter() throws Exception { Query q = new Query(featureSource.getSchema().getTypeName()); // no sorting, let's see if the database can use native one FilterFactory ff = dataStore.getFilterFactory(); PropertyIsEqualTo filter = ff.equal( ff.literal("one"), ff.function("strToLowerCase", ff.property(aname("stringProperty"))), true); q.setFilter(filter); q.setStartIndex(0); q.setMaxFeatures(1); SimpleFeatureCollection features = featureSource.getFeatures(q); // check size assertEquals(1, features.size()); // check actual iteration try (SimpleFeatureIterator it = features.features()) { assertTrue(it.hasNext()); SimpleFeature f = it.next(); ReferencedEnvelope fe = ReferencedEnvelope.reference(f.getBounds()); assertEquals(1, ((Number) f.getAttribute(aname("intProperty"))).intValue()); assertFalse(it.hasNext()); // assertEquals(fe, features.getBounds()); assertTrue(areReferencedEnvelopesEqual(fe, features.getBounds())); } }
assertSame(data, road.getDataStore()); assertEquals(3, road.getCount(Query.ALL)); assertEquals(new ReferencedEnvelope(1, 5, 0, 4, null), road.getBounds(Query.ALL)); assertEquals(3, all.size()); assertEquals(roadBounds, all.getBounds()); assertEquals(roadBounds, all.getBounds()); assertEquals(some.getSchema(), road.getSchema()); Query query = new Query("road", rd12Filter, new String[] {"name", "geom"}); assertEquals(2, half.size()); assertEquals(2, half.getSchema().getAttributeCount()); SimpleFeatureIterator reader = half.features(); SimpleFeatureType type = half.getSchema(); reader.close(); SimpleFeatureType actual = half.getSchema(); assertEquals(type.getTypeName(), actual.getTypeName()); assertEquals(type.getName(), actual.getName()); assertEquals(type.getAttributeCount(), actual.getAttributeCount());
public void testGetFeaturesWithOffset() throws Exception { Query q = new Query(featureSource.getSchema().getTypeName()); q.setSortBy( new SortBy[] { dataStore.getFilterFactory().sort(aname("intProperty"), SortOrder.ASCENDING) }); q.setStartIndex(2); SimpleFeatureCollection features = featureSource.getFeatures(q); // check size assertEquals(1, features.size()); // check actual iteration try (SimpleFeatureIterator it = features.features()) { assertTrue(it.hasNext()); SimpleFeature f = it.next(); ReferencedEnvelope fe = ReferencedEnvelope.reference(f.getBounds()); assertEquals(2, ((Number) f.getAttribute(aname("intProperty"))).intValue()); assertFalse(it.hasNext()); // assertEquals(fe, features.getBounds()); assertTrue(areReferencedEnvelopesEqual(fe, features.getBounds())); } }
void doTestSimpleJoinWithLimitOffset(boolean exposePrimaryKeys) throws Exception { dataStore.setExposePrimaryKeyColumns(exposePrimaryKeys); FilterFactory ff = dataStore.getFilterFactory(); Filter j = ff.equal(ff.property(aname("stringProperty")), ff.property(aname("name")), true); Query q = new Query(tname("ft1")); q.getJoins().add(new Join(tname("ftjoin"), j)); q.setFilter(ff.greater(ff.property(aname("intProperty")), ff.literal(0))); q.setStartIndex(1); q.setSortBy(new SortBy[] {ff.sort(aname("intProperty"), SortOrder.ASCENDING)}); SimpleFeatureCollection features = dataStore.getFeatureSource(tname("ft1")).getFeatures(q); assertEquals(1, features.size()); try (SimpleFeatureIterator it = features.features()) { assertTrue(it.hasNext()); SimpleFeature f = it.next(); assertEquals("two", f.getAttribute(aname("stringProperty"))); SimpleFeature g = (SimpleFeature) f.getAttribute(aname("ftjoin")); assertEquals("two", g.getAttribute(aname("name"))); } }
public void testGetFeaturesWithOffsetLimit() throws Exception { Query q = new Query(featureSource.getSchema().getTypeName()); // no sorting, let's see if the database can use native one q.setStartIndex(1); q.setMaxFeatures(1); SimpleFeatureCollection features = featureSource.getFeatures(q); // check size assertEquals(1, features.size()); // check actual iteration try (SimpleFeatureIterator it = features.features()) { assertTrue(it.hasNext()); SimpleFeature f = it.next(); ReferencedEnvelope fe = ReferencedEnvelope.reference(f.getBounds()); assertEquals(1, ((Number) f.getAttribute(aname("intProperty"))).intValue()); assertFalse(it.hasNext()); // assertEquals(fe, features.getBounds()); assertTrue(areReferencedEnvelopesEqual(fe, features.getBounds())); } }
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"))); } }
CoordinateReferenceSystem WGS84 = CRS.decode("EPSG:4326", true); Query query = new Query(); query.setCoordinateSystem(sourceCRS); query.setCoordinateSystemReproject(WGS84); SimpleFeatureCollection featureCollection = featureSource.getFeatures(query); SimpleFeatureIterator it = featureCollection.features(); int i = 0; while (it.hasNext()) { SimpleFeature feature = it.next(); Geometry geom = (Geometry) feature.getDefaultGeometry(); Point point = null; if (geom instanceof Point) { label = Integer.toString(i); } else { label = feature.getAttribute(labelAttribute).toString(); Number n = (Number) feature.getAttribute(inputAttribute); input = n.doubleValue(); it.close(); } catch (Exception ex) { LOG.error("Error loading population from shapefile: {}", ex.getMessage());
@Test public void testFeautureSourceFidFilter() throws Exception { // grab the last feature in the collection (there are more than one) SimpleFeatureSource fs = rts.getFeatureSource(RENAMED); // build a filter that will retrieve that feature only FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); final String fid = RENAMED + ".1107531701011"; Filter fidFilter = ff.id(Collections.singleton(ff.featureId(fid))); SimpleFeatureCollection fc = fs.getFeatures(new Query(RENAMED, fidFilter)); assertEquals(RENAMED, fc.getSchema().getName().getLocalPart()); assertEquals(1, fc.size()); FeatureIterator<SimpleFeature> it = fc.features(); assertTrue(it.hasNext()); SimpleFeature sf = it.next(); assertFalse(it.hasNext()); it.close(); assertEquals(fid, sf.getID()); }
public void testNaturalSortingAsc() throws Exception { Query q = new Query(featureSource.getSchema().getTypeName()); q.setSortBy(new SortBy[] {SortBy.NATURAL_ORDER}); try (SimpleFeatureIterator features = featureSource.getFeatures(q).features()) { String prevId = null; while (features.hasNext()) { String currId = features.next().getID(); if (prevId != null) assertTrue(prevId.compareTo(currId) <= 0); prevId = currId; } } }
public void testGeometryFactoryHintsCS() throws IOException { SimpleFeatureSource fs = dataStore.getFeatureSource(tname("road")); assertTrue(fs.getSupportedHints().contains(Hints.JTS_COORDINATE_SEQUENCE_FACTORY)); Query q = new Query(tname("road")); Hints hints = new Hints( Hints.JTS_COORDINATE_SEQUENCE_FACTORY, new LiteCoordinateSequenceFactory()); q.setHints(hints); try (SimpleFeatureIterator it = fs.getFeatures(q).features()) { it.hasNext(); SimpleFeature f = (SimpleFeature) it.next(); LineString ls = (LineString) f.getDefaultGeometry(); assertTrue(ls.getCoordinateSequence() instanceof LiteCoordinateSequence); } }
void doTestSimpleJoinWithSort(boolean exposePrimaryKeys) throws Exception { dataStore.setExposePrimaryKeyColumns(exposePrimaryKeys); FilterFactory ff = dataStore.getFilterFactory(); Filter j = ff.equal(ff.property(aname("stringProperty")), ff.property(aname("name")), true); Query q = new Query(tname("ft1")); q.getJoins().add(new Join(tname("ftjoin"), j)); q.setSortBy(new SortBy[] {ff.sort(aname("intProperty"), SortOrder.DESCENDING)}); SimpleFeatureCollection features = dataStore.getFeatureSource(tname("ft1")).getFeatures(q); try (SimpleFeatureIterator it = features.features()) { assertTrue(it.hasNext()); assertEquals("two", it.next().getAttribute(aname("stringProperty"))); assertTrue(it.hasNext()); assertEquals("one", it.next().getAttribute(aname("stringProperty"))); assertTrue(it.hasNext()); assertEquals("zero", it.next().getAttribute(aname("stringProperty"))); } }
CoordinateReferenceSystem WGS84 = CRS.decode("EPSG:4326", true); Query query = new Query(); query.setCoordinateSystem(sourceCRS); query.setCoordinateSystemReproject(WGS84); SimpleFeatureCollection featureCollection = featureSource.getFeatures(query); SimpleFeatureIterator it = featureCollection.features(); SimpleFeature protoFt = it.next(); if (propertyFields == null) { propertyFields = new ArrayList<String>(); for (Property p : protoFt.getProperties()) { propertyFields.add(p.getName().toString()); it = featureCollection.features(); PointSet ret = new PointSet(featureCollection.size()); int i=0; while (it.hasNext()) { SimpleFeature feature = it.next(); Geometry geom = (Geometry) feature.getDefaultGeometry(); ft.setId(Integer.toString(i)); } else { ft.setId(feature.getProperty(originIDField).getValue().toString());