Ordering of layers.

May 9, 2012 at 5:20 AM

hello...

I want to order all the layers in MAP alphabetically.

I have seen map.layers.orderBY.. but not getting hoe to apply it.

plz give some code demo for same.

thank you.

Developer
May 9, 2012 at 7:11 PM

I've created a tutorial for you. You can follow the article or just paste the code below into a new class.

namespace DotSpatial.Plugins
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
 
    using DotSpatial.Controls;
    using DotSpatial.Controls.Header;
 
    public class SortLayersPlugin : Extension
    {
        public override void Activate()
        {
            App.HeaderControl.Add(new SimpleActionItem("Sort Layers", ButtonClick));
            base.Activate();
        }
 
        public override void Deactivate()
        {
            App.HeaderControl.RemoveAll();
            base.Deactivate();
        }
 
        public void ButtonClick(object sender, EventArgs e)
        {
            // Get a list of layers sorted alphabetically by LegendText.
            var newLayers = App.Map.Layers.OrderByDescending(l => l.LegendText).ToList();
 
            // Suspending events speeds our changes up and prevents redrawing multiple times.
            // The events will be called only once, when we call ResumeEvents().
            App.Map.Layers.SuspendEvents();
 
            // By default, it appears some part of the layer is disposed when it is removed from the collection.
            // If we were to use App.Map.Layers.Clear(), we would stlil need to LockDispose on each layer.
            while (App.Map.Layers.Any())
            {
                var layer = App.Map.Layers[0];
                layer.LockDispose();
                App.Map.Layers.RemoveAt(0);
            }
 
            // As we add each layer back in, in the correct order, we UnlockDispose so that the layer can be disposed
            // at the appropriate time.
            foreach (IMapLayer newLayer in newLayers)
            {
                App.Map.Layers.Add(newLayer);
                newLayer.UnlockDispose();
            }
            App.Map.Layers.ResumeEvents();
        }
    }
}
May 10, 2012 at 9:10 AM

thanks a lot....

It is working properly.

Thank u.

May 13, 2012 at 2:34 AM

It very useful to see the example

<embed id="lingoes_plugin_object" type="application/lingoes-npruntime-capture-word-plugin" width="0" height="0" hidden="true"></embed>