24

I'm trying to use PHP's CURL function to fetch geoJSON information from a GeoServer instance. I'm doing this using URL variables rather than trying to construct a full XML GetFeature request.

However, I would like to be able to get a subset of the results based on some of the property fields' contents.

So, I can get all of the results using this URL:

http://www.myURL.com/geoserver/namespace/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=layername&outputFormat=json&BBOX=1,2,3,4

can I also limit the results to "Where field LIKE value" or "field = value"?

I've spent an hour trawling through the Geoserver/ECQL documentation and none of it clearly states "append the functions to your URL in this format". As a result, I'm not clear if it's possible to use URL variables to perform these tasks, but some people seem to suggest that it is.

Can anyone help with a clear example of a working filter using GeoServer's WFS with URL parameters?

1 Answer 1

36

I suppose you have been reading this http://docs.geoserver.org/latest/en/user/tutorials/cql/cql_tutorial.html

Here comes some sample requests which are sending queries for the demo server of Boundless but which should work similarly with your own server if you have demo layer topp:states installed.

Select where STATE_NAME is Illinois

http://demo.opengeo.org/geoserver/wfs?service=wfs&version=1.0.0&request=getfeature&typename=topp:states&PROPERTYNAME=STATE_NAME&CQL_FILTER=STATE_NAME='Illinois'

Notice the use of standard WFS parameter PROPERTYNAME which is used here for shortening the output. Leave is out if you want all attributes, or write a list for selecting some attributes.

Then select states with name starting with "I"

http://demo.opengeo.org/geoserver/wfs?service=wfs&version=1.0.0&request=getfeature&typename=topp:states&PROPERTYNAME=STATE_NAME&CQL_FILTER=STATE_NAME LIKE 'I%25'

Notice that the comparison string is 'I%' but at least with my browser (Firefox 31.0) it must be URL-encoded and it comes 'I%25'. This is not mentioned in the CQL tutorial. If your own filters fail it may mean that you must URL-encode also some other other characters in your filter.

If you need geojson add &outputformat=application/json

http://demo.opengeo.org/geoserver/wfs?service=wfs&version=1.0.0&request=getfeature&typename=topp:states&PROPERTYNAME=STATE_NAME&CQL_FILTER=STATE_NAME LIKE 'I%25'&outputformat=application/json
4
  • 11
    For anyone else struggling with this, you can also add multiple CQL filters to the URL using the format &CQL_FILTER=field=value AND field=value AND field=value - but you need to URL encode the " AND " so that the spaces are encoded. Hope this helps save someone else time.
    – Ambulare
    Commented Aug 14, 2014 at 9:23
  • Very useful answer, the geoserver tutorial just leaves out so many things. How would you extend the query to return the feature that has XY coordinates? Or all features inside a bounding box?
    – Luffydude
    Commented Mar 20, 2017 at 12:11
  • @Luffydude If you need the entire feature, just skip the "&PROPERTYNAME=STATE_NAME" in the query. You will then get the entire feature including the geometry Commented Jul 24, 2020 at 10:04
  • For encoding your url, just use one of the online tools. I just googles and found this one: url-encode-decode.com For instance, the &CQL_FILTER=field=value AND field=value AND field=value becomes %26CQL_FILTER%3Dfield%3Dvalue+AND+field%3Dvalue+AND+field%3Dvalue.
    – shaheen g
    Commented Aug 20, 2021 at 23:46

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