Hello.  I am working with shapefiles and having some confusion with
regard to how I should go about assigning and/or reconciling the
identifier of a newly-created SimpleFeature  instance with the
identifier it seems to be granted when it is added to the feature store.
(I am using 2.5.2).
 
Working from examples on the User Guide, when I go to create a new
feature, I do this (omitting some of the DefaultTransaction boiler
plate)...
 
// Use the factory class to create the feature object.
// (At this point, I expect the new feature to have empty attribute
values, 
// and it does.  So that's good.)
SimpleFeature newFeature = 
 SimpleFeatureBuilder.build(
  this.featureType, 
  new Object[]{}, 
  null); // I can also assign my own ID at this point, but that doesn't
resolve my problem.
 
// Set the geometry on the feature
newFeature.setDefaultGeometry(geom);
 
// I then create a new FeatureCollection to hold the new feature.  (I'm
not sure
// if this is the most correct way to do this, but it seems to work as I
expect.)
FeatureCollection<SimpleFeatureType, SimpleFeature> featureColl =
  FeatureCollections.newCollection();
featureColl.add(newFeature);
 
// Now I have a feature collection with one feature (the new feature) in
it.  I
// then ask the FeatureStore to accept it.  (Incidentally, this all
happens within
// a class that manages a single shapefile, so I keep a reference to the
FeatureStore).
this.featureStore.addFeatures(featureColl);
 
Generally, all of that works well.  The features I add using this method
show up in the shapefile, and if I supply values (instead of the empty
Object array), those show up as well.  I run into problems when I need
to change the persisted state of the  SimpleFeature after it has been
created.
 
>From the example above, if I call newFeature.getID() after calling
this.featureStore.addFeatures(), the value is based on a GUID (something
like: "fid--FB1379A1-DB69-44CB-90A2-6EEF785A4A29_-8000".  And, by the
way, That value isn't a problem for me.  I don't expect to get IDs that
persist across runtime sessions).  
 
However, if after adding this feature (the only feature in the
collection at this point), I iterate over the FeatureCollection and
print out the feature's ID, it comes back as something like
"myTestShapeFile.1".  That ID is also fine with me, with the exception
of the fact that it doesn't match the value returned when I call
newFeature.getID().
 
I run into trouble at the point where I have made changes to the
attributes of newFeature and need to update the persisted data.  From
the documentation, it seems to me that I need to create a filter using
the feature's ID to use with FeatureStore.modifyFeatures() method.  So,
I do something like this (again, this sample omits the transaction code,
exception handling, etc.)...
 
Set<FeatureId> fids = new HashSet<FeatureID>();
fids.add(newFeature.getIdentifier());
Filter filter = ff.id(fids);
// ...create and set the transaction, etc....
this.featureStore.modifyFeatures(attributeDescription, "A new value",
filter);
transaction.commit();
 
I have tried various permutations of the code above.  However, I assume
at this point that the underlying problem is the mismatch between what
the SimpleFeature object (newFeature) thinks its ID is, and what the
FeatureStore thinks its idea is.  To illustrate that point: if, at the
same point, I call this.featureStore.getFeatures(filter).size(), the
result is 0.
 
Can anyone provide me with some advice about how to reconcile those two?
Or, is there some way that I may simply persist changes to the
SimpleFeature object without needing the filter and the modifyFeatures()
method?  I shall indeed be most grateful for any pointers you can
provide.
 
Many thanks.
 
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to