# Add Layer from Geodatabase not working

**URL:** <https://mapwindow.discourse.group/t/add-layer-from-geodatabase-not-working/1193>\
**Category:** MapWinGIS\
**Created:** [January 4, 2022, 7:21pm UTC](https://mapwindow.discourse.group/t/add-layer-from-geodatabase-not-working/1193 "2022-01-04T19:21:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![alrajhi20](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@alrajhi20](https://mapwindow.discourse.group/u/alrajhi20)\
**Post date:** [January 4, 2022, 7:21pm UTC](https://mapwindow.discourse.group/t/add-layer-from-geodatabase-not-working/1193/1 "2022-01-04T19:21:36Z")

</div>

Hi all, it might old but still struggling from it.  
I have an ESRI geo-database that has Roads data. I want to display it in axMap control.  
This is my function:

```auto
private void openGDB()
{
    try
    {
        var ogr = new OgrDatasource();
        string source = @"path\data.gdb";
        if (!ogr.Open(source)) // no error here
        {
            MessageBox.Show("Failed to open data source. Err: " + ogr.GdalLastErrorMsg);
        }
        else
        {
            int count = ogr.LayerCount; // This displays count properly
            Debug.WriteLine(count);

            var layer = new OgrLayer();
            layer = ogr.GetLayerByName("Road");

            int a = axMap1.AddLayer(layer, true) ; // here a gets number 1
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

```

Code works without error but data are not displayed.  
When I debug the code, the only error I get is the following at layer var:  
GdalLastErrorMsg = “Unable to open EPSG support file gcs.csv. Try setting the GDAL\_DATA environment variable to point to the directory containing EPSG csv files.” when layer created. But when I check layer’s values again in the same run, that error disappears.  
When I add shapefile to axMap it works fins, but from geo-database it does not. All layers has the same projection.  
Also as long as I remember, while I was installing mapwingis, I was asked while installation if I want to register variables to system, and I selected yes to that. So, GdalLastErrorMsg kind of make no sense to me!  
Can someone enlighten me on what is wrong with my code.

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [January 4, 2022, 8:00pm UTC](https://mapwindow.discourse.group/t/add-layer-from-geodatabase-not-working/1193/2 "2022-01-04T20:00:12Z")

</div>

Hello @alrajhi20

First try something more generic, like

```
for (int i = 0; i < count; i++)
{
    var layer = ogr.GetLayer(i);
    Debug.Print("Layer name: " + layer.Name);
    int a = axMap1.AddLayer(layer, true);
}

```

Another option is if you have MapWindow5 installed (which uses the OCX), you could try loading it there.

If neither of these options reveal anything new, would you mind posting the GDB and I can debug it on this end?

Regards,  
Jerry.

---

<div class="post-metadata">

**Author:** ![alrajhi20](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@alrajhi20](https://mapwindow.discourse.group/u/alrajhi20)\
**Post date:** [January 5, 2022, 6:05am UTC](https://mapwindow.discourse.group/t/add-layer-from-geodatabase-not-working/1193/3 "2022-01-05T06:05:57Z")

</div>

Dear Mr. Jerry, Thank u for your reply. I added the code and all layers got printed successfully. But it does not appear in the axMap. The other options you suggested, I installed mapwindow5, but when I navigate to any esri geo-database, it only enables me to select geo-database not feature class; not expandable.  
See attached picture.  
 ![mwg](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/325ece3399cd6fce9eede6e319a2866557455397.png)  
It shows all shapefiles properly. Do you think mapwindow5 has same issue in my machine as mine?  
Please advise, Thanks

---

<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, 9:20am UTC](https://mapwindow.discourse.group/t/add-layer-from-geodatabase-not-working/1193/4 "2022-01-05T09:20:56Z")

</div>

Can you have a look if the `GDAL_DATA` variable is correctly set on your machine?  
It should point to the `gdal-data` subfolder of the folder MapWinGIS is installed.  
If it points to the correct location can you check if the file `gcs.csv` is in it?

---

<div class="post-metadata">

**Author:** ![alrajhi20](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@alrajhi20](https://mapwindow.discourse.group/u/alrajhi20)\
**Post date:** [January 6, 2022, 4:25am UTC](https://mapwindow.discourse.group/t/add-layer-from-geodatabase-not-working/1193/5 "2022-01-06T04:25:21Z")

</div>

Hi Mr. Pmeems,  
I checked GDAL\_DATA variable but wasnt there. I added it with the proper folder where mapwingis is installed. I also found gcs.csv in that folder. But still data not shown.

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

![var](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/7f68650dc4cf6f3f818d622d3aaf48d89579dbf3.png)

---

<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 10, 2022, 9:23am UTC](https://mapwindow.discourse.group/t/add-layer-from-geodatabase-not-working/1193/6 "2022-01-10T09:23:14Z")

</div>

Can you share (part of) your gdb file? So we can debug?

---

<div class="post-metadata">

**Author:** ![alrajhi20](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@alrajhi20](https://mapwindow.discourse.group/u/alrajhi20)\
**Post date:** [January 11, 2022, 7:19am UTC](https://mapwindow.discourse.group/t/add-layer-from-geodatabase-not-working/1193/7 "2022-01-11T07:19:12Z")

</div>

Yes Mr. pmeems,  
Please have a look to gdb attached. I sent sample data.  
[data.zip](https://mapwindow.discourse.group/uploads/short-url/wRootV4FZ7H0c6ZaRauQnERJvoj.zip) (2.0 MB)

---

<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 12, 2022, 10:19am UTC](https://mapwindow.discourse.group/t/add-layer-from-geodatabase-not-working/1193/8 "2022-01-12T10:19:56Z")

</div>

I added this code behind a button in my test application:

```auto
private void btnLoadGdb_Click(object sender, EventArgs e)
{
    var ogr = new OgrDatasource();
    var utils = new Utils();

    try
    {
        axMap1.Clear();

        const string source = @"path\data.gdb";
        if (!ogr.Open(source)) 
        {
            txtResults.Text += $@"{Environment.NewLine}Failed to open data source. Err: {ogr.GdalLastErrorMsg}";
        }
        else
        {
            var count = ogr.LayerCount; 
            txtResults.Text += $@"{Environment.NewLine}Number of layers: {count}";

            for (var i = 0; i < count; i++)
            {
                var layer = ogr.GetLayer(i);
                txtResults.Text += $@"{Environment.NewLine}Adding layer name: {layer.Name}{Environment.NewLine}";
                axMap1.AddLayer(layer, true);

                // Set some coloring:
                var buffer = layer.GetBuffer();

                switch (layer.ShapeType2D)
                {
                    case ShpfileType.SHP_POINT:
                        buffer.DefaultDrawingOptions.FillColor = utils.ColorByName(tkMapColor.Red);
                        buffer.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.DarkRed);
                        break;
                    case ShpfileType.SHP_POLYLINE:
                        buffer.DefaultDrawingOptions.FillColor = utils.ColorByName(tkMapColor.Green);
                        buffer.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.DarkGreen);
                        break;
                    case ShpfileType.SHP_POLYGON:
                        buffer.DefaultDrawingOptions.FillColor = utils.ColorByName(tkMapColor.Blue);
                        buffer.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.BlueViolet);
                        buffer.DefaultDrawingOptions.FillTransparency = 200;
                        break;
                }
            }
        }

        var numLayers = axMap1.NumLayers;
        txtResults.Text += $@"{Environment.NewLine}Number of loaded layers: {numLayers}";

        // Change order of layers: points on top, polygons at the bottom:
        for (var i = 0; i < numLayers; i++)
        {
            var layerHandle = axMap1.get_LayerHandle(i);
            var sf = axMap1.get_Shapefile(layerHandle);
            switch (sf.ShapefileType2D)
            {
                case ShpfileType.SHP_POINT:
                    axMap1.MoveLayerTop(layerHandle);
                    break;
                case ShpfileType.SHP_POLYGON:
                    axMap1.MoveLayerBottom(layerHandle);
                    break;
            }
        }
    }
    catch (Exception ex)
    {
        txtResults.Text += $@"{Environment.NewLine}Exception: {ex}";
        txtResults.Text += $@"{Environment.NewLine}OgrDatasource error: {ogr.GdalLastErrorMsg}";
    }
}

```

And the result is:

 ![image](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/985df919ec107f827061e0096cd4abb75e03153d.jpeg)

The logging says:

```auto
Number of layers: 3
Adding layer name: Enclosure 
4 Converting geometries...8 Converting geometries...13 Converting geometries...17 Converting geometries...21 Converting geometries... [...] ...100 Converting geometries...100 Completed Error: GDAL FAILURE: SELECT from table mw_styles failed, no such table/featureclass.
Adding layer name: BuiltUpArea 
6 Converting geometries...12 Converting geometries...18 Converting geometries...25 Converting geometries...31 Converting geometries...[...] ...100 Converting geometries...100 Completed Error: GDAL FAILURE: SELECT from table mw_styles failed, no such table/featureclass.
Adding layer name: Road 
1 Converting geometries...2 Converting geometries...3 Converting geometries...4 Converting geometries...5 Converting geometries...6 Converting geometries...[...] ...100 Converting geometries...100 Completed Error: GDAL FAILURE: SELECT from table mw_styles failed, no such table/featureclass.
Number of loaded layers: 3

```

The FAILURE message is not a problem. That table is used to style the data and is optional.

It looks OK to me

---

<div class="post-metadata">

**Author:** ![alrajhi20](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@alrajhi20](https://mapwindow.discourse.group/u/alrajhi20)\
**Post date:** [January 12, 2022, 3:43pm UTC](https://mapwindow.discourse.group/t/add-layer-from-geodatabase-not-working/1193/9 "2022-01-12T15:43:40Z")

</div>

This works perfect. Thank you Mr. pmeems, you really saved me a lot of time. Very beautiful.  
Though having it working, I tried to modify my code to match yours, but it never works with other than your code!!!.  
Any way, Thank again

---

<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 13, 2022, 3:21pm UTC](https://mapwindow.discourse.group/t/add-layer-from-geodatabase-not-working/1193/10 "2022-01-13T15:21:05Z")

</div>


