Querying multiple layers in a single query with GeoServer?

Our problem, get multiple features filtered from multiple different layers using WFS.

Let’s work with the topp data coming by default in GeoServer. A matter of taste.

To apply a single CQL_filter on a WFS layer it’s simple. We have the “unknown” 😉 topp:tasmania_water_bodies published in one 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 next petition 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 petition.

But, how we must 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 in this way:

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

and setting the associated CQL filters ordered respect 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

And, what about the propertyName?. Using the propertyName parameter on the WFS petition we can filter the returned feature properties as we want. This parameter is important to reduce the response’s size getting 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.

In this case the request will be:

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

  • 9 years with Barcelona Natural Science Museum

    Geolocation is a key attribute for activity in a natural science museum since cartography is present in almost all its fields of activity: collections, research, publications, exhibitions, etc. The collaboration…

  • Customised GIS training for your sector

    Our experience tells us that the best way to consolidate knowledge and have your staff effectively integrate GIS as a daily tool is through customised courses for your business. Generalist…

  • One kilometer from home

    On March the 23rd, after five weeks of COVID-19 lockdown, president Sanchez announced that Spain will begin easing restrictions. In this first attempt at  loosening measures across the country, children…

  • Querying multiple layers in a single query with GeoServer?

    Our problem, get multiple features filtered from multiple different layers using WFS. Let’s work with the topp data coming by default in GeoServer. A matter of taste. To apply a…