This project has moved. For the latest updates, please go here.

CustomTheme Bug

Topics: General Topics
Jun 9, 2007 at 7:33 AM
Why is this happening?

I create a map with custom theme with the following code:
myLayer = new SharpMap.Layers.VectorLayer("LayerName");
myLayer.DataSource = new SharpMap.Data.Providers.ShapeFile(strShapePath);
SharpMap.Rendering.Thematics.CustomTheme myTheme = new SharpMap.Rendering.Thematics.CustomTheme(GetThemeStyle);
SharpMap.Styles.VectorStyle defaultstyle = new SharpMap.Styles.VectorStyle();
myTheme.DefaultStyle = defaultstyle;
(mapBox1.Map.Layers[0] as SharpMap.Layers.VectorLayer).Theme = myTheme;

I added random color in the GetThemeStyle function here:
private SharpMap.Styles.VectorStyle GetThemeStyle(SharpMap.Data.FeatureDataRow row)
    string strThemeField = row["STATE"].ToString();
    SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();
    if (strThemeField != null)
        Color cThemeColor = Color.FromArgb(150, rd.Next(255), rd.Next(255), rd.Next(255));
        style.Fill = new SolidBrush(cThemeColor);
    style.EnableOutline = false;
    return style;

To my surprise, when I added a button for refreshing like:
private void btnRefresh_Click(object sender, EventArgs e)
it is calling GetThemeStyle and refreshing the map with random colors everytime I press Refresh.

Why does it need to call GetThemeStyle for every mapBox1.Refresh();?
It means it is calling GetThemeStyle everytime I pan or zoom.
This will actually throw exception at times when multiple layers are added. That's how I got to know about this.

I feel this is a bug, anyone please correct me if I am wrong.
Jun 9, 2007 at 9:01 AM
Yeah I think it is necessary to have that. Otherwise it does not know what color to render when the map is refreshed.

But the problem comes when multiple maps are added.

This thread: gives the solution for my problem.
Jun 13, 2007 at 4:06 AM
This is not a bug, but by design. The styling are not stored on the geometries, but used during the rendering of the map.
In order for this to work, you will have to create a dictionary of colors linked to each feature, and retrive them in the GetThemeStyle delegate, instead of generating them on each render. Or better yet, create a new column in your shapefile with the color values once and for all, and use the attribute to set the color. This is how it was meant to be used.
Jun 13, 2007 at 6:40 AM
I understood that once I started playing with the GetThemeStyle function. That's the reason I posted immediately on this thread, saying, it is necessary.

But, it does makes sense to have another parameter, as proposed in the ThreadId=909. Right?