ThinkGeo.com    |     Documentation    |     Premium Support

Layers groupped in GroupLayer aren't accesible by key

Hello again!


Why, after I added GroupLayer for my layers, I can't find any FeatureLayer and even GroupLayer by its key value?


Every time I get something like KeyNotFound Exception. I know, that layer with that key exists.


When I debug my application, GroupLayer has layers but none are being found by LayerOverlay.Layers[key] or WinFormsMap.FindFeatureLayer(key)


Am I doing something wrong?


For example, code like this doesn't work with GroupLayer:


LayerOverlay lo = (LayerOverlay)MapControl.CustomOverlays[0]; 


GroupLayer gl = (GroupLayer)lo.Layers[e.Node.Name];


This code also doesn't work with groupped layers, but works with the same layers without GroupLayer:


FeatureLayer checkedLayer = _refMapControl.FindFeatureLayer(childNode.Name);


where childNode.Name holds key for layer


Thanks in advance


Łukasz


 



Łukasz,


I can get the layer from Layer Collection by key using following code
 


LayerOverlay layerOverlay = (LayerOverlay)winformsMap1.CustomOverlays[0];
GroupLayer groupLayer = (GroupLayer)lo.Layers["groupLayer"];


 
Could you attach some demo to recreate this issue?
 
The API FindFeatureLayer() returns the FeatureLayer inside the StaticOverlay ,DynamicOverlay or CustomerOverlays’ Layers. In your case the GroupLayer is inside the CustomerOverlays, but the GroupLayer is not a FeatureLayer so the API won’t return it back even though there are some FeatureLayers inside the GroupLayer.
 
Thanks,
ThinkGeo Support

So, if I have GroupLayer and some FeatureLayers in this GroupLayer, than I can't access them through FindFeatureLayer method, or I can't access only the GroupLayer?


I have attached some demo, which is not working for me. I've removed all references from project folder.


Ther is Layer with key "Parcels". When I'm trying to chage the visibility of this layer it' s not being found.


Oh, I'm taking these layers from Postgres data source.


Regards


Łukasz


 



618-GroupLayerTest.zip (29.9 KB)

Łukasz,


FindFeatureLayer method will loop through all the layers in each LayerOverlay to find the FeatureLayer by key.

The following tree view shows which layer is accessible by the API and which is not;

winformMap

    -CustomerOverlays

        --Overlay1

            ---FeatureLayer1 (Can be found by key)

            ---GroupLayer1   (Can NOT be found as this is not a FeatureLayer)

                ----FeatureLayer2 (Can NOT be found as this layer is inside the GroupLayer)

                ----FeatureLayer3 (Can NOT be found as this layer is inside the GroupLayer)

            --Overlay2

                ---FeatureLayer4 (Can be found by key)

                ---RasterLayer1   (Can NOT be found as this is not a FeatureLayer)


FindFeatureLayer method needs to pass in a string as parameter; it should be the key specified when you add the layer into a LayerOverlay. 

There are two overloads for the add method, one uses GUID and the other uses specified sting as the key.



overlay.Layers.Add(new ShapeFileFeatureLayer());
overlay.Layers.Add("layer1", new ShapeFileFeatureLayer());


Following code shows how to get the layer by key.




LayerOverlay layerOverlay = new LayerOverlay();
FeatureLayer featureLayer1 = new ShapeFileFeatureLayer();
featureLayer1.Name = "Name1";
layerOverlay.Layers.Add("key1", featureLayer1);

FeatureLayer testLayer = winformsMap1.FindFeatureLayer("key1"); // Here you need to pass in the key not the name of the layer.


Thanks,

ThinkGeo Support

 



Yes, I’m using the key, which is the same like layer name. 
  
 So, method FindFeatureLayer will not find layers groupped in GroupLayer at all. 
  
 I have to check if current layer is GroupLayer and then find FeatureLayer in GroupLayer.Layers by key. 
  
 Thanks

Yes, 
  
 You can create your own method to loop through GroupLayer.Layers to find the FeatureLayer by key or by layer name. 
  
 Thanks, 
 ThinkGeo Support