From: <svn...@os...> - 2012-06-21 01:43:15
|
Author: jive Date: 2012-06-20 18:43:08 -0700 (Wed, 20 Jun 2012) New Revision: 38822 Modified: trunk/docs/src/main/java/org/geotools/data/SimpleFeatureStoreExamples.java trunk/docs/user/library/data/featuresource.rst Log: update feature collection doc examples around addFeature including batch feature events Modified: trunk/docs/src/main/java/org/geotools/data/SimpleFeatureStoreExamples.java =================================================================== --- trunk/docs/src/main/java/org/geotools/data/SimpleFeatureStoreExamples.java 2012-06-20 22:47:41 UTC (rev 38821) +++ trunk/docs/src/main/java/org/geotools/data/SimpleFeatureStoreExamples.java 2012-06-21 01:43:08 UTC (rev 38822) @@ -1,15 +1,23 @@ package org.geotools.data; +import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Set; +import org.geotools.data.collection.ListFeatureCollection; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureStore; import org.geotools.factory.CommonFactoryFinder; import org.geotools.factory.GeoTools; +import org.geotools.feature.FeatureCollections; +import org.geotools.feature.simple.SimpleFeatureBuilder; +import org.geotools.geometry.jts.GeometryBuilder; import org.opengis.feature.Feature; import org.opengis.feature.FeatureVisitor; +import org.opengis.feature.simple.SimpleFeature; +import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.filter.Filter; import org.opengis.filter.FilterFactory; import org.opengis.filter.identity.FeatureId; @@ -20,6 +28,87 @@ String typeName; +private void addFeaturesExample() throws Exception { + // addExample start + SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource( typeName ); + + SimpleFeatureType featureType = store.getSchema(); + + SimpleFeatureBuilder build = new SimpleFeatureBuilder(featureType); + GeometryBuilder geom = new GeometryBuilder(); + + List<SimpleFeature> list = new ArrayList<SimpleFeature>(); + list.add( build.buildFeature("fid1", new Object[]{ geom.point(1,1), "hello" } ) ); + list.add( build.buildFeature("fid2", new Object[]{ geom.point(2,3), "martin" } ) ); + SimpleFeatureCollection collection = new ListFeatureCollection(featureType, list); + + Transaction transaction = new DefaultTransaction("Add Example"); + store.setTransaction( transaction ); + try { + store.addFeatures( collection ); + transaction.commit(); // actually writes out the features in one go + } + catch( Exception eek){ + transaction.rollback(); + } + // addExample end +} + +private void addFeatureIdExample(SimpleFeatureCollection collection ) throws Exception { + // addFeatureIdExample start + Transaction transaction = new DefaultTransaction("Add Example"); + SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName); + store.setTransaction(transaction); + try { + List<FeatureId> added = store.addFeatures( collection ); + System.out.println( added ); // prints out the temporary feature ids + + transaction.commit(); + System.out.println( added ); // prints out the final feature ids + + Set<FeatureId> selection = new HashSet<FeatureId>( added ); + FilterFactory ff = CommonFactoryFinder.getFilterFactory(); + Filter selected = ff.id( selection ); // filter selecting all the features just added + } + catch( Exception problem){ + transaction.rollback(); + throw problem; + } + // addFeatureIdExample end +} + +private void addFeaturesEvents(SimpleFeatureCollection collection ) throws Exception { + // addEvents start + Transaction transaction = new DefaultTransaction("Add Example"); + SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName); + store.setTransaction(transaction); + + class CommitListener implements FeatureListener { + public void changed(FeatureEvent featureEvent) { + if (featureEvent instanceof BatchFeatureEvent ){ + BatchFeatureEvent batchEvent = (BatchFeatureEvent) featureEvent; + + System.out.println( "area changed:" + batchEvent.getBounds() ); + System.out.println( "created fids:" + batchEvent.fids ); + } + else { + System.out.println( "bounds:" + featureEvent.getBounds() ); + System.out.println( "change:" + featureEvent.filter ); + } + } + } + CommitListener listener = new CommitListener(); + store.addFeatureListener( listener ); + try { + List<FeatureId> added = store.addFeatures( collection ); + transaction.commit(); + } + catch( Exception problem){ + transaction.rollback(); + throw problem; + } + // addEvents end +} private void removeExample() throws Exception { // removeExample start Transaction transaction = new DefaultTransaction("removeExample"); Modified: trunk/docs/user/library/data/featuresource.rst =================================================================== --- trunk/docs/user/library/data/featuresource.rst 2012-06-20 22:47:41 UTC (rev 38821) +++ trunk/docs/user/library/data/featuresource.rst 2012-06-21 01:43:08 UTC (rev 38822) @@ -197,30 +197,30 @@ Each feature has an identifier that is intended to be unique in agreement with the WFS specification. For most implementations the FeatureID is assigned when the feature is added - (and even more interestingly when it is committed!):: - - try { - Set<FeatureId> added = store.addFeatures( collection ); - System.out.println( added ); // prints out the temporary feature ids - - transaction.commit(); - System.out.println( added ); // prints out the final feature ids - } - catch( Exception eek){ - transaction.rollback(); - } - FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); - Filter selected = ff.id( added ); // filter selecting all the features just added + (and even more interestingly when it is committed!): - Please be careful; if you write down a FeatureID when you created in memory the feature you will lose - track of it (since the ID not actually assigned until the feature is stored for safekeeping). - You need to update any filters you have to use the real FeatureID after the commit has happened. + .. literalinclude:: /../src/main/java/org/geotools/data/SimpleFeatureStoreExamples.java + :language: java + :start-after: // addExample start + :end-before: // addExample end - To help you with this: + FeatureID are assigned during the commit process. While we make an attempt to determine an + appropriate ID prior to commit we ask that you wait until commit() is finished before + writing down the identifiers of the added content. - * The commit feature event has the final set of identifiers - * Any FeatureID instances in memory can get their internal value updated to reflect the final official value - + The FeatureID instances returned by addFeatures are updated to reflect the final value + provided during commit. If you need to perform this step yourself you can listen for a + BatchFeatureEvent as shown below. + +* FeatureEvents are sent out when adding: + + .. literalinclude:: /../src/main/java/org/geotools/data/SimpleFeatureStoreExamples.java + :language: java + :start-after: // addEvents start + :end-before: // addEvents end + + The BatchFeatureEvent sent out during commit contains the final set of identifiers + * Handling of FeatureID Yourself Recentlyy a few datastore implementations (JDBCNG and Property) added support for a "Hint" allowing |