4

I want to build an IDictionary with the feature layers of my TOC in ArcObjects. So I need key value pairs. The value will be an attribute of the feature layer but I am wondering what to use as key. I can use the layer name but there can theoretically be duplicate layer names in the TOC. Any suggestions on how I can get a unique reference to a feature layer?

Background is that I need to store the layers and their querydefinitions in a custom application that runs in desktop and ArcEngine environment. I don't know the layers before. It could be that the user defines layer groups that have sublayers with the same name. This would lead to an error when storing it in the dictionary.

  • 2
    This is a good starting point: gis.stackexchange.com/questions/7643/… – steffan Dec 4 '14 at 10:41
  • 1
    You do not say if this is a desktop or some sort of server side development. If it is a server side development have a look at the ILayerDescription interface as it has an ID property. I've never done any server side development so not sure if that is a red herring? – Hornbydd Dec 4 '14 at 12:29
  • Define "unique" in the question. What will you be needing the key for? Without knowing that its just a guessing game. – Stefan Dec 4 '14 at 13:15
  • unique so that is serves as a key in a dictionary. I added the info to my original post. – Chris P Dec 4 '14 at 15:27
2

IEnumLayer will build an index for you when you initialize it. It indexes all layers in the order that they appear in the TOC (the top layer will be index 0, the next down will be 1, etc). You can use this index as your key as it will always be unique to the layer.

Here's the code to initialize and access in ArcGIS Desktop:

        ESRI.ArcGIS.ArcMapUI.IMxDocument pDoc;
        pDoc = ArcMap.Document;//get current mxd and set it as pDoc
        ESRI.ArcGIS.Carto.IActiveView activeView;
        activeView = pDoc.ActiveView;  

        IMap pMap = pDoc.FocusMap; //set pMap as current document


        ESRI.ArcGIS.Carto.IEnumLayer pEnumLayer;
        pEnumLayer = pDoc.FocusMap.Layers;  //get all layers in the map


        ESRI.ArcGIS.Carto.ILayer pLayer;
        pLayer = pEnumLayer.Next(); //grab the layer from enumerator

        while (pLayer != null) //loop through all your layers
        {
              //build your dictionary here
              pLayer = pEnumLayer.Next();
        }

If the user will get the chance to add, remove or reorder layers in the course of your application you will of course need to re-initialize IEnumLayers and rebuild your dictionary accordingly as necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.