CustomTheme rendering

The CustomTheme class uses a delegate method to specify the style. The delegate takes a FeatureDataRow as parameter and returns an IStyle (VectorStyle for vectorlayers and LabelStyle for LabelLayers). If the returned style is null, the layer style is used instead.

The CustomTheme is for instance used for the SimpleChart extension to render different graphs on each cities.

Below is an example of a style delegate for rendering different styling based on various attributes:
private SharpMap.Styles.VectorStyle GetCountryStyle(SharpMap.Data.FeatureDataRow row)
{
	SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();
	switch (row["NAME"].ToString().ToLower())
	{
		case "denmark": //If country name is Danmark, fill it with green
			style.Fill = Brushes.Green;
			return style;
		case "united states": //If country name is USA, fill it with Blue and add a red outline
			style.Fill = Brushes.Blue;
			style.Outline = Pens.Red;
			return style;
		case "china": //If country name is China, fill it with red
			style.Fill = Brushes.Red;
			return style;
		default:
			break;
	}
	//If country name starts with S make it yellow
	if (row["NAME"].ToString().StartsWith("S"))
	{
		style.Fill = Brushes.Yellow;
		return style;
	}
	// If geometry is a (multi)polygon and the area of the polygon is less than 30, make it cyan
	else if (row.Geometry is GeoAPI.Geometries.IPolygonal && row.Geometry.Area < 30)
	{
		style.Fill = Brushes.Cyan;
		return style;
	}
	else //None of the above -> Use the default style
		return null;
}

To apply this style to a layer, set the Theme property on your VectorLayer:
SharpMap.Rendering.Thematics.CustomTheme myTheme = new SharpMap.Rendering.Thematics.CustomTheme(GetCountryStyle);
myVectorLayer.Theme = iTheme;

Resulting image:
customtheme.png

Last edited Aug 31, 2012 at 12:25 PM by FObermaier, version 3