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; it.close(); } catch (Exception ex) { LOG.error("Error loading population from shapefile: {}", ex.getMessage());
SimpleFeatureIterator it = featureCollection.features(); SimpleFeature protoFt = it.next(); if (propertyFields == null) { propertyFields = new ArrayList<String>(); it = featureCollection.features(); while (it.hasNext()) { SimpleFeature feature = it.next(); Geometry geom = (Geometry) feature.getDefaultGeometry();
@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")); }
@Test public void testSimpleRename() throws IOException { SimpleFeatureSource fs = store.getFeatureSource(BUILDINGS.getLocalPart()); SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.init(fs.getSchema()); tb.setName("houses"); SimpleFeatureType target = tb.buildFeatureType(); SimpleFeatureSource retyped = RetypingFeatureSource.getRetypingSource(fs, target); assertTrue(retyped instanceof SimpleFeatureLocking); assertEquals(target, retyped.getSchema()); assertEquals(target, ((DataStore) retyped.getDataStore()).getSchema("houses")); assertEquals(target, retyped.getFeatures().getSchema()); SimpleFeatureIterator it = retyped.getFeatures().features(); SimpleFeature f = it.next(); it.close(); assertEquals(target, f.getType()); }
/** Helper method that crestes a lsit of features from a collection (iterator) of features. */ private List<SimpleFeature> getFeatures(SimpleFeatureCollection collection) { List<SimpleFeature> features = new ArrayList<>(); try (SimpleFeatureIterator iterator = collection.features()) { while (iterator.hasNext()) { features.add(iterator.next()); } } return features; }
@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(); // test a non mapped attribute String newDescription = ((String) original.getAttribute("description")) + " xxx"; store.modifyFeatures( original.getFeatureType().getDescriptor("description"), newDescription, fidFilter); SimpleFeature modified = store.getFeatures(fidFilter).features().next(); assertEquals(newDescription, modified.getAttribute("description")); // test a mapped attribute MultiPoint mpo = (MultiPoint) original.getAttribute("pointProperty"); MultiPoint mpm = mpo.getFactory().createMultiPoint(new Coordinate[] {new Coordinate(10, 12)}); store.modifyFeatures( original.getFeatureType().getDescriptor("pointProperty"), mpm, fidFilter); modified = store.getFeatures(fidFilter).features().next(); assertTrue(mpm.equalsExact((Geometry) modified.getAttribute("pointProperty"))); }
public void testFeatureMembers() throws Exception { Element featureCollection = GML3MockData.element(TEST.TestFeatureCollection, document, document); Element featureMember = GML3MockData.element(GML.featureMembers, document, featureCollection); Element feature = GML3MockData.feature(document, featureMember); feature.setAttributeNS(GML.NAMESPACE, "id", "fid.1"); feature = GML3MockData.feature(document, featureMember); feature.setAttributeNS(GML.NAMESPACE, "id", "fid.2"); SimpleFeatureCollection fc = (SimpleFeatureCollection) parse(); assertNotNull(fc); assertEquals(2, fc.size()); SimpleFeatureIterator i = fc.features(); try { SimpleFeature f = (SimpleFeature) i.next(); assertEquals("fid.1", f.getID()); f = (SimpleFeature) i.next(); assertEquals("fid.2", f.getID()); } finally { i.close(); } } }
static void accept(SimpleFeatureCollection collection, FeatureVisitor[] visitors) { SimpleFeatureIterator iterator = collection.features(); try { while (iterator.hasNext()) { SimpleFeature feature = (SimpleFeature) iterator.next(); for (int i = 0; i < visitors.length; i++) { FeatureVisitor visitor = visitors[i]; visitor.visit(feature); } } } finally { iterator.close(); } }
@Override public void addGranules(SimpleFeatureCollection granules) { checkTransaction(); SimpleFeatureIterator features = granules.features(); boolean firstSchemaCompatibilityCheck = false; while (features.hasNext()) { SimpleFeature feature = features.next(); if (!firstSchemaCompatibilityCheck) { firstSchemaCompatibilityCheck = true; checkSchemaCompatibility(feature); } try { catalog.addGranule(typeName, feature, transaction); } catch (IOException e) { throw new RuntimeException( "Exception occurred while adding granules to the catalog", e); } } }
public void testFeatureMember() throws Exception { Element featureCollection = GML3MockData.element(TEST.TestFeatureCollection, document, document); Element featureMember = GML3MockData.element(GML.featureMember, document, featureCollection); Element feature = GML3MockData.feature(document, featureMember); feature.setAttributeNS(GML.NAMESPACE, "id", "fid.1"); featureMember = GML3MockData.element(GML.featureMember, document, featureCollection); feature = GML3MockData.feature(document, featureMember); feature.setAttributeNS(GML.NAMESPACE, "id", "fid.2"); SimpleFeatureCollection fc = (SimpleFeatureCollection) parse(); assertNotNull(fc); assertEquals(2, fc.size()); SimpleFeatureIterator i = fc.features(); try { SimpleFeature f = (SimpleFeature) i.next(); assertEquals("fid.1", f.getID()); f = (SimpleFeature) i.next(); assertEquals("fid.2", f.getID()); } finally { i.close(); } }
/** * Navigate the collection and call vistor.visit( Feature ) for each element in the collection. * * @param collection the SimpleFeatureCollection containing the features we want to visit * @param visitor the visitor which already knows which attributes it wants to meet */ static void accept(SimpleFeatureCollection collection, FeatureVisitor visitor) { SimpleFeatureIterator iterator = collection.features(); try { while (iterator.hasNext()) { SimpleFeature feature = (SimpleFeature) iterator.next(); visitor.visit(feature); } } finally { iterator.close(); } }
protected Set<String> collectFeatureIds(SimpleFeatureCollection fc) { Set<String> identifiers = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); try (SimpleFeatureIterator fi = fc.features()) { while (fi.hasNext()) { SimpleFeature sf = fi.next(); identifiers.add(sf.getID()); } } return identifiers; } }
/** * item at the specified index. * * @param index index of item * @return the item at the specified index. * @throws IndexOutOfBoundsException if index is not between 0 and size */ public SimpleFeature get(int position) { FeatureId fid = index.get(position); if (collection instanceof RandomFeatureAccess) { RandomFeatureAccess random = (RandomFeatureAccess) collection; return random.getFeatureMember(fid.getID()); } SimpleFeatureIterator it = collection.features(); try { while (it.hasNext()) { SimpleFeature feature = (SimpleFeature) it.next(); if (id.equals(feature.getID())) { return feature; } } throw new IndexOutOfBoundsException(); } finally { it.close(); } }
private void count(SimpleFeatureStore store, int expected) throws IOException, IllegalAttributeException { int i = 0; for (SimpleFeatureIterator reader = store.getFeatures().features(); reader.hasNext(); ) { reader.next(); i++; } assertEquals("Number of known feature as obtained from reader", expected, i); } }
public void testGetFeatureStoreModifyFeatures1() throws IOException { SimpleFeatureStore road = (SimpleFeatureStore) data.getFeatureSource("road"); AttributeDescriptor descriptor = roadType.getDescriptor("name"); road.modifyFeatures(descriptor.getName(), "changed", rd1Filter); SimpleFeatureCollection results = road.getFeatures(rd1Filter); assertEquals("changed", results.features().next().getAttribute("name")); }