1

Re: Grid map example in C#

Hi,

I've posted this example in response to a specific query about how to create a grid map image for a website using .NET.  I've written it in c#, it is not polished, but does show the priciples of how to set year filters, colours, etc.  I've included an image of what the form looks like when I use it specifying the key for Water Vole (NHMSYS0000080205).  The code beneath the image is a full dump of GridMapQuery.cs file showing all the auto-generated code too, so just pick out anything that is useful.

Jon

[img]http://www.searchnbn.net/library/forum_images/gridMapFormExample.gif[/img]

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace WindowsApplication1
{
    /// <summary>
    /// Summary description for GridMapQuery.
    /// </summary>
    public class GridMapQuery : System.Windows.Forms.Form
    {

        
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            //Run whichever form you want from here...
            Application.Run(new GridMapQuery());
//            Application.Run(new OneSiteQuery());
//            Application.Run(new Form1());
        }


        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Label labelTvk;
        private System.Windows.Forms.TextBox textBoxTvk;
        private System.Windows.Forms.Label labelVc;
        private System.Windows.Forms.TextBox textBoxVc;
        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.ColorDialog colorDialog1;
        private System.Windows.Forms.Label year1Colour;
        private System.Windows.Forms.Label year3FilterText;
        private System.Windows.Forms.Label year3Colour;
        private System.Windows.Forms.Label year2Colour;
        private System.Windows.Forms.TextBox year1Start;
        private System.Windows.Forms.TextBox year3Start;
        private System.Windows.Forms.TextBox year2End;
        private System.Windows.Forms.TextBox year2Start;
        private System.Windows.Forms.TextBox year1End;
        private System.Windows.Forms.TextBox year3End;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public GridMapQuery()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <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.button1 = new System.Windows.Forms.Button();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.labelTvk = new System.Windows.Forms.Label();
            this.textBoxTvk = new System.Windows.Forms.TextBox();
            this.labelVc = new System.Windows.Forms.Label();
            this.textBoxVc = new System.Windows.Forms.TextBox();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.year1Start = new System.Windows.Forms.TextBox();
            this.year3End = new System.Windows.Forms.TextBox();
            this.year3Start = new System.Windows.Forms.TextBox();
            this.year2End = new System.Windows.Forms.TextBox();
            this.year2Start = new System.Windows.Forms.TextBox();
            this.year1End = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.year3FilterText = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.year2Colour = new System.Windows.Forms.Label();
            this.year3Colour = new System.Windows.Forms.Label();
            this.year1Colour = new System.Windows.Forms.Label();
            this.colorDialog1 = new System.Windows.Forms.ColorDialog();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(160, 424);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(104, 40);
            this.button1.TabIndex = 0;
            this.button1.Text = "Submit";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // pictureBox1
            // 
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pictureBox1.Location = new System.Drawing.Point(424, 8);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(500, 500);
            this.pictureBox1.TabIndex = 1;
            this.pictureBox1.TabStop = false;
            // 
            // labelTvk
            // 
            this.labelTvk.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.labelTvk.Location = new System.Drawing.Point(32, 32);
            this.labelTvk.Name = "labelTvk";
            this.labelTvk.Size = new System.Drawing.Size(88, 23);
            this.labelTvk.TabIndex = 2;
            this.labelTvk.Text = "Taxon key:";
            this.labelTvk.Click += new System.EventHandler(this.labelTvk_Click);
            // 
            // textBoxTvk
            // 
            this.textBoxTvk.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.textBoxTvk.Location = new System.Drawing.Point(128, 32);
            this.textBoxTvk.Name = "textBoxTvk";
            this.textBoxTvk.Size = new System.Drawing.Size(128, 20);
            this.textBoxTvk.TabIndex = 3;
            this.textBoxTvk.Text = "NHMSYS0000080205";
            // 
            // labelVc
            // 
            this.labelVc.Location = new System.Drawing.Point(16, 312);
            this.labelVc.Name = "labelVc";
            this.labelVc.Size = new System.Drawing.Size(168, 16);
            this.labelVc.TabIndex = 4;
            this.labelVc.Text = "Vice county number (optional):";
            // 
            // textBoxVc
            // 
            this.textBoxVc.Location = new System.Drawing.Point(184, 312);
            this.textBoxVc.Name = "textBoxVc";
            this.textBoxVc.Size = new System.Drawing.Size(40, 20);
            this.textBoxVc.TabIndex = 5;
            this.textBoxVc.Text = "";
            // 
            // richTextBox1
            // 
            this.richTextBox1.Location = new System.Drawing.Point(424, 528);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(496, 168);
            this.richTextBox1.TabIndex = 6;
            this.richTextBox1.Text = "";
            // 
            // year1Start
            // 
            this.year1Start.Location = new System.Drawing.Point(112, 200);
            this.year1Start.Name = "year1Start";
            this.year1Start.Size = new System.Drawing.Size(64, 20);
            this.year1Start.TabIndex = 7;
            this.year1Start.Text = "1986";
            // 
            // year3End
            // 
            this.year3End.Location = new System.Drawing.Point(216, 264);
            this.year3End.Name = "year3End";
            this.year3End.Size = new System.Drawing.Size(64, 20);
            this.year3End.TabIndex = 8;
            this.year3End.Text = "1959";
            // 
            // year3Start
            // 
            this.year3Start.Location = new System.Drawing.Point(112, 264);
            this.year3Start.Name = "year3Start";
            this.year3Start.Size = new System.Drawing.Size(64, 20);
            this.year3Start.TabIndex = 9;
            this.year3Start.Text = "1";
            // 
            // year2End
            // 
            this.year2End.Location = new System.Drawing.Point(216, 232);
            this.year2End.Name = "year2End";
            this.year2End.Size = new System.Drawing.Size(64, 20);
            this.year2End.TabIndex = 10;
            this.year2End.Text = "1985";
            // 
            // year2Start
            // 
            this.year2Start.Location = new System.Drawing.Point(112, 232);
            this.year2Start.Name = "year2Start";
            this.year2Start.Size = new System.Drawing.Size(64, 20);
            this.year2Start.TabIndex = 11;
            this.year2Start.Text = "1960";
            // 
            // year1End
            // 
            this.year1End.Location = new System.Drawing.Point(216, 200);
            this.year1End.Name = "year1End";
            this.year1End.Size = new System.Drawing.Size(64, 20);
            this.year1End.TabIndex = 12;
            this.year1End.Text = "2007";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(16, 200);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(72, 23);
            this.label1.TabIndex = 13;
            this.label1.Text = "Year filter 1:";
            this.label1.Click += new System.EventHandler(this.label1_Click);
            // 
            // year3FilterText
            // 
            this.year3FilterText.Location = new System.Drawing.Point(16, 264);
            this.year3FilterText.Name = "year3FilterText";
            this.year3FilterText.Size = new System.Drawing.Size(72, 23);
            this.year3FilterText.TabIndex = 14;
            this.year3FilterText.Text = "Year filter 3:";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(16, 232);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(72, 23);
            this.label3.TabIndex = 15;
            this.label3.Text = "Year filter 2:";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(216, 176);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(72, 16);
            this.label4.TabIndex = 16;
            this.label4.Text = "End year";
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(112, 176);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(72, 16);
            this.label5.TabIndex = 17;
            this.label5.Text = "Start year";
            this.label5.Click += new System.EventHandler(this.label5_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.textBoxTvk);
            this.groupBox1.Controls.Add(this.labelTvk);
            this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.groupBox1.Location = new System.Drawing.Point(8, 8);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(376, 96);
            this.groupBox1.TabIndex = 18;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Mandatory thing";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.year2Colour);
            this.groupBox2.Controls.Add(this.year3Colour);
            this.groupBox2.Controls.Add(this.year1Colour);
            this.groupBox2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.groupBox2.Location = new System.Drawing.Point(8, 152);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(392, 224);
            this.groupBox2.TabIndex = 19;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Optional things";
            // 
            // year2Colour
            // 
            this.year2Colour.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0)));
            this.year2Colour.Location = new System.Drawing.Point(304, 80);
            this.year2Colour.Name = "year2Colour";
            this.year2Colour.Size = new System.Drawing.Size(24, 23);
            this.year2Colour.TabIndex = 23;
            this.year2Colour.Click += new System.EventHandler(this.year2Colour_Click);
            // 
            // year3Colour
            // 
            this.year3Colour.BackColor = System.Drawing.Color.Red;
            this.year3Colour.Location = new System.Drawing.Point(304, 112);
            this.year3Colour.Name = "year3Colour";
            this.year3Colour.Size = new System.Drawing.Size(24, 23);
            this.year3Colour.TabIndex = 22;
            this.year3Colour.Click += new System.EventHandler(this.year3Colour_Click);
            // 
            // year1Colour
            // 
            this.year1Colour.BackColor = System.Drawing.Color.Yellow;
            this.year1Colour.Location = new System.Drawing.Point(304, 48);
            this.year1Colour.Name = "year1Colour";
            this.year1Colour.Size = new System.Drawing.Size(24, 23);
            this.year1Colour.TabIndex = 20;
            this.year1Colour.Click += new System.EventHandler(this.year1Colour_Click);
            // 
            // GridMapQuery
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(960, 701);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.year3FilterText);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.year1End);
            this.Controls.Add(this.year2Start);
            this.Controls.Add(this.year2End);
            this.Controls.Add(this.year3Start);
            this.Controls.Add(this.year3End);
            this.Controls.Add(this.year1Start);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.textBoxVc);
            this.Controls.Add(this.labelVc);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.groupBox2);
            this.Name = "GridMapQuery";
            this.Text = "GridMapQuery";
            this.groupBox1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        private void button1_Click(object sender, System.EventArgs e)
        {
            try
            {
                richTextBox1.Text = "";



                //Add OS background
                //gms.Background = Background.OSMap;
                //gms.BackgroundSpecified = true;

                //Set up the request
                GridMapRequest gmr = new GridMapRequest();

                //Set the species key
                gmr.TaxonVersionKey = textBoxTvk.Text;

                //Sort out the dates and colours
                String fillColour1 = "#"+IntToHex(year1Colour.BackColor.R)+IntToHex(year1Colour.BackColor.G)+IntToHex(year1Colour.BackColor.B);;
                String fillColour2 = "#"+IntToHex(year2Colour.BackColor.R)+IntToHex(year2Colour.BackColor.G)+IntToHex(year2Colour.BackColor.B);;
                String fillColour3 = "#"+IntToHex(year3Colour.BackColor.R)+IntToHex(year3Colour.BackColor.G)+IntToHex(year3Colour.BackColor.B);;
                Band year1 = new Band();
                Band year2 = new Band();
                Band year3 = new Band();
                year1.from = Int32.Parse(year1Start.Text);
                year2.from = Int32.Parse(year2Start.Text);
                year3.from = Int32.Parse(year3Start.Text);
                year1.to = Int32.Parse(year1End.Text);
                year2.to = Int32.Parse(year2End.Text);
                year3.to = Int32.Parse(year3End.Text);
                year1.fill = fillColour1;
                year2.fill = fillColour2;
                year3.fill = fillColour3;
                Band[] yearBands = new Band[3];
                yearBands[0] = year1;
                yearBands[1] = year2;
                yearBands[2] = year3;
                gmr.Classification = yearBands;

                //This can be used to control the resolution of data mapped
                gmr.Resolution = Resolution._10km;
                gmr.ResolutionSpecified = true;

                //Set up additional grid map settings
                GridMapSettings gms = new GridMapSettings();
                
                //Set the vice county if there is one
                if(textBoxVc.Text!="")
                {
                    gms.ViceCountySpecified = true;
                    gms.ViceCounty = Int32.Parse(textBoxVc.Text);
                }

                //Set the image size
                gms.Height = 500;
                gms.HeightSpecified = true;
                gms.Width = 500;
                gms.WidthSpecified = true;

                //Apply additional grid map settings to the request
                gmr.GridMapSettings = gms;

                //Get the response
                GatewayWebService gws = new GatewayWebService();
                GridMap gm = gws.GetGridMap(gmr);
                
                //Add the map image
                insertMap(gm.Map.Url.ToString());

                //Add the bounding box to the output - this is for georeferencing
                //BoundingBox bb = gm.Map.BoundingBox;
                //richTextBox1.Text += "Bounding box: " + bb.minx + ", " + bb.miny + ", " + bb.maxx + ", " + bb.maxy;

                //Add the dataset titles to the output
                DatasetSummary[] ds = gm.DatasetSummaryList.DatasetSummary;
                richTextBox1.Text += "\nDatasets contributing to map\n________________________";
                for(int i=0; i<ds.Length; i++){
                    richTextBox1.Text += "\n" + ds[i].ProviderMetadata.DatasetTitle;
                }
            }
            catch(Exception ex)
            {
                richTextBox1.Text += "\n\n" + ex.Message;
            }
        }

        private void insertMap(String url)
        {
            HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
            req.Method = "GET";
            WebResponse resp = req.GetResponse();
            Stream st = resp.GetResponseStream();
            Bitmap bmp = new Bitmap(Image.FromStream(st));
            pictureBox1.Image = bmp;
        }

        private void labelTvk_Click(object sender, System.EventArgs e)
        {
        
        }

        private void label1_Click(object sender, System.EventArgs e)
        {
        
        }

        private void label5_Click(object sender, System.EventArgs e)
        {
        
        }

        private void year1Colour_Click(object sender, System.EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                year1Colour.BackColor =  colorDialog1.Color;
            }
        }

        private void year3Colour_Click(object sender, System.EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                year3Colour.BackColor =  colorDialog1.Color;
            }
        }

        private void year2Colour_Click(object sender, System.EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                year2Colour.BackColor =  colorDialog1.Color;
            }
        }

        public string IntToHex(int number)
        {
            string result = String.Format("{0:x}", number);
            if(result.Length ==1)
                result = "0" + String.Format("{0:x}", number);
            return result;
        }





    }
}