MapBox with VariableLayerCollection render

Topics: WinForms Controls
Developer
Jan 21, 2011 at 2:54 AM

Hi, I'v been working with the latest version of SharpMap, and I've noticed that there is a MapImage Control that can render VariableLayerCollection.

 

My experience tells-me that the MapBox Control is faster then MapImage, but the current MapBox Control is not aware of the Map.VariableLayers.

 

I've changed the MapBox Control and I want to share it.

I'm not an expert with codeplex, but I think that I have a patch file. How can I submit it?

 

Thank you

Cofee

Developer
Jan 21, 2011 at 2:54 AM

--- MapBox_old.cs	sex Jan 21 00:44:56 2011
+++ MapBox.cs	sex Jan 21 02:47:55 2011
@@ -23,6 +23,7 @@
 using System.ComponentModel;
 using System.Drawing;
 using System.Windows.Forms;
+using SharpMap.Layers;
 
 namespace SharpMap.Forms
 {
@@ -199,6 +200,8 @@
         private Pen m_RectanglePen = new Pen(Color.FromArgb(244, 244, 244), 1);
         private float m_Scaling = 0;
         private Image m_Image;
+        private Image m_ImageStatic;
+        private Image m_ImageVariable;
         private PreviewModes m_PreviewMode;
 
         [Description("The color of selecting rectangle.")]
@@ -276,9 +279,12 @@
             set
             {
                 m_Map = value;
-                
+
                 if (m_Map != null)
+                {
+                    m_Map.VariableLayers.VariableLayerCollectionRequery += this.VariableLayersRequery;
                     Refresh();
+                }
             }
         }
 
@@ -321,6 +327,8 @@
             SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
             DoubleBuffered = true;
             m_Map = new Map(ClientSize);
+            m_Map.VariableLayers.VariableLayerCollectionRequery += this.VariableLayersRequery;
+
             m_ActiveTool = Tools.None;
             LostFocus += new EventHandler(MapBox_LostFocus);
         }
@@ -334,6 +342,86 @@
             }
         }
 
+        /// 
+        /// Handles need to requery of variable layers
+        /// 
+        /// <param name="sender" />
+        /// <param name="e" />
+        private void VariableLayersRequery(object sender, EventArgs e)
+        {
+            lock (m_Map)
+            {
+                if (m_Dragging) return;
+                m_ImageVariable  = GetMap(m_Map.VariableLayers, LayerCollectionType.Variable);
+            }
+            UpdateImage(false);
+            this.Invalidate();
+        }
+
+        private Image GetMap(LayerCollection layers, LayerCollectionType layerCollectionType)
+        {
+            if ((layers == null || layers.Count == 0 || Width == 0 || Height == 0))
+                return null;
+
+            Bitmap retval = new Bitmap(Width, Height);
+            Graphics g = Graphics.FromImage(retval);
+            m_Map.RenderMap(g, layerCollectionType);
+            g.Dispose();
+
+            if (layerCollectionType == LayerCollectionType.Variable)
+                retval.MakeTransparent(m_Map.BackColor);
+
+            return retval;
+
+        }
+
+
+
+        private void UpdateImage(bool forceRefresh)
+        {
+            if ((!(m_ImageStatic == null && m_ImageVariable == null) || forceRefresh) && 
+                (Width != 0 && Height != 0))
+            {
+                Bitmap bmp = new Bitmap(Width, Height);
+                Graphics g = Graphics.FromImage(bmp);
+                if (forceRefresh)
+                {
+                    lock (m_Map)
+                    {
+                        m_ImageVariable = GetMap(m_Map.VariableLayers, LayerCollectionType.Variable);
+                        m_ImageStatic = GetMap(m_Map.Layers, LayerCollectionType.Static);
+                    }
+
+                }
+
+                if (m_ImageStatic != null)
+                {
+                    try
+                    {
+                        g.DrawImageUnscaled(m_ImageStatic, 0, 0);
+                    }
+                    catch
+                    {
+                    }
+                }
+                if (m_ImageVariable != null)
+                    try
+                    {
+                        g.DrawImageUnscaled(m_ImageVariable, 0, 0);
+
+                    }
+                    catch 
+                    {
+                        
+                        
+                    } g.Dispose();
+
+                m_Image = bmp;
+            }
+        }
+
+
+
         private void SetCursor()
         {
             if (m_ActiveTool == Tools.None)
@@ -360,7 +448,7 @@
                 {
                     Cursor c = Cursor;
                     Cursor = Cursors.WaitCursor;
-                    m_Image = m_Map.GetMap();
+                    UpdateImage(true);
                     Cursor = c;
                 }
                 

Coordinator
Jan 21, 2011 at 7:31 AM

Hello cofee,

thanks for sharing this. If you have enhancements for the sharpmap library, you can post patches by clicking on the "Source Code" tab and then on the link Upload Patch.

A short notice in the forum could help promote it :).

Hth FObermaier

Developer
Jan 21, 2011 at 10:41 AM

Hi FObermaier,

 

Do I need to post this patch or you will do that ?

 

cofee