Home arrow GIS Data & Resources arrow Scripts and Code arrow C# Code arrow ArcGIS Server 9.2 - Get Layer Indices by Name
ArcGIS Server 9.2 - Get Layer Indices by Name PDF Print E-mail

Written by Steve Gourley,

This example of ArcGIS Server 9.2 C# code allows a programmer to input a string array of layer names. The output of this function are the indices of the layer positions in the MXD map service. The out variables in c# are a way to return multiple data types and variables. This function outputs the same result in string array and int array format. This is very useful for doing queries and other functions where you need to specify a layer to perform an action on. ESRI has not been very consistent with they're methods since some methods expect a string representation of the index and others expect integers. Thus being the reason both are returned.

One of the nice features of this function, is that it returns the indices in the same order as you place them in your findLayers array. For instance, if you use this function to find the indices of {"roads", "hydrants", "parcels"} and the resulting array returned is {3,1,0}  you know that roads is at position 3, hydrants at 1 and parcels at 0 in your mxd map service.  You can then access your returned array results at position 0 for roads 1 for hydrants and 2 for parcels and you will get the correct index of the layer.

        /// <summary>
        /// string[] findMe = new string[] { "Test", "Test2", "Test3" };
        /// string[] stringLayerIDs;
        /// int[] intLayerIDs;
        /// getLayers(findMe, out intLayerIDs, out stringLayerIDs);
        /// </summary>
        /// <param name="findLayers">string array of layers to find</param>
        /// <param name="stringLayerIDs">out variable of string layer ids</param>
        /// <param name="intLayerIDs">out variable of int layer ids</param>
        //big fix 11-12
        public void getLayers(string[] findLayers, out string[] stringLayerIDs, out int[] intLayerIDs)
        {
            intLayerIDs = new int[findLayers.Length];
            stringLayerIDs = new string[findLayers.Length];

            NameValueCollection nvc = new NameValueCollection();

            foreach (IMapFunctionality mapFunc in m_map.GetFunctionalities())
            {    //skip graphics layers
                if (mapFunc is ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality) continue;

                string[] lids;
                string[] lnames; // get all layers
                mapFunc.GetLayers(out lids, out lnames);

                //populate hash table with layer names
                foreach (string layerName in findLayers)
                {
                    nvc.Add(layerName, null);
                }

                int b = 0, a = 0, index = 0;
                while (b < lnames.Length && index < findLayers.Length)
                {
                    while (a < findLayers.Length)
                    {
                        if (lnames[b] == findLayers[a])
                        {
                            nvc.Set(findLayers[a],lids[b]);

                            index++;
                            break;
                        }
                        a++;
                    }
                    a = 0;
                    b++;
                }
            }
            nvc.CopyTo(stringLayerIDs, 0);

            intLayerIDs = Array.ConvertAll<stringint>(stringLayerIDs, Convert.ToInt32);
        }

Users' Comments  
 

Display 1 of 1 comments

1. Fri, 11-30-2007 at 02:07 PM

Thanks Steve!

Display 1 of 1 comments

Add your comment

27, Nov. 2007
Last Updated ( 30, Nov. 2007 )
 
< Prev   Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for