Adding geometries to a GeometryBag is giving me odd results

Discussion created by peterlen on Jun 10, 2014
Latest reply on Jun 20, 2014 by agray1
I am using Java ArcObjects and I want to go through a FeatureCursor, get each feature's shape, and add the shape to a GeometryBag.  What I do is the following:

GeometryBag gbag = new GeometryBag();
IGeometryCollection gcoll = (IGeometryCollection) gbag;

IFeature feat = featureCursor.nextFeature();
while (feat != null) {
    IGeometry geom = feat.getShape();
    gcoll.addGeometry(geom, null, null);
    feat = featureCursor.nextFeature();

The syntax that I used is based on various posts I found on how to do it.  My problem is that if I go through my collection after adding all of the geometries, and print out the coordinates from the geometry envelope, all of them are the same.  For example, if my feature cursor has three items, my collection will have three items, but all three items will have the same set of coordinates.  I ran some debug where I checked each geom in the loop and also printed the collection information after each addGeometry call.  I could see that I was getting different geometries in the loop but each time I printed the lastest for the collection, it would have the same coordinates for each item, which would be the coordinates for the last item added to the collection.  I ultimately use the gbag as the geometry for a spatial query and it is returning the number of hits that are correct for items that fit within that one coordinate set so it isn't something for how I am printing out the collection items.  This seemed to be an easy task but now this collection is getting whacky on me.  I have tried numerous variation changes but to no avail.

Any thoughts on why the collection is not keeping the different geometry info that was added to it.  It has to be something with overwriting or who knows.

Thanks for any thoughts - Peter