Adding a layer/Removing a layer - C#

Ian Erickson ierickson at ANALYGIS.COM
Tue Apr 4 18:56:29 EDT 2006


Can anyone point me to an example of using MapScript C# that:

a) Loads a map
b) Adds 2 or 3 *dynamic* layers, classes, and styles
c) Disposes of the map and associated layers properly.

I've got an example working here - but when more than 1 layerObj is 
created and added to the map, I get a bunch of 
mapscriptPINVOKE.delete_classObj or .delete_layerObj errors.  Obviously, 
things are happening in the SWIG interface - like disposing of objects - 
but it doesn't appear to be working properly.

I've attached some sample C# code - perhaps someone can immediately 
identify my error...

//-------------------------------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MapScriptMap
{
    /// <summary>
    /// Summary description for MapServerForm.
    /// </summary>
    public class MapServerForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.PictureBox MapImage;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        private System.Windows.Forms.Button buttonCreatePointLayer;

        private mapObj msMap = null;
        private System.Windows.Forms.Button buttonCreateLineLayer;
        private ArrayList layerIndex = null;

        public MapServerForm()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            InitializeMap();
        }

        private void InitializeMap()
        {
            try
            {
                this.msMap = new mapObj(@"C:\data\base.map");
                this.layerIndex = new ArrayList(0);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                this.msMap = null;
            }
        }

        private void CreatePointLayer(string layerName)
        {
            this.CreatePointLayer(layerName, 7, 
System.Drawing.Color.FromArgb(0, 192, 0));
        }
       
        private void CreatePointLayer(string layerName, int symbolSize, 
System.Drawing.Color color)
        {
            if (this.layerExists(layerName))
                return;

            layerObj pointLayer = new layerObj(this.msMap);
            pointLayer.name = layerName;
            pointLayer.type = MS_LAYER_TYPE.MS_LAYER_POINT;
            pointLayer.setProjection("init=epsg:4326");
            pointLayer.status = 1;

            classObj pointClass = new classObj(pointLayer);
            pointClass.label.type = MS_FONT_TYPE.MS_TRUETYPE;
            pointClass.label.font = "arial-bold";
            pointClass.label.size = 7;
            pointClass.label.color.setRGB(0,0,0);
            pointClass.label.position = 2;

            styleObj pointStyle = new styleObj(pointClass);
            pointStyle.symbol = 7;
            pointStyle.antialias = 1;
            pointStyle.size = symbolSize;
            pointStyle.color.setRGB((int)color.R,(int)color.G,(int)color.B);
            pointStyle.outlinecolor.setRGB((int)(color.R / 
2),(int)(color.G / 2),(int)(color.B / 2));

            Console.WriteLine("Point layer: " + layerName + " added to 
the map...");

            this.layerIndex.Add(layerName);
        }

        private void CreateLineLayer(string layerName)
        {
            if (this.layerExists(layerName))
                return;

            layerObj lineLayer = new layerObj(this.msMap);
            lineLayer.name = layerName;
            lineLayer.type = MS_LAYER_TYPE.MS_LAYER_LINE;
            lineLayer.setProjection("init=epsg:4326");
            lineLayer.transparency = 30;
            lineLayer.status = 1;

            classObj lineClass = new classObj(lineLayer);
            lineClass.label.type = MS_FONT_TYPE.MS_TRUETYPE;
            lineClass.label.font = "arial-bold";
            lineClass.label.size = 7;
            lineClass.label.color.setRGB(0,0,0);
            lineClass.label.position = 2;

            styleObj lineStyle = new styleObj(lineClass);
            lineStyle.symbol = 7;
            lineStyle.size = 8;
            lineStyle.color.setRGB(192,0,192);

            Console.WriteLine("Line layer: " + layerName + " added to 
the map...");

            this.layerIndex.Add(layerName);
        }

        private bool layerExists(string layerName)
        {
            if (this.msMap == null)
                return false;

            for (int i = 0; i < this.msMap.numlayers; i++)
            {
                if (this.msMap.getLayer(i).name.ToUpper() == 
layerName.ToUpper())
                    return true;
            }

            return false;
        }

        private void RemoveLayers()
        {
            foreach(string layerName in this.layerIndex)
            {
                layerObj l = this.msMap.getLayerByName(layerName);
//                while (l.numclasses > 0)
//                {
//                    classObj c = l.getClass(0);
//                    l.removeClass(0);
//                    c.Dispose();
//                }
                this.msMap.removeLayer(l.index);
                l.Dispose();
            }

            this.layerIndex.Clear();
        }

        private void RenderMap()
        {
            if (this.msMap == null)
                return;

            msMap.setSize(this.MapImage.Width, this.MapImage.Height);

            imageObj msImage;
            try
            {
                msImage = msMap.draw();

                System.IO.MemoryStream ms = new 
System.IO.MemoryStream(msImage.getBytes());
                Bitmap img = new Bitmap(ms);

                this.MapImage.Image = img;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.MapImage = new System.Windows.Forms.PictureBox();
            this.buttonCreatePointLayer = new System.Windows.Forms.Button();
            this.buttonCreateLineLayer = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // MapImage
            //
            this.MapImage.BorderStyle = 
System.Windows.Forms.BorderStyle.Fixed3D;
            this.MapImage.Location = new System.Drawing.Point(8, 8);
            this.MapImage.Name = "MapImage";
            this.MapImage.Size = new System.Drawing.Size(272, 248);
            this.MapImage.TabIndex = 0;
            this.MapImage.TabStop = false;
            //
            // buttonCreatePointLayer
            //
            this.buttonCreatePointLayer.Location = new 
System.Drawing.Point(288, 8);
            this.buttonCreatePointLayer.Name = "buttonCreatePointLayer";
            this.buttonCreatePointLayer.Size = new 
System.Drawing.Size(176, 23);
            this.buttonCreatePointLayer.TabIndex = 1;
            this.buttonCreatePointLayer.Text = "Create Point Layer";
            this.buttonCreatePointLayer.Click += new 
System.EventHandler(this.buttonCreatePointLayer_Click);
            //
            // buttonCreateLineLayer
            //
            this.buttonCreateLineLayer.Location = new 
System.Drawing.Point(288, 40);
            this.buttonCreateLineLayer.Name = "buttonCreateLineLayer";
            this.buttonCreateLineLayer.Size = new 
System.Drawing.Size(176, 23);
            this.buttonCreateLineLayer.TabIndex = 2;
            this.buttonCreateLineLayer.Text = "Create Line Layer";
            this.buttonCreateLineLayer.Click += new 
System.EventHandler(this.buttonCreateLineLayer_Click);
            //
            // MapServerForm
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(472, 302);
            this.Controls.Add(this.buttonCreateLineLayer);
            this.Controls.Add(this.buttonCreatePointLayer);
            this.Controls.Add(this.MapImage);
            this.Name = "MapServerForm";
            this.Text = "MapServer Map";
            this.Closing += new 
System.ComponentModel.CancelEventHandler(this.MapServerForm_Closing);
            this.Load += new System.EventHandler(this.MapServerForm_Load);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new MapServerForm());
        }

        private void MapServerForm_Load(object sender, System.EventArgs e)
        {
            this.RenderMap();
        }

        private void buttonCreatePointLayer_Click(object sender, 
System.EventArgs e)
        {
            this.CreatePointLayer("BLAH", 10, 
System.Drawing.Color.FromArgb(192, 0, 0));

            this.RenderMap();
        }

        private void buttonCreateLineLayer_Click(object sender, 
System.EventArgs e)
        {
            this.CreateLineLayer("LINE");

            this.RenderMap();
        }

        private void MapServerForm_Closing(object sender, 
System.ComponentModel.CancelEventArgs e)
        {
//            this.RemoveLayers();
//            if (this.msMap != null)
//                this.msMap.Dispose();
//
//            this.msMap = null;
        }
    }
}


-- 
Ian Erickson
AnalyGIS, LLC
Gold Canyon, AZ  85218
http:// www.analygis.com

tel: 480.677.6260
mob: 480.221.7173
fax: 480.677.6261

See AnalyGIS at work:  
http://65.39.85.13/google/ 
http://65.39.85.13/virtualearth/



More information about the mapserver-users mailing list