WFS query and temporal-functions

Hi,
in geoserver there are the temporal functions: now returns the current time as a Date.

Is there a way to run a WFS query using now? I have tried using this query URL, but it does not work because it’s wrong.

https://sample.com/ows?service=WFS&request=GetFeature&typeName=view_can&CQL_FILTER=field_name < now()

Thank you

1 Like

Probably it’s not enabled on the server.

If yes, how to enable it?

Hi Andrea,
“now”, written like that, is a special keyword that works only in the WMS time support.

However, in CQL one case use all registered functions, and if you have a GeoServer that’s
recent enough (a release from the last couple of years),
there is a “now()” function instead… which I see you’re already trying to use.

What do you mean “does not work because it’s wrong”? What result do you get?
Internally the now function generates the current local date… wondering if you’re having a timezone problem?

Regards,

1 Like

Thank you, Andrea. I thought it was wrong for two reasons:

  • because I can’t find any help documentation to evaluate it;
  • because I had “Could not parse CQL filter list. Function not found. Parsing : data_end &lt; now().”

And it was actually wrong because I used %20%3C%20now() and not %20%3C%20'now()'.

I take the opportunity to add a question: is there a way to say in the query between today and the last 30 days?
Something like CQL_FILTER=field_name <= 'now()' AND data_fine >= 'now() - 30 days'.

Thank you

1 Like

GeoMesa supports this functionality, with the currentDate function, for example, currentDate('-P1D'). The functionality comes from the geomesa-filter module, which can be installed in GeoServer (make sure the supported version is correct). Installation will require transitive dependencies too - the easiest thing is probably to just install the GeoMesa Redis plugin, which is the smallest plugin that includes all the necessary jars.

(discourse will only let me post 2 links, so I’ll put additional links in a follow on)

Thanks,
Emilio

2 Likes

Hmm, I seem to have been flagged as a spammer :confused: Are my posts actually considered against the community guidelines, or is there a way to get them re-instated?
thanks,
Emilio

1 Like

Hi Emilio,
I can see your post from an incognito brower tab too, so they have been published and are visible to anyone.

I can see the message has been put in the moderator queue because, quoting

“This new user tried to create multiple posts with links to the same domain. All posts from this user that include links should be reviewed. See the newuser_spam_host_threshold site setting.”

I believe the control of how many links can be used in a post is in the hands of OSGeo itself, and I’m not sure for how long someone is considered “a new user”.

@jive any clue about this?

Cheers
Andrea

Hi Andrea,
the documentation you need is here:
https://docs.geoserver.org/2.25.x/en/user/filter/function_reference.html#temporal-functions

The filtering will be something like this (mind, not tested):
dateDifference(now(), data_fine, 'd') < 30.

Also, depending on the datasource you’re using, the above filter can be delegated down to storage, or executed in memory.
Currently I believe only the postgis data store is able to execute that function natively.

Cheers
Andrea

I can’t write the query right. This one work (data_fine < 'now()' AND data_fine >= '2024-11-30')

CQL_FILTER=data_fine%20%3C%20%27now()%27%20AND%20data_fine%20%3E%3D%20%272024-11-30%27"

But these are not:

  • CQL_FILTER=dateDifference%28%27now%28%29%27%2C%20data_fine%2C%20%27d%27%29%20%3C%20%2730%27
  • CQL_FILTER=dateDifference%28%27now%28%29%27%2C%20data_fine%2C%20%27d%27%29%20%3C%2030

I started from

CQL_FILTER=dateDifference('now()', data_fine, 'd') < '30'

The error is:

<ows:ExceptionText>Could not parse CQL filter list.
Function not found. Parsing : dateDifference(&amp;apos;now()&amp;apos;, data_fine, &amp;apos;d&amp;apos;) &amp;lt; &amp;apos;30&amp;apos;.</ows:ExceptionText>

This works:

curl -X POST \
  https://mysite/ows \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "service=WFS&version=2.0.0&request=GetFeature&typeName=view_test&cql_filter=data_fine < 'now()' AND data_fine >= '2024-10-30'"

Not this one

curl -X POST \
  https://mysite/ows \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "service=WFS&version=2.0.0&request=GetFeature&typeName=view_test&cql_filter=dateDifference(now(), data_fine, 'd') < 30""

The error

<?xml version="1.0" encoding="UTF-8"?><ows:ExceptionReport xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.0" xsi:schemaLocation="http://www.opengis.net/ows/1.1 https://gestione-autorizzazioni.comune.bari.it/cnt/geoserver/schemas/ows/1.1.0/owsAll.xsd">
<ows:Exception exceptionCode="NoApplicableCode">
<ows:ExceptionText>Could not parse CQL filter list.
Function not found. Parsing : dateDifference(now(), data_fine, &amp;apos;d&amp;apos;) &amp;lt; 30.</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>

To use dateDifference in this way do I must install GeoFence?

Thank you

Hi Andrea,
the correct way to write it is:

data_fine < now() AND dateDifference(now(), data_fine, ‘d’) < 30

and its url encoded form, according to https://www.urlencoder.org/ is

data_fine%20%3C%20now%28%29%20AND%20dateDifference%28now%28%29%2C%20data_fine%2C%20%27d%27%29%20%3C%2030

now() is a function, while ‘now()’ is just a static string. I have the impression you’re trying to work
around a GeoServer that does not really have the “now” function. Which version are you using?

Ciao Andrea,
I don’t want to bore you :slight_smile:

It seems to me that now() works, but dateDifference does not work.

In this way I have 7 items

CQL_FILTER=data_fine%20%3E%20%27now%28%29%27

In this way I have 87 items

CQL_FILTER=data_fine%20%3C%20%27now%28%29%27

Unfortunately, I cannot easily trace the version of geoserver.

If I use dateDifference instead, I get the error responses I wrote you.

Trust level is earned by reading and especially replying to things. So congratulating the developers on a new release is a good way to start :slight_smile:

More seriously @aaime-geosolutions can adjust someone’s trust level as a moderator.

1 Like

thank you! I guess I was being too thorough in my links to documentation :laughing: