ThinkGeo.com    |     Documentation    |     Premium Support

Can not remove Feature from InternalFeatures

Hi, following code does not remove the Feature from the InternalFeatures of our InMemoryFeatureLayer :

    await this.Map.DeHighlightFeature(foundFeatures[0], true);


    public async Task DeHighlightFeature(Feature f, bool refresh)
    {
        this.HighlightLayer.Open();
        this.HighlightLayer.InternalFeatures.Remove(f);
        this.HighlightLayer.Close();

        if (refresh)
            await this.RefreshAsync(this.HighlightOverlay);
    }

Highlight layer before (and after) removal:

Any idea? I have tried removing the Open / Close of the layer and it does not make a difference. It used to work a few versions back. I have also tried removing it with the key (id), which doesn’t work either.
The ThinkGeo Log does not give any outputs sadly.

Hi Julian,

I think it’s because foundFeatures[0] is a newly created feature, it was not added into the collection, even it share the value with an item in the collection. The following code works fine in my test:

        InMemoryFeatureLayer layer = new InMemoryFeatureLayer();
        var features = new Collection<Feature>();
        var feature = new Feature();
        features.Add(feature);
        layer.InternalFeatures.Add(feature);
        layer.InternalFeatures.Remove(features[0]);

Can you review how foundFeatures[0] is created?

Thanks,
Ben

Hi Ben, thanks, that was the issue, I guess we have to keep track of the Features and remove them like that. Any idea why it doesn’t work even though the Id matches? I thought it works with only a key as well?

Also, could you please give me a quick explanation on when it is necessary to Open() and Close() a layer? The code I inherited code had Open and Close around it however it also works without it.

Thanks!

Hi Julian,

There are two ways of editing for an InMemoryFeatureLayer

  1. This is the “standard” way cross all the layers.
    layer.EditTools.BeginTransaction();
    // between BeginTransaction() and CommitTranscation(), you can add/edit/delete a feature
    layer.EditTools.Delete(“id”); // here it uses id of the feature
    layer.EditTools.CommitTransaction();

  2. But for InMemoryFeatureLayer specifically, you can use the InternalFeatures collection as a shortcut. Because Feature is a reference type, it doesn’t work if the feature is newly created even if its Id exists in the collection. You can loop through the internalFeatures and remove the one with the same id.

Open() needs to be called (only once) before querying the features(using the methods within layer.QueryTools.). It is for initializing the connection to the data source so in fact it didn’t do anything for InMemoryFeatureLayer, you don’t need it if you only uses InternalFeatures collection.

Thanks,
Ben

Hi Ben, thanks, just to verify that I understood correctly, so everytime I want to edit a layer (any layer, not just InMemoryFeatureLayer), I need to call

layer.EditTools.BeginTransaction(); 
// do QueryTools Manipulation
layer.EditTools.CommitTransaction();

And it’s probably best to use a guard with

if (!layer.IsOpen()) layer.Open()

before any function within the QueryTools namespace.

Thanks for your help!
J

That sounds good! 2 quick things:

  1. I think it’s a typo but it should be
    // do EditTools (instead of QueryTools) Manipulations between Begin and Commit Transactions

  2. layer.Open() internally will check IsOpen property and avoid calling the actual Open code if IsOpen is true.

Thanks,
Ben

1 Like

Gotscha, thanks Ben!

Sure, Any Time! :slight_smile: