MapWinGIS:SampleCode-C Sharp Net:LegendSymbolizer

This is a back-up of the WIKI.
Not all links might work
We're working on a new wiki.

Main Page | Recent changes | View source | Page history | Log in / create account |

Printable version | Disclaimers | Privacy policy

Assign a shapefile color scheme that appears both in the map and the legend.

The code in this area is divided into two sections. The first is a class that actually has the code for creating a color scheme and applying it, while the second part is simply using the first class. The colors are stepped across a gradient determined by the specified field index.

The ApplyColorScheme Class

     public class ApplyLegendColorscheme
     {
         public ApplyLegendColorscheme()
         {
 
         }
 
         /// <summary>
         /// Requires a reference to MapWinInterfaces.dll & MapWinGIS.ocx
         /// </summary>
         /// <param name="legend">A Legend Control</param>
         /// <param name="iLayer">The integer index of the layer to apply the scheme to</param>
         /// <param name="iFieldIndex">The actual field index to use for catagorizing the layers</param>
         public void ApplyScheme(LegendControl.Legend legend, int iLayer, int iFieldIndex, double minValue, double maxValue, int numSteps)
         {
             MapWinGIS.ShapefileColorScheme shpColorScheme = new MapWinGIS.ShapefileColorScheme();
             shpColorScheme.LayerHandle = iLayer;
             shpColorScheme.FieldIndex = iFieldIndex;
             double stepSize = (maxValue - minValue) / (double)numSteps;
             double hue = Convert.ToDouble(System.Drawing.Color.Blue.GetHue());
             double sat = .5;
             double brightStep = 1 / (double)numSteps;
             for (int i = 0; i < numSteps; i++)
             {
                 MapWinGIS.ShapefileColorBreak clsBreak = new MapWinGIS.ShapefileColorBreak();
                 clsBreak.StartValue = i * (stepSize) + minValue;
                 clsBreak.EndValue = (i + 1) * stepSize + minValue;
                 Color colBreak = ColorFromHSL(hue, sat, brightStep * i);
                 uint colUInt = Convert.ToUInt32(colBreak.R + colBreak.G * 256 + colBreak.B * (256 * 256));
                 clsBreak.StartColor = colUInt;
                 clsBreak.EndColor = colUInt;  // For a discrete ramp, start and end colors are the same, but there are several steps
                 shpColorScheme.Add(clsBreak);
             }
             
             if (legend.Map.ApplyLegendColors(shpColorScheme) == false)
             {
                 MessageBox.Show(legend.Map.get_ErrorMsg(legend.Map.LastErrorCode));
             }
             legend.Layers.ItemByHandle(iLayer).Refresh();
 
         }
 
         ///  <summary>
         /// Converts a colour from HSL to RGB
         /// </summary>
         /// <remarks>Adapted from the algoritm in Foley and Van-Dam</remarks>
         /// <param name="hue">A double representing degrees ranging from 0 to 360 and is equal to the GetHue() on a Color structure.</param>
         /// <param name="brightness">A double value ranging from 0 to 1, where 0 is black and 1 is white.</param>
         /// <param name="saturation">A double value ranging from 0 to 1, where 0 is gray and 1 is fully saturated with color.</param>
         /// <returns>A Color structure with the equivalent hue saturation and brightness</returns>
         public static Color ColorFromHSL(double hue, double saturation, double brightness)
         {
             double normalizedHue = hue / 360;
             double red = 0, green = 0, blue = 0;
             double temp1, temp2;
 
             if (brightness == 0)
             {
                 red = green = blue = 0;
             }
             else
             {
                 if (saturation == 0)
                 {
                     red = green = blue = brightness;
                 }
                 else
                 {
                     if (brightness <= 0.5)
                     {
                         temp2 = brightness * (1.0 + saturation);
                     }
                     else
                     {
                         temp2 = brightness + saturation - (brightness * saturation);
                     }
 
                     temp1 = 2.0 * brightness - temp2;
 
                     double[] temp3 = { normalizedHue + 1.0 / 3.0, normalizedHue, normalizedHue - 1.0 / 3.0 };
                     double[] color = { 0, 0, 0 };
                     for (int i = 0; i < 3; i++)
                     {
                         if (temp3[i] < 0) temp3[i] += 1.0;
 
                         if (temp3[i] > 1) temp3[i] -= 1.0;
 
                         if (6.0 * temp3[i] < 1.0)
                         {
                             color[i] = temp1 + (temp2 - temp1) * temp3[i] * 6.0;
                         }
                         else if (2.0 * temp3[i] < 1.0)
                         {
                             color[i] = temp2;
                         }
                         else if (3.0 * temp3[i] < 2.0)
                         {
                             color[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - temp3[i]) * 6.0);
                         }
                         else
                         {
                             color[i] = temp1;
                         }
                     }
 
                     red = color[0];
                     green = color[1];
                     blue = color[2];
 
                 }
 
             }
             if (red > 1) red = 1;
             if (red < 0) red = 0;
             if (green > 1) green = 1;
             if (green < 0) green = 0;
             if (blue > 1) blue = 1;
             if (blue < 0) blue = 0;
             return Color.FromArgb((int)(255 * red), (int)(255 * green), (int)(255 * blue));
         }
     }

Using the ApplyColorScheme Class

     // Requires a reference to MapWinInterfaces.dll and MapWinGIS.ocx
     public partial class Form1 : Form
     {
         private ApplyLegendColorscheme colorscheme;
 
         public Form1()
         {
             InitializeComponent();
             colorscheme = new ApplyLegendColorscheme();
             legend1.Map = axMap1.GetOcx() as MapWinGIS.Map;
         }
 
 
         private void openToolStripMenuItem_Click(object sender, EventArgs e)
         {
             // Create a shapefile to compare geographic coordinates against
             OpenFileDialog ofd = new OpenFileDialog();
             ofd.Filter = "Shapefiles |*.shp";
             if (ofd.ShowDialog(this) != DialogResult.OK) return;
             MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile();
             sf.Open(ofd.FileName, null);
             int iLayer = legend1.Layers.Add(sf, true);
             legend1.Map.set_LayerName(iLayer, System.IO.Path.GetFileName(sf.Filename));
             axMap1.set_ShapeLayerPointSize(iLayer, 5);
             double min = double.MaxValue;
             double max = double.MinValue;
             int iField = 7;
             for (int shp = 0; shp < sf.NumShapes; shp++)
             {
                 object v = sf.get_CellValue(iField, shp);
                 double val = Convert.ToDouble(v);
                 if (val < min) min = val;
                 if (val > max) max = val;
             }
             colorscheme.ApplyScheme(legend1, iLayer, iField, min, max, 10);
         }

Retrieved from "http://mapwindow.org/wiki/index.php/MapWinGIS:SampleCode-C_Sharp_Net:LegendSymbolizer"

This page has been accessed 2,622 times. This page was last modified on 16 April 2010, at 22:08.