Select to view content in your preferred language

C# removing layers & empty grouplayers

1351
0
06-22-2018 06:53 AM
MikeRatcliffe
Frequent Contributor

Since I didn't see much out there (C#, SDK 2.1) on manipulating grouplayer, specifically removing them from the TOC if empty; here is an example.  Function "removes unchecked layers in the TOC" through a series of steps.

private async Task X_TOC_lyrs()
{
await ActMap();//mapview.active.map checker

//Removes group-level layers that are unchecked, at once

IReadOnlyCollection<Layer> dlayers = MapView.Active.Map.Layers.Where(l => l.IsVisible == false).ToList();
await QueuedTask.Run(() => MapView.Active.Map.RemoveLayers(dlayers));

//Removes subgroup-level layers that are unchecked, one-by-one

IEnumerable<Layer> dlayers2 = MapView.Active.Map.GetLayersAsFlattenedList().Where(l => l.IsVisible == false).ToList();
foreach (var dlyr in dlayers2)
{
await QueuedTask.Run(() => MapView.Active.Map.RemoveLayer(dlyr));
}

//Remove empty groups

IReadOnlyList<GroupLayer> glayers = MapView.Active.Map.Layers.OfType<GroupLayer>().ToList();
foreach (var lyr in glayers)
{
if(lyr.Layers.Count == 0)
{
await QueuedTask.Run(() => MapView.Active.Map.RemoveLayer(lyr));
}
}

}

0 Kudos
0 Replies