Slow Rendering Shape Files

Topics: Data Access, SharpMap Project, SharpMap v0.9 / v1.x, WinForms Controls
Mar 2, 2015 at 4:57 PM
Edited Mar 2, 2015 at 4:59 PM
I am having issues with certain larger shape files taking in upwards of 5-7 seconds to load into the map (become visible). An example would be of a shape file containing several hundred housing structures for a small city. Example code below:
        private void ToggleLayer(CheckBox cb)
        {
            cb.ForeColor = cb.Checked ? Color.GreenYellow : Color.Coral;

            var layerName = Path.GetFileNameWithoutExtension(cb.Tag.ToString());

            if (cb.Checked)
            {
                var newLayer = new SharpMap.Layers.VectorLayer(layerName, 
                    new SharpMap.Data.Providers.ShapeFile(cb.Tag.ToString(), true));
                mbMap.Map.Layers.Add(newLayer);
                mbMap.Map.ZoomToExtents();
            }
            else
                mbMap.Map.Layers.Remove(mbMap.Map.Layers
                    .FirstOrDefault(l => l.LayerName == layerName));

            mbMap.Refresh();
            mbMap.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
        }
Is there any faster way of doing this? If not, is there a way to track the map load progress to be shown in a progress bar? Any help is appreciated.
Coordinator
Mar 2, 2015 at 8:06 PM
Edited Mar 2, 2015 at 8:07 PM
If you discard the layer every time you uncheck your checkbox, you have to pay the price or reloding the spatial index into memory.
My advice is: keep the layer in memory and bind newLayer.Style.Enabled to cb.Checked.
All you have to do then is call mbMap.Refresh().
Mar 2, 2015 at 10:05 PM
Edited Mar 2, 2015 at 10:05 PM
Well, the actual load of the file isn't what is causing the slowdown. I have all layers loaded into the map (Enabled set to false) within the form load (loads instantly), but when setting Enable to true for one of these large layers it takes quite some time to render. Are there any settings that would optimize the rendering section of the map? I've tried adjusting the SmoothingMode of each layer to "None", and also setting the previewmode on the map to "fast".

Updated Routine (rendering slowdown occurs when Enabled set true):
        private void ToggleLayer(CheckBox chk)
        {
            chk.ForeColor = chk.Checked ? Color.GreenYellow : Color.Coral;

            var layerName = Path.GetFileNameWithoutExtension(chk.Tag.ToString());
            var layer = mbMap.Map.Layers.FirstOrDefault(l => l.LayerName == layerName);
           
            layer.Enabled = chk.Checked;
            
            if (layer.Enabled)
                mbMap.Map.ZoomToExtents();

            mbMap.Refresh();
            mbMap.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
        }
Code loading layers (within loop generating the layer checkboxes):
            var layerName = Path.GetFileNameWithoutExtension(chk.Tag.ToString());
                
            var newLayer = new SharpMap.Layers.VectorLayer(layerName,
               new SharpMap.Data.Providers.ShapeFile(chk.Tag.ToString(), true))
            {
                Enabled = false,
                SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None
            };

            mbMap.Map.Layers.Add(newLayer);
Do keep in mind that there are quite a few structures/roads being rendered for the parish in question, though I've loaded them into other mapping software without this amount of slowdown in rendering. I do hope a solution is available for this rendering lag.
Coordinator
Mar 3, 2015 at 8:56 AM
Have you tried disabeling the use of memory cache:
new SharpMap.Data.Providers.ShapeFile(chk.Tag.ToString(), true, false))
Marked as answer by abystus on 3/3/2015 at 7:28 AM
Mar 3, 2015 at 3:25 PM
Edited Mar 3, 2015 at 3:28 PM
That was the problem! Everything is instantly rendering now. I now see the warning "BEWARE in case of large shape files.", and have to wonder why this option is enabled by default if not specified? It seems ideal performance would benefit from this being disabled by default, and the option would be there to enable if needed. I'm glad we were able to come to an acceptable resolution for the issue. Thanks for taking the time to look into this.