This project is read-only.

Clearing Map Layers and Adding from Layers from Sql Database, possible bug

Dec 15, 2015 at 7:11 AM
This may be a possible bug: In my plugin, after the users create a project collection (part of the plugin), I copy the layers on the current map to a new central database. I then wish to blank the map layers, and then add the tables/layer from the database. This is the code I use:

List<string> layers = new List<string>();
foreach (Layer fl in MWContext.Map.Layers) // read the layer names so I can retrieve it later
{
layers.Add(fl.Name);
}
// MWContext is a copy of the _context from the plugin initialization
MWContext.Layers.Clear(); // I have tried clearing from the layers, and the map


MWContext.Map.Redraw(Api.Enums.RedrawType.All);

string conn == @"MSSQL:server=.\sqlExpress;Database=" + EMDSBL.EMDSDataInterface.EMDSProject.ProjectName + ";Trusted_Connection=Yes";

foreach (string ly in layers)
{
// MWContext.Map.Layers.AddFromDatabase(conn, ly.Replace("dbo.", string.Empty), true); <- first tried from map, then tried from layers, and I tried it with keeping the "dbo." with the same results


MWContext.Legend.Layers.AddFromDatabase(conn, ly.Replace("dbo.", string.Empty), true); <- then tried from legend


MWContext.Map.Redraw(Api.Enums.RedrawType.All);


}


MWContext.Map.Redraw(Api.Enums.RedrawType.All);

The layers are displayed on the map. On the map, there are two Layers properties, one of which is from the map control. The one from the map control displays the information properly. The other layers property matches the count, but all the layers are null in this property. All the MWContext.Legend.Layers and MWContext.Layers all show the right count, but display a null value for each loaded layer when looking at the "results" view.

Steve
Dec 15, 2015 at 7:45 AM
Here are a couple of screenshots. I have 2 layers added to the map:
Image

Then I run my code, which writes it out to the database. Now before I clear the layers, the Layers collection in MWContext.legend (which mirrors the MWContext.Layer) shows the correct information
Image

The next screen shows the layers collection after clearing the map layers:
Image

The next screen displays what the collection looks like after I do the AddFromDatabase call for the MWContext.Legend.layers and MWContext.Layers collection:
Image

The final screenshot shows the MWContext.Map display. Notice there are two different Layer collections listed.
Image

The map display on the main page now looks like this. It looks almost like the first image, with the correct layers, but has no data layers in the legend.
Image

The screenshots match the code above. Any help would be appreciated.

Steve
Dec 15, 2015 at 10:45 AM
Hi Steve, it's a bug / inconsistency in the API. AddFromDatabase method must not be in the API (I've committed changes to exclude it; https://mapwindow.atlassian.net/browse/CORE-124). Instead layers should be added through ILayerService.AddDatabaseLayer (or other methods of the same interface). An instance of ILayerService can be retrieved with IAppContext.Container.Resolve<ILayerService>(); or by injecting it in constructor of your class if you've registered it in composition root.