# AddLayers.cs in examples

**URL:** <https://mapwindow.discourse.group/t/addlayers-cs-in-examples/617>\
**Category:** MapWinGIS\
**Created:** [June 15, 2020, 3:45am UTC](https://mapwindow.discourse.group/t/addlayers-cs-in-examples/617 "2020-06-15T03:45:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kayumiy](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/kayumiy/32/233_2.png) [@Kayumiy](https://mapwindow.discourse.group/u/Kayumiy)\
**Post date:** [June 15, 2020, 3:45am UTC](https://mapwindow.discourse.group/t/addlayers-cs-in-examples/617/1 "2020-06-15T03:45:19Z")

</div>

[AddLayers.cs](https://www.mapwindow.org/documentation/mapwingis4.9/_add_layers_8cs-example.html)

```
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;
    }
```

---

<div class="post-metadata">

**Author:** ![Kayumiy](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/kayumiy/32/233_2.png) [@Kayumiy](https://mapwindow.discourse.group/u/Kayumiy)\
**Post date:** [June 15, 2020, 3:56am UTC](https://mapwindow.discourse.group/t/addlayers-cs-in-examples/617/2 "2020-06-15T03:56:39Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Kayumiy](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/kayumiy/32/233_2.png) [@Kayumiy](https://mapwindow.discourse.group/u/Kayumiy)\
**Post date:** [June 15, 2020, 4:08am UTC](https://mapwindow.discourse.group/t/addlayers-cs-in-examples/617/3 "2020-06-15T04:08:09Z")

</div>

1. ![image](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/4f2c7c168f64e28f8d31305326ae7596f32407fd.png)

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);`

---

<div class="post-metadata">

**Author:** ![mainxt](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mainxt/32/33_2.png) [@mainxt](https://mapwindow.discourse.group/u/mainxt)\
**Post date:** [June 15, 2020, 10:12am UTC](https://mapwindow.discourse.group/t/addlayers-cs-in-examples/617/4 "2020-06-15T10:12:10Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![pmeems](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/pmeems/32/9_2.png) [@pmeems](https://mapwindow.discourse.group/u/pmeems)\
**Post date:** [January 5, 2022, 3:09pm UTC](https://mapwindow.discourse.group/t/addlayers-cs-in-examples/617/5 "2022-01-05T15:09:04Z")

</div>


