Querying multiple layers in a single query with GeoServer

Our problem: to get features by filtering different layers using WFS.

Let’s work with the topp layers, from default GeoServer data. Applying a single CQL_filter on a WFS layer is simple. We have the “unknown” 😉 topp:tasmania_water_bodies published in a Demo GeoServer (thanks GeoSolutions ;-)) and we want to get the features where the area is greater than 1066494066 square meters, so we can make the following query to the server

https://demo.geo-solutions.it/geoserver/wfs?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&typeNames=topp:tasmania_water_bodies&propertyName=&cql_filter=AREA<1066494066&outputFormat=application/json

Easy! We’ll get 4 features that satisfy filters. The syntax is clear, we must use typeNames=topp:tasmania_water_bodies and cql_filter=AREA<1066494066 in the query. But, what must we do if we want get features from two or more layers at the same time?

If we get at the same time the features type Alley from the topp:tasmania_roads layer with the topp:tasmania_water_bodies features filtered before, we must use similar request but separating the typeNames like this:

typeNames=(topp:tasmania_water_bodies)(topp:tasmania_roads)

and setting the associated CQL filters ordered by the typenames:

cql_filter=AREA<1066494066;TYPE='alley'

https://demo.geo-solutions.it/geoserver/wfs?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&typeNames=(topp:tasmania_water_bodies)(topp:tasmania_roads)&propertyName=&cql_filter=AREA<1066494066;TYPE=‘alley’&outputFormat=application/json

What about the propertyName? By using the propertyName parameter on the WFS petition we can filter the returned feature properties the way we want to. This parameter is important to reduce the size of the response: we get only the properties we are interested in.

In our example, we can get only the CNTRY_NAME of the topp:tasmania_water_bodies and the TYPE of the topp:tasmania_roads:

https://demo.geo-solutions.it/geoserver/wfs?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&typeNames=(topp:tasmania_water_bodies)(topp:tasmania_roads)&propertyName=(CNTRY_NAME)(TYPE)&cql_filter=AREA%3C1066494066;TYPE=%27alley%27&outputFormat=application/json

Michogar’s GIST

Enjoy!!

Do you have a project idea and want to turn it into reality? We would like to hear from you, tell us about it

The facts define us