Hi Nenad,

I've just tested using the function in a SLD filter (see below) and it
worked but after a minor fix. I was overriding the 'evaluate' method
that takes a SimpleFeature arg but you need to use the version that
takes an Object arg for the function to be evaluated by the GeoTools
rendering system.

Here is the code that worked:

import java.util.LinkedHashMap;
import java.util.Map;
import org.geotools.filter.FunctionExpressionImpl;

public class VarLookupFunction extends FunctionExpressionImpl {
    private static final Map<String, Object> table = new
LinkedHashMap<String, Object>();

    public VarLookupFunction() {
        super("varLookup");
    }

    public static void setValues(Map<String, Object> values) {
        table.clear();
        if (values != null) {
            table.putAll(values);
        }
    }

    public static void setValue(String name, Object value) {
        table.put(name, value);
    }

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

    @Override
    public Object evaluate(Object feature) {
        String varName = getExpression(0).evaluate(feature, String.class);
        return table.get(varName);
    }
}

Note, for the moment please ignore the later version of the function
that I posted with the ThreadLocal lookup. That won't work with
JMapPane at the moment because the rendering is done on a background
thread.

Hopefully, soon there will be a function, probably called "env" or
"environment", added to GeoTools but in the meantime the above hack
should do what you need.

Michael

<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0"
    xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd"
    xmlns="http://www.opengis.net/sld";
    xmlns:ogc="http://www.opengis.net/ogc";
    xmlns:xlink="http://www.w3.org/1999/xlink";
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
  <NamedLayer>
    <Name>Simple polygon</Name>
    <UserStyle>
      <Title>GeoServer SLD Cook Book: Simple point</Title>
      <FeatureTypeStyle>
        <Rule>
            <Filter>
              <PropertyIsEqualTo>
                <PropertyName>COLOR_MAP</PropertyName>
                <Function name="VarLookup">
                  <Literal>colorMapValue</Literal>
                </Function>
              </PropertyIsEqualTo>
            </Filter>
          <PolygonSymbolizer>
            <Fill>
              <CssParameter name="fill">#00A0A0</CssParameter>
            </Fill>
          </PolygonSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing. 
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to