[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[udig-devel] Snippet : A methode used to create Style for a discrete value
|
hello,
here a little UNOFFICIAL (unsupported, to your own risk, without
garanty on anything ) snippet about Style (based on somelse code,
don't remember where (a test class maybe):
/**
* this function build a polygon style usable to color a
discrete attribute into a feature. (kind of soil,...)
* of course length(colors) is equal to length(setofdata)
* @param name : the name of the discret attribute to color
* @param colors : an array of color, One color by discrete
value, can be null (choose a ramdom color in that case)
* @param setofdata : all the real discrete values
* @param schema : the FeatureType ( use FeatureSource FS =
theLayer.getResource(FeatureSource.class, null); FS.getSchema())
* @return Style : the style
* @throws IllegalFilterException
*/
public static Style buildTypedStyle(String name,Color[]
colors,String[] setofdata, FeatureType schema) throws
IllegalFilterException {
int count =setofdata.length;
//grab attribute col
StyleBuilder sb = new StyleBuilder();
FilterFactory ff = sb.getFilterFactory();
StyleFactory sf = sb.getStyleFactory();
Style ret = sb.createStyle();
AttributeExpression value = sb.getFilterFactory
().createAttributeExpression(schema, name);
Rule[] rules = new Rule[count+1]; // one more in order
to add the ELSE rule entry
for (int i=0;i<count;i++)
{
LikeFilterImpl cf1 = (LikeFilterImpl)
ff.createLikeFilter();
LiteralExpression le = ff.createLiteralExpression
(setofdata[i]);
cf1.setValue(value);
cf1.setPattern(le,"*","?","\\"); //+"*"
rules[i] = sf.createRule();
rules[i].setFilter(cf1);
Color c;
if (colors== null)
c = createRandomColor();
else
{
c = colors[i];
//if (c== null)
// c = createRandomColor();
}
PolygonSymbolizer symb1 = sb.createPolygonSymbolizer
(c, Color.black, 1.0);
rules[i].setSymbolizers(new Symbolizer[] { symb1 });
}
// we build the else
rules[count] = sf.createRule();
PolygonSymbolizer elsePoly = sb.createPolygonSymbolizer
(Color.white, 1.0);
rules[count].setSymbolizers(new Symbolizer[] { elsePoly });
rules[count].setIsElseFilter(true);
FeatureTypeStyle ft = sf.createFeatureTypeStyle(rules);
ft.setFeatureTypeName("Feature");
ft.setName(name);
ret.addFeatureTypeStyle(ft);
return ret;
}
private static Random random = new Random();
public static Color createRandomColor() {
return new Color(random.nextInt(200), random.nextInt(200),
random.nextInt(200));
}
private Color createColor(String text) {
int i = Integer.decode("0x" + text).intValue();
return Color.decode("" + i);
}
have fun
be aware : can't be editable because of some uggly problem with Style/
XML serialization (the problem is possibly fixed i do not check lately)
okay here a code to apply :
StyleLayer sl = new StyleLayer(selectedLayer);
StyleBlackboard sbb = sl.getStyleBlackboard();
sbb.put(SLDContent.ID, style);
sl.setStyleBlackboard(sbb);
sl.apply();
jacques divol