We want to update some features of a Layer. We have used the following Code to update features but not successful.

Firstly Get SimpleFeatureSource by a Feature Layer.

SimpleFeatureSource sfSource = (SimpleFeatureSource) Lyr.getFeatureSource();

After quried on it we got some data in the form of SimpleFeatureCollection.

org.opengis.filter.Filter flt = ff.within(ff.property("the_geom"),ff.literal(polygon));
SimpleFeatureCollection sfc = sfSource.getFeatures(flt);
SimpleFeatureIterator iterator = sfc.getFeatures().features();          
while (iterator.hasNext()) {
   SimpleFeature feature = iterator.next(); 
   feature.setAttribute(0, object);

If we do this feature's attribute will update and reflect in sfc (SimpleFeatureCollection) but not reflect in sfSource (SimpleFeatureSource).


Transaction transaction = new DefaultTransaction("Transaction");
    Maplayer MpLayer = this._MapListLayers.get(cmbLayers.getSelectedItem().toString());
    FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
    Filter flt = ff.id(ff.featureId("long cl0805143437.44177"), ff.featureId("long cl0805143437.44178"),
            ff.featureId("long cl0805143437.44179"), ff.featureId("long cl0805143437.44180"),
            ff.featureId("long cl0805143437.44181"), ff.featureId("long cl0805143437.44182"),
            ff.featureId("long cl0805143437.44183"), ff.featureId("long cl0805143437.44184"),
            ff.featureId("long cl0805143437.44185"), ff.featureId("long cl0805143437.44186"),
            ff.featureId("long cl0805143437.44187"), ff.featureId("long cl0805143437.44188"),
            ff.featureId("long cl0805143437.44189"), ff.featureId("long cl0805143437.44190"));
    try {
        MpLayer._SimpleStore.modifyFeatures("Equipment", new String("Equipment"), flt);
    } catch (Exception e) {

As per your comments we have tried upper code but results me in an error shown below:-

java.lang.RuntimeException: OGR Error: General Error at org.gdal.ogr.ogrJNI.Layer_SetFeature(Native Method) at org.gdal.ogr.Layer.SetFeature(Layer.java:145) at OgrSource.JniOGR.LayerSetFeature(JniOGR.java:284) at OgrSource.OGRDirectFeatureWriter.write(OGRDirectFeatureWriter.java:114) at org.geotools.data.InProcessLockingManager$1.write(InProcessLockingManager.java:337) at org.geotools.data.store.DiffTransactionState.commit(DiffTransactionState.java:161)


GeoTools uses a streaming feature source methodology, so whenever you ask the FeatureSource for new features it rereads the Datastore. This is especially important if you are using a filter or all sorts of surprises may occur.

If you want to modify a feature permanently you need to use a FeatureStore and call the modifyFeatures method. So your example above will become something like:

Transaction transaction = new DefaultTransaction("Example1");
SimpleFeatureStore store = (SimpleFeatureStore) sfSource;
store.setTransaction( transaction );

Filter flt = ff.within(ff.property("the_geom"),ff.literal(polygon));

SimpleFeatureType featureType = store.getSchema();
try {
   store.modifyFeatures( "0", object, filter );
catch( Exception eek){
  • After Apply upper code following error will generate:- ERROR 6: SetFeature() must be used on a feature with a FID. java.lang.RuntimeException: OGR Error: General Error at org.gdal.ogr.ogrJNI.Layer_SetFeature(Native Method) at org.gdal.ogr.Layer.SetFeature(Layer.java:145) at org.geotools.data.ogr.jni.JniOGR.LayerSetFeature(JniOGR.java:287) at org.geotools.data.ogr.OGRDirectFeatureWriter.write(OGRDirectFeatureWriter.java:137) at org.geotools.data.InProcessLockingManager$1.write(InProcessLockingManager.java:337) – Shubham Goyal Dec 27 '16 at 6:36
  • Does your feature have a fid? – Ian Turton Dec 27 '16 at 12:15
  • That doesn't actually verify anything, check for null – Ian Turton Dec 27 '16 at 12:42
  • Yes each feature has FeatureID. Verified by following Code :- simplefeature.getIdentifier(); – Shubham Goyal Dec 27 '16 at 12:42
  • I have checked thoroughly but i didnt find any null values in ID attribute. – Shubham Goyal Dec 27 '16 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.