Hi Enamul,

> Turns out that SLDStyleFactory will cache the style if the fill
> expression doesn't have at least one attribute set as argument. I was
> using a ColorRampFunction that takes zero arguments, which made
> SLDStyleFactory think that this is a static style.  Doing this
> resolves it:
>
>        propertyName_ = filterFactory_.property("the_geom");
>        super.setParameters(new ArrayList()
>        {{
>            add(propertyName_);
>        }});
>

Well done !  I couldn't see why your code wasn't working, but testing
it here with a similar case shows that you are quite right about the
behaviour of SLDStyleFactory.

I'll add this to the material that we cover in our examples and put an
item about it into the FAQ because it will surely trap others in the
future :-)

In case it's useful, here is a more GeoTools-ized version of the function...

import java.awt.Color;
import java.util.List;
import org.geotools.filter.FunctionExpressionImpl;

public class ColorPickerFunction extends FunctionExpressionImpl {

    public ColorPickerFunction() {
        super("colorfn");
    }

    @Override
    public int getArgCount() {
        return 2;
    }

    @Override
    public Object evaluate(Object feature) {
        int value = getExpression(0).evaluate(feature).hashCode();
        List<Color> colors = (List<Color>) getExpression(1).evaluate(feature);
        int index = value % colors.size();
        return colors.get(index);
    }
}

And here is an example of use via the standard filter factory API to
create a polygon style...

        StyleFactory sf = CommonFactoryFinder.getStyleFactory(null);
        FilterFactory2 ff2 = CommonFactoryFinder.getFilterFactory2(null);

        Function fn = ff2.function("ColorFn",
                ff2.property("the_geom"),
                ff2.literal(Arrays.asList(Color.BLUE, Color.CYAN,
Color.GREEN, Color.RED, Color.YELLOW)));

        Fill fill = sf.createFill(fn);
        Stroke stroke = sf.createStroke(fn, ff2.literal(1.0f));
        Symbolizer sym = sf.createPolygonSymbolizer(stroke, fill, "the_geom");

        Style style = SLD.wrapSymbolizers(sym);

------------------------------------------------------------------------------

_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to