Highlight particular shape_id on a layer from code?

Topics: General Topics, SharpMap v0.9 / v1.x, WinForms Controls
Jun 1, 2008 at 7:03 PM
I have a spatial simulation application that currently displays a jpg and two shape files, one a line layer and the other a polygon layer.  This part is working fine (thanks to advice from discussion forum participants).  What I am interested in is changing the display associated with a particular shape_id in the polygon (and line) shape files, when the simulation indicates that a new condition is reached by the particular spatial object.  The attribute table of each shape file currently contains only a shape_id, but I can add other external references, unique and known to the simulation, so that I can match to the individual shape_id. 

To clarify, the application involves storms hitting a harbor.  The harbor is divided into areas, identified as individual polygons in a single shape file.  The simulation can determine the degree to which a given storm affects each area, assigning a numerical value to that area.  I want to be able to display each polygon in the polygon file with a different color, based on the numerical value assigned by the simulation, and see this change as the simulation runs.  That is, I want to use the layer as an output animation display.

Any suggestions as to how to do this would be greatly appreciated.  I am using v9 with GDAL extensions.

Thanks in advance.

Richard Males
Cincinnati, Ohio, USA
Coordinator
Jun 2, 2008 at 10:25 AM
Hi Richard, it sounds like could be accomplished using either the GradientTheme (if you want to add the discriminator value to the attribute table) or the CustomTheme (where you would use the id to find the correct value via some custom logic) both are found in the namespace: SharpMap.Rendering.Thematics HTH jd
Jun 13, 2008 at 4:03 PM
Thank you, I have attempted to use custom theme, I am getting an error on compile.  The code works fine if I comment out the two lines below, displays my shape file just fine.  Undoubtedly something simple,
any help much appreciated, I am somewhat of a novice.

SharpMap.Rendering.Thematics.CustomTheme myTheme = new SharpMap.Rendering.Thematics.CustomTheme(GetReachStyle);
layer.Theme = myTheme;

Error is

Error    1    The best overloaded method match for 'SharpMap.Rendering.Thematics.CustomTheme.CustomTheme(SharpMap.Rendering.Thematics.CustomTheme.GetStyleMethod)' has some invalid arguments   
Error    2    Argument '1': cannot convert from 'method group' to 'SharpMap.Rendering.Thematics.CustomTheme.GetStyleMethod'   


Code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharpMap.Geometries;
using SharpMap.Layers;
using SharpMap.Forms;
using SharpMap.Data.Providers;

namespace SharpMapHighlight
{
    public partial class Form1 : Form
    {
        private SharpMap.Map myMap;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myMap = new SharpMap.Map(mapBox1.Size);
            mapBox1.Map = myMap;
            SharpMap.Layers.VectorLayer layer;
            layer = new SharpMap.Layers.VectorLayer("Breakwater", new SharpMap.Data.Providers.ShapeFile("c:\\breakwatercode\\gis\\NeahBayBreakwater.shp"));
            layer.LayerName = "Breakwater";
            layer.Style.Fill = new SolidBrush(Color.Red);
            layer.Style.Outline = new Pen(Color.Black);
            layer.Style.EnableOutline = true;
            myMap.Layers.Add(layer);
 
            SharpMap.Rendering.Thematics.CustomTheme myTheme = new SharpMap.Rendering.Thematics.CustomTheme(GetReachStyle);
            layer.Theme = myTheme;

            myMap.ZoomToExtents();
            mapBox1.Refresh();
        }

             
        private SharpMap.Styles.VectorStyle GetReachStyle(SharpMap.Data.FeatureDataRow row)
        {
            SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();
            switch (row["Repair"].ToString().ToLower())
            {
                case "0":
                    style.Fill = Brushes.Green;
                    return style;
                case "1":
                    style.Fill = Brushes.Blue;
                    style.Outline = Pens.Red;
                    return style;
                default:
                    return null;
             }
        
          
        }
    }
}
Coordinator
Jun 13, 2008 at 4:24 PM
Edited Jun 13, 2008 at 5:08 PM
Hi Richard, I was able to run your code with no changes apart from the shapefile path.. the only issue I had was because 'Repair' is not a column in the shapefile I used - but it was running into the GetReachStyle method ok.
 
Edit:
I was using Vs2008 - in vs2005 you may need to change :
  SharpMap.Rendering.Thematics.CustomTheme myTheme = new SharpMap.Rendering.Thematics.CustomTheme(GetReachStyle);

to  
SharpMap.Rendering.Thematics.CustomTheme myTheme = new SharpMap.Rendering.Thematics.CustomTheme(new CustomTheme.GetStyleMethod( GetReachStyle));


jd
Jun 15, 2008 at 7:24 PM
Thank you for your reply, although it did not work, it pointed me in the right direction.  I am running GDAL patch of version 0.9.  I found that when I revise my GetReachStyle as follows including the layer parameter in the call, it works just fine with the original call in 2005 (as does your proposed modified call, again in  VS 2005).

Dick

     private SharpMap.Styles.VectorStyle GetReachStyle(SharpMap.Data.FeatureDataRow row, SharpMap.Layers.ILayer layer)
        {
            SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();
            switch (row["Repair"].ToString().ToLower())
            {
                case "0":
                    style.Line.Width = 10;
                    style.Line.Color = Color.Red;
                    return style;
                case "1":
                    style.Line.Width = 5;
                    style.Line.Color = Color.Green;
                    return style;
                default:
                    return null;
             }
           
          
        }
Jun 19, 2008 at 7:45 PM


Hi rbmales.
I just wanna do the same thing when i write the same function ..an error appear :
" No overload for 'GetReachStyle' matches delegate 'SharpMap.Rendering.Thematics.CustomTheme.GetStyleMethod' "....This error just appear only when i add layer to its input parameters.

Actaully i don't understand from where this GetStyleMethod gets the values of its input parameters: FeatureDataRow row, SharpMap.Layes.ILayer layer?!
cos when i tried to send the parameters to this function ...it didn't work and an error appeared that a method name excepected !!
I will be thankful if u can help me :)
thnx in advance...