Hi,
I am trying to filter an IEnumLayer to only contain visible layers. I want to pass this to IFeatureCache2.AddLayers(...). I could not find any public implementations of IEnumLayer. Since we are in C# I wrote a couple of extension methods that convert IEnumLayer to IEnumerable<ILayer> and vice-versa. But passing my custom implementation of IEnumLayer did not seem to make AddLayers(...) happy.
Here is my extensions class:
public static class EnumLayerExtensions
{
public static IEnumerable<ILayer> AsEnumerable(this IEnumLayer layers)
{
layers.Reset();
var layer = layers.Next();
while (layer != null)
{
yield return layer;
layer = layers.Next();
}
}
public static IEnumLayer ToEnumLayer(this IEnumerable<ILayer> layers)
{
return new EnumLayerWrapper(layers);
}
private class EnumLayerWrapper : IEnumLayer
{
private readonly ILayer[] _layers;
private int _index;
public EnumLayerWrapper(IEnumerable<ILayer> layers)
{
_layers = layers.ToArray();
_index = 0;
}
public ILayer Next()
{
return _index < _layers.Length ? _layers[_index++] : null;
}
public void Reset()
{
_index = 0;
}
}
}
using this I can then write the following code:
var visibleLayers = Map.Layers.AsEnumerable().Where(l=>l.Visible);
var cache = new FeatureCacheClass() as IFeatureCache2;
cache.Initialize(point,radius);
cache.AddLayers(visibleLayers.ToEnumLayer(), null);
In the above code, line 04 will just run for ever as-is. If I use the layers straight from the Map, it comes back fairly fast. I have written a handful of unit tests to validate my implementation of ToEnumLayer() and they all seem to pass. Based on all this, I have a couple of questions:
- Is there an out of the box implementation of IEnumLayer that I can use to filter visible layers only to pass to IFeatureCache2.AddLayers()?
- What's wrong with my implementation of IEnumLayer?
Thanks in advance for any help,
Eric.