ClassTheme for everyone's use

Topics: General Topics
Sep 1, 2006 at 6:04 PM
I have created a ClassTheme that allows you to define different styles for different values in a field. You can also do this with the CustomTheme class, but this handles this more specific case whereas CustomeTheme is for more general use. You can also define new Matching clases. The code is below. Please email me if you have any questions.

Currently the one limitation is the assumption of a string-typed database field.

Enjoy!
-Abe
abe dot gillespie at gmail

snip
using System;
using System.Collections.Generic;

using SharpMap;
using SharpMap.Data;
using SharpMap.Styles;
using SharpMap.Rendering.Thematics;

public abstract class ClassMatch
{
public ClassMatch() { }

public ClassMatch(IStyle style)
{
_style = style;
}

public abstract bool IsMatch(object attribute);

private IStyle _style;
public virtual IStyle Style { get { return _style; } set { _style = value; } }
}

public class ExactMatch : ClassMatch
{
public ExactMatch(IStyle style, string matchValue) : base(style)
{
_value = matchValue;
}

public ExactMatch(string matchValue)
{
_value = matchValue;
}

private string _value;
public string MatchValue {get {return _value; }}
public override bool IsMatch(object attribute)
{
return (MatchValue == (string)attribute);
}
}

public class ClassTheme : ITheme
{
public ClassTheme(IStyle defaultStyle, string matchField)
{
_style = defaultStyle;
_field = matchField;
}

private IStyle _style;
public IStyle DefaultStyle { get { return _style; } }

private string _field;
public string MatchField {get {return _field; }}
private List<ClassMatch> _classes = new List<ClassMatch>();
protected List<ClassMatch> classes { get { return _classes; } }

public void AddClass(ClassMatch classMatch)
{
_classes.Add(classMatch);
}

#region ITheme Members
public IStyle GetStyle(FeatureDataRow attribute)
{
IStyle style = DefaultStyle;
foreach (ClassMatch match in classes)
{
if (match.IsMatch(attributeMatchField))
{
style = match.Style;
break;
}
}
return style;
}
#endregion
}

// Example usage -
VectorStyle style = createPointStyle("symbol1.png");
ClassTheme theme = new ClassTheme(style, "Status");
theme.AddClass(new ExactMatch(createPointStyle("symbol1.png", "1"));
theme.AddClass(new ExactMatch(createPointStyle("symbol2.png", "2"));
theme.AddClass(new ExactMatch(createPointStyle("symbol1.png", "3"));

layer = new VectorLayer("mylayer");
MsSql ds = new MsSql(connectionString, "mylayer", "WKB_Geometry", "Id");
layer.DataSource = ds;
layer.Theme = theme;
map.Layers.Add(layer);

public VectorStyle createPointStyle(string imagePath)
{
VectorStyle style = new VectorStyle();
style.Symbol = new Bitmap(imagePath);
style.SymbolScale = 1.0f;
return style;
}
Developer
Sep 1, 2006 at 10:25 PM
> Currently the one limitation is the
> assumption of a string-typed database field.

Suggestion: use generics and the "Equals" method instead of "==" operator.

Regards,
Ricardo Stuven.