1

I use a GeoServer which i query over Openlayers with WMS and CQL.

I have a date field in my database which i want to filter with CQL.

I want only have Dates which has the weekday monday.

The Result would contain only two dates.

id   time   
1    12.10.2015 12:26:31
2    19.10.2015 13:45:00
3    20.10.2015 12:16:11
4    21.10.2015 12:03:45
5    22.10.2015 12:01:45

I don't see any way to do that. Are the some functions to do that? I was thinking of use a formular to calculate the weekday, but therefore i need the day, month, year and century from a date and i don't see any way to extracts this values.

0

If it is your own GeoServer and database you can create a weekday attribute by making a new layer through SQL View http://docs.geoserver.org/stable/en/user/data/database/sqlview.html

Select the weekday from from the date column with SQL

select to_char(DATE '2001-02-16','Day')
Friday

http://grokbase.com/t/postgresql/pgsql-general/0898jbyc4z/…;. – user30184 6 mins ago

0

GeoTools (and hence GeoServer) allows you to write your own custom functions to use in filters. There is a tutorial that explains all the steps. So all you would need is a function implementation that returned the day of the week, something like:

package spike;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.print.attribute.standard.MediaSize.NA;

import org.geotools.filter.capability.FunctionNameImpl;
import org.geotools.util.Converters;
import org.opengis.filter.capability.FunctionName;
import org.opengis.filter.expression.Expression;
import org.opengis.filter.expression.ExpressionVisitor;
import org.opengis.filter.expression.Function;
import org.opengis.filter.expression.Literal;

public class DayOfWeekFunction implements Function {

    static FunctionName NAME = new FunctionNameImpl("DayOfWeek", String.class,
        FunctionNameImpl.parameter("date", Date.class));
    private List<Expression> params;
    private Literal fallback;

    public DayOfWeekFunction(List<Expression> params, Literal fallback) {
        if (params == null) {
            throw new NullPointerException("No parameters supplied");
        }
        if (params.size() != 1) {
            throw new IllegalArgumentException("DayOfWeek(date) requires exactly one argument");
        }
        this.params = params;
        this.fallback = fallback;
    }

    @Override
    public Object evaluate(Object object) {
        return evaluate(object, String.class);
    }

    @Override
    public <T> T evaluate(Object object, Class<T> context) {
        Expression dateExpression = params.get(0);
        Date date = dateExpression.evaluate(object, Date.class);
        String day = new SimpleDateFormat("E").format(date);
        return Converters.convert(day, context);
    }

    @Override
    public Object accept(ExpressionVisitor visitor, Object extraData) {
        return visitor.visit(this, extraData);
    }

    @Override
    public String getName() {
        return NAME.getName();
    }

    @Override
    public FunctionName getFunctionName() {
        return NAME;
    }

    @Override
    public List<Expression> getParameters() {
        return params;
    }

    @Override
    public Literal getFallbackValue() {

        return fallback;
    }

}
  • This is a nice solution when i would have a complex feature. The only question is where do you integrate this in my geoserver? – nik the lion Oct 28 '15 at 10:18
  • You build a jar with the filter and the META-INF/service/... file in it and then drop it into your geoserver WEB-INF/lib folder and restart. The rest is magic! – Ian Turton Oct 28 '15 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.