AddLayers.cs in examples

AddLayers.cs

indent preformatted text by 4 spaces // <summary>
    // Adds all the shapefiles and images with .tif and .png extentions from the specified folder to the map
    // </summary>
    public bool AddLayers(AxMap axMap1, string dataPath)
    {
        axMap1.RemoveAllLayers();
        axMap1.LockWindow(tkLockMode.lmLock);
        try
        {
            string[] files = Directory.GetFiles(dataPath);
            foreach (string file in files)
            {
                int layerHandle = -1;
                if (file.ToLower().EndsWith(".shp"))
                {
                    Shapefile sf = new Shapefile();
                    if (sf.Open(file, null))
                    {
                        layerHandle = axMap1.AddLayer(sf, true);
                    }
                    else
                        MessageBox.Show(sf.ErrorMsg[sf.LastErrorCode]);
                }
                else if (file.ToLower().EndsWith(".tif") ||
                         file.ToLower().EndsWith(".png"))
                {
                    Image img = new Image();
                    if (img.Open(file, ImageType.TIFF_FILE, false, null))
                    {
                        layerHandle = axMap1.AddLayer(img, true);
                    }
                    else
                        MessageBox.Show(img.ErrorMsg[img.LastErrorCode]);
                }
                if (layerHandle != -1)
                    axMap1.set_LayerName(layerHandle, Path.GetFileName(file));
            }
        }
        finally
        {
            axMap1.LockWindow(tkLockMode.lmUnlock);
            Debug.Print("Layers added to the map: " + axMap1.NumLayers);
        }
        return axMap1.NumLayers > 0;
    }

I have some questions regarding this code above:

  1. What is layer? According to the code, JPEG or PNG image and map data which is shapefile can be layer. Am I right?

If I analyse the comment " Adds all the shapefiles and images with .tif and .png extentions from the specified folder to the map"

If I want to add something to the map, something is considered as a layer. Is It right? (something can be shapefile, JPEG, PNG and so on).

I. Why do we give a value -1 to the layerHandle variable in the first time?

II. After opening shapefile, Why do we always call this method:
layerHandle = axMap1.AddLayer(sf, true);

III. After opening image file, Why do we always call this method again:
layerHandle = axMap1.AddLayer(sf, true);

  1. The usual variable initialization
  2. sf - variable shapefile, first, we open a file from disk in it, and then add it to AxMap1 and visualization is already taking place there.
  3. These methods are performed in different ways, look carefully. In the first case, open sf, in the second img !
1 Like