GeoServer Blog
Using Logical Operators in GeoServer Filters
GeoSpatial Techno is a startup focused on geospatial information that is providing e-learning courses to enhance the knowledge of geospatial information users, students, and other startups. The main approach of this startup is providing quality, valid specialized training in the field of geospatial information.
( YouTube | LinkedIn | Facebook | X )
Using Logical Operators in GeoServer Filters
In this session, we want to talk about the Using logical operators to combine multiple filters in GeoServer. If you want to access the complete tutorial, click on the link.
Introduction
Logical operators in Web Feature Service (WFS) filtering are essential for combining multiple conditions within GeoServer. These operators enable users to construct complex queries, facilitating data retrieval from the WFS service. The primary logical operators include AND, OR, and NOT.
Note. This video was recorded on GeoServer 2.22.4, which is not the most up-to-date version. Currently, versions 2.25.x and 2.26.x are supported. To ensure you have the latest release, please visit this link and avoid using older versions of GeoServer.
AND
This operator combines multiple conditions into a single filter expression. The resulting expression matches only those features that meet all of the specified criteria.
- As an example of using this filter in WFS getFeature request, navigate to the Demos page, then select Demo requests.
- From the Request section, select the WFS_getFeature1.0.xml request.
Use the following block codes to replace line 26:
<ogc:And>
<PropertyIsLessThan>
<PropertyName>LAND_KM</PropertyName>
<Literal>100000</Literal>
</PropertyIsLessThan>
<PropertyIsGreaterThan>
<PropertyName>PERSONS</PropertyName>
<Literal>5000000</Literal>
</PropertyIsGreaterThan>
</ogc:And>
Note. In all examples in this blog post, we utilize the topp:states
layer.
In this example, we applied a filter to the layer, where the value of the LAND_KM
attribute, is less than 100,000 and the PERSONS
is greater than 5 million people.
The results include three states: Massachusetts
, New Jersey
and Indiana
.
To use the CQL filtering to apply the equivalent of this example, first, preview the top:states
layer in the Layer Preview section. Then, add the filter CQL_FILTER=LAND_KM<100000 And PERSONS>5000000
to the end of the URL.
The complete URL for the layer is as follows:
http://localhost:8080/geoserver/topp/wms?service=WMS&version=1.1.0&request=GetMap&layers=topp%3Astates&bbox=-124.73142200000001,24.955967,-66.969849,49.371735&width=768&height=330&srs=EPSG%3A4326&styles=&format=application/openlayers&CQL_FILTER=LAND_KM<100000 And PERSONS>5000000
OR
This operator allows you to combine multiple conditions and retrieve features that satisfy any of the specified conditions. In simpler terms, at least one condition must be true for the filter to be considered a match.
Here’s an example of how to use the “OR” operator to filter a WFS layer based on two conditions.
<ogc:Or>
<PropertyIsLessThan>
<PropertyName>LAND_KM</PropertyName>
<Literal>100000</Literal>
</PropertyIsLessThan>
<PropertyIsGreaterThan>
<PropertyName>PERSONS</PropertyName>
<Literal>5000000</Literal>
</PropertyIsGreaterThan>
</ogc:Or>
Press the Submit button.
In this example, we filtered the layer to display features that meet either of these conditions: The value of the LAND_KM
attribute is less than 100,000 or the PERSONS
attribute represents a population greater than 5 million people. The results include 25
states.
To apply the same example using the CQL filtering and observe the results, use the following code in the URL of the layer as mentioned above:
http://localhost:8080/geoserver/topp/wms?service=WMS&version=1.1.0&request=GetMap&layers=topp%3Astates&bbox=-124.73142200000001,24.955967,-66.969849,49.371735&width=768&height=330&srs=EPSG%3A4326&styles=&format=application/openlayers&CQL_FILTER=LAND_KM<100000 Or PERSONS>5000000
NOT
In GeoServer, the NOT operator, also known as the logical negation operator, is used to invert the meaning of a filter expression. It takes one or more filter expressions and returns features that don’t meet the specified conditions.
Here’s an example of using the “NOT” operator for filtering a WFS layer by two conditions:
<ogc:Not>
<ogc:Or>
<PropertyIsLessThan>
<PropertyName>LAND_KM</PropertyName>
<Literal>100000</Literal>
</PropertyIsLessThan>
<PropertyIsGreaterThan>
<PropertyName>PERSONS</PropertyName>
<Literal>5000000</Literal>
</PropertyIsGreaterThan>
</ogc:Or>
</ogc:Not>
Press the Submit button.
In this example, we filtered the layer to show features that don’t meet any of these conditions. That is, neither the value of the LAND_KM
attribute is less than 100,000 nor is the value of the PERSONS
parameter more than 5 million people. The results include 24
states.
To see how to use the NOT operator in CQL filtering, use the following code at the end of the URL’s layer:
http://localhost:8080/geoserver/topp/wms?service=WMS&version=1.1.0&request=GetMap&layers=topp%3Astates&bbox=-124.73142200000001,24.955967,-66.969849,49.371735&width=768&height=330&srs=EPSG%3A4326&styles=&format=application/openlayers&CQL_FILTER=NOT(LAND_KM<100000 Or PERSONS>5000000)
Combine operators
GeoServer provides the capability of combining logical operators with geometric filters, enhancing the flexibility of WFS filtering. This feature enables users to create more specific and reliable filtering criteria.
Here is an example that effectively uses both spatial and comparison filtering:
<ogc:Filter>
<ogc:And>
<ogc:Intersects>
<ogc:PropertyName>the_geom</ogc:PropertyName>
<gml:LineString xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
<gml:coordinates>-73.9,43.5 -81.1,38.6 -78.57,35.5</gml:coordinates>
</gml:LineString>
</ogc:Intersects>
<PropertyIsGreaterThanOrEqualTo>
<PropertyName>PERSONS</PropertyName>
<Literal>10000000</Literal>
</PropertyIsGreaterThanOrEqualTo>
</ogc:And>
</ogc:Filter>
In this example, we filtered out states with populations exceeding 10 million, as well as states intersected by a LineString with given coordinates.
Thus, we identified New York
and Pennsylvania
as the two states that satisfy these conditions.
To see how to use CQL filtering for this example, follow the instance shown on the screen:
CQL_FILTER=INTERSECTS(the_geom,LINESTRING(-73.9 43.5,-81.1 38.6,-78.57 35.5)) AND PERSONS>10000000
As a final example, we examine a comprehensive scenario that incorporates different operators. This example includes a spatial operator, such as the Within filter. Additionally, it showcases two comparison operators, namely PropertyIsLike
and PropertyIsGreaterThan
.
To better understand these concepts, use the following example:
<ogc:Filter>
<ogc:And>
<ogc:Within>
<ogc:PropertyName>the_geom</ogc:PropertyName>
<gml:Polygon xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
<gml:outerBoundaryIs>
<gml:LinearRing>
<gml:coordinates>-100,30 -100,45 -80,45 -80,30 -100,30</gml:coordinates>
</gml:LinearRing>
</gml:outerBoundaryIs>
</gml:Polygon>
</ogc:Within>
<ogc:Or>
<ogc:Filter>
<ogc:PropertyIsLike wildCard="%" singleChar="_" escape="!">
<ogc:PropertyName>STATE_NAME</ogc:PropertyName>
<ogc:Literal>%na%</ogc:Literal>
</ogc:PropertyIsLike>
</ogc:Filter>
<PropertyIsGreaterThan>
<PropertyName>STATE_FIPS</PropertyName>
<Literal>30</Literal>
</PropertyIsGreaterThan>
</ogc:Or>
</ogc:And>
</ogc:Filter>
In this example, we use WFS filtering to extract the States that are completely enclosed within specific coordinates. Moreover, we retrieve States whose STATENAME
includes the letters “na” or whose STATEFIPS
value is greater than 30.
Therefore, we have identified three states that meet the specified criteria: Tennessee
, Indiana
, and Ohio
.
To use the CQL filtering for this example, use the following code:
CQL_FILTER=Within(the_geom,Polygon((-100 30,-100 45,-80 45,-80 30,-100 30))) AND (STATE_NAME LIKE '%na%' OR STATE_FIPS > 30)
In this session, we took a brief journey through the Combining multiple operators for WFS filtering in GeoServer. If you want to access the complete tutorial, click on the link.
GeoServer 2.25.5 Release
GeoServer 2.25.5 release is now available with downloads (bin, war, windows), along with docs and extensions.
This is a maintenance release of GeoServer providing existing installations with minor updates and bug fixes. GeoServer 2.25.5 is made in conjunction with GeoTools 31.5, and GeoWebCache 1.25.3. The final release of the 2.25 series is planned for February 2025, please start making plans for an upgrade to 2.26.x or newer.
Thanks to Andrea Aime (GeoSolutions) for making this release.
Release notes
Improvement:
- GEOS-11612 Add system property support for Proxy base URL -> use headers activation
- GEOS-11616 GSIP 229 - File system access isolation
- GEOS-11644 Introducing the rest/security/acl/catalog/reload rest endpoint
Bug:
- GEOS-11494 WFS GetFeature request with a propertyname parameter fails when layer attributes are customized (removed or reordered)
- GEOS-11606 geofence-server imports obsolete asm dep
- GEOS-11611 When Extracting the WFS Service Name from the HTTP Request A Slash Before the Question Marks Causes Issues
- GEOS-11643 WCS input read limits can be fooled by geotiff reader
Task:
- GEOS-11609 Bump XStream from 1.4.20 to 1.4.21
- GEOS-11610 Update Jetty from 9.4.55.v20240627 to 9.4.56.v20240826
- GEOS-11631 Update MySQL driver to 9.1.0
For the complete list see 2.25.5 release notes.
Community Updates
Community module development:
- GEOS-11635 Add support for opaque auth tokens in OpenID connect
- GEOS-11637 DGGS min/max resolution settings stop working after restart
Community modules are shared as source code to encourage collaboration. If a topic being explored is of interest to you, please contact the module developer to offer assistance.
About GeoServer 2.25 Series
Additional information on GeoServer 2.25 series:
- GeoServer 2.25 User Manual
- GeoServer 2024 Roadmap Plannings
- Raster Attribute Table extension
- Individual contributor clarification
Release notes: ( 2.25.5 | 2.25.4 | 2.25.3 | 2.25.2 | 2.25.1 | 2.25.0 | 2.25-RC )
GeoServer 2.26.1 Release
GeoServer 2.26.1 release is now available with downloads (bin, war, windows), along with docs and extensions.
This is a stable release of GeoServer recommended for production use. GeoServer 2.26.1 is made in conjunction with GeoTools 32.1, and GeoWebCache 1.26.1.
Thanks to Peter Smythe (AfriGIS) for making this release.
Security Considerations
This release addresses security vulnerabilities and is considered an important upgrade for production systems.
- GEOS-11557 CVE-2024-45748 High. The details will be released later.
See project security policy for more information on how security vulnerabilities are managed.
Release notes
Improvement:
- GEOS-11557 CVE-2024-45748 High
- GEOS-11561 Client-Delegating MapML Proxy
- GEOS-11588 GWC disk quota, check JDBC connection pool validation query
Bug:
- GEOS-11524 csw: default queryables mapping not generated
- GEOS-11543 Unable to use propertyName to filter properties in a GetFeature request when service is not set
- GEOS-11553 SLD Style: Empty SE Rotationelement throws RuntimeException (QGIS generated SLD)
- GEOS-11556 NullPointerException when GWC disk quota monitoring is disabled
- GEOS-11559 The customized attributes editor is prone to setting the wrong attribute source
- GEOS-11573 TileLayer preview doesn’t work anymore
Task:
- GEOS-11574 Bump org.eclipse.jetty:jetty-server from 9.4.52.v20230823 to 9.4.55.v20240627 in /src
- GEOS-11587 Update map fish-print-v2 2.3.2 - see new MAPFISH_PDF_FOLDER configuration option
- GEOS-11609 Bump XStream from 1.4.20 to 1.4.21
- GEOS-11610 Update Jetty from 9.4.55.v20240627 to 9.4.56.v20240826
For the complete list see 2.26.1 release notes.
Community Updates
Community module development:
- GEOS-11107 Open search for EO community module: packaging missing gt-cql-json-xx.x.jar
- GEOS-11517 Using various OGC APIs results in service enabled check related WARN logs
- GEOS-11560 OGC API modules lack cql2-json in assembly
- GEOS-11563 Allow configuring a DGGS resolution offset on a layer basis
- GEOS-11565 Allow configuring the minimum and maximum DGGS resolution for a layer
- GEOS-11579 DGGS modules prevent GeoServer startup if JEP is not installed
Community modules are shared as source code to encourage collaboration. If a topic being explored is of interest to you, please contact the module developer to make contact and offer assistance, even if it is just to say that it works for you.
About GeoServer 2.26 Series
Additional information on GeoServer 2.26 series:
Exploring CQL/ECQL Filtering in GeoServer
GeoSpatial Techno is a startup focused on geospatial information that is providing e-learning courses to enhance the knowledge of geospatial information users, students, and other startups. The main approach of this startup is providing quality, valid specialized training in the field of geospatial information.
( YouTube | LinkedIn | Facebook | X )
Exploring CQL and ECQL Filtering in GeoServer
In this session, we want to talk about the Using CQL/ECQL Filters in GeoServer in detail. If you want to access the complete tutorial, click on the link.
Introduction
Contextual Query Language (CQL) is a text-based query for search/retrieval adopted by the OGC for the Catalogue Web Services specification. Unlike the XML-based Filter Encoding language, CQL is more readable and easier for manual authoring. However, it has limitations, such as not being able to encode ID filters and requiring the attribute to be on the left side of comparison operators.
To overcome these limitations, GeoServer offers an extended version called ECQL, which closely resembles SQL and provides greater flexibility. ECQL allows users to define filters for querying data in GeoServer using attribute comparisons, logical operators, and spatial predicates. It is compatible with GeoServer’s REST API and can be used for WMS and WFS requests to retrieve filtered data.
Note. This video was recorded on GeoServer 2.22.4, which is not the most up-to-date version. Currently, versions 2.25.x and 2.26.x are supported. To ensure you have the latest release, please visit this link and avoid using older versions of GeoServer.
Note. Future version of GeoServer will include support for CQL2 which provides both a text and a JSON representation.
Comparison operators
To compare attribute values or other numeric and text values in your CQL / ECQL (Extended Common Query Language) expressions, you can utilize comparison operators.
In the Layer Preview section, first click on the OpenLayers option for the topp:states
layer. Next, locate and click on the Toggle options toolbar in the top left corner to access the advanced options.
In the CQL filter box within this toolbar, enter the filter expression STATE_NAME = 'Texas'
, and then press the Apply button. This filter will retrieve and display the data for the state of Texas.
By reviewing the following examples using the Toggle options toolbar from the LayerPreview page, you will learn how to effectively understand and apply comparison operators using CQL/ECQL expressions:
- ‘PropertyIsGreaterThanOrEqualTo’ filter
This filter shows the states that have more than or equal to 5 million inhabitants.
PERSONS >= 5000000
- ‘PropertyIsLike’ filter
This filter shows the states whose names, contain the letters ‘ing’ like Washington and Wyoming.
STATE_NAME like '%ing%'
- ‘PropertyIsBetween’ filter
This filter shows the states with a population of 5 million to 10 million.
PERSONS between 5000000 and 10000000
Spatial operators
These operators enable you to perform spatial queries and filter data, based on various relationships between geometries. Here are the explanations for some commonly used spatial operators:
- ‘Intersect’ filter
This filter allows you to query spatial data in GeoServer based on geometric intersection relationships. This filter returns all features that have any spatial intersection or overlap.
The syntax for the Intersect filter in CQL is as follows:
Intersects(the_geom,Point(-90 40))
- ‘Within’ filter
The Within filter checks if a spatial object is completely within another spatial object. This filter retrieves all features that are located within the boundaries of a specified geometric shape, using spatial relationships.
Within(the_geom,Polygon((-100 30,-100 45,-80 45,-80 30,-100 30)))
- ‘Contains’ filter
This filter is the inverse of the “Within” filter. It checks if a spatial object completely contains another spatial object and helps you retrieve features that fully enclose the specified geometry.
CONTAINS(the_geom,LINESTRING(-73.9 43.5,-77.76 42.56))
Bounding Box operators
The Bounding Box operator is used to filter data based on a specified bounding box. The “bbox” filter in CQL allows you to query spatial data in GeoServer based on a bounding box or a rectangular area.
CQL filters can also be utilized with the GET method. To use the bbox filter using the GET method, enter the following code in the URL address bar of your browser:
http://localhost:8080/geoserver/topp/wms?service=WMS&version=1.1.0&request=GetMap&layers=topp:states&bbox=-124.73142200000001,24.955967,-66.969849,49.371735&width=768&height=330&srs=EPSG:4326&format=application/openlayers&CQL_FILTER=BBOX(the_geom,-110,41,-95,45)
This filter enables you to retrieve all features that intersect, or are contained within the specified bounding box.
In this session, we took a brief journey through the “CQL filtering in GeoServer”. If you want to access the complete tutorial, click on the link.
GeoServer 2.25.4 Release
GeoServer 2.25.4 release is now available with downloads (bin, war, windows), along with docs and extensions.
This is a maintenance release of GeoServer providing existing installations with minor updates and bug fixes. GeoServer 2.25.4 is made in conjunction with GeoTools 31.4, and GeoWebCache 1.25.3.
Thanks to Jody Garnett for making this release.
Update 2024-11-08: Testing from Sören Kalesse noted the downloads included snapshot jars. The binaries have been updated with intended geotools and geowebcache jars.
Security Considerations
This release addresses security vulnerabilities and is considered an important upgrade for production systems.
- GEOS-11557 CVE-2024-45748 High
See project security policy for more information on how security vulnerabilities are managed.
Release notes
New Feature:
- GEOS-11352 REST service for URL checks
Improvement:
- GEOS-11399 Use Catalog streaming API in LayerGroupPage
- GEOS-11427 metadata: “fix all” to support changing config repeatable field
- GEOS-11463 WMS vector dimension validation should query only one feature and only for dimension attribute
- GEOS-11502 Permit resize on user/group/role palette textbox to allow for extra long role names
- GEOS-11503 Update mongo schemaless DWITHIN to support non-point geometry
- GEOS-11557 CVE-2024-45748 High
- GEOS-11588 GWC disk quota, check JDBC connection pool validation query
Bug:
- GEOS-10811 GeoServer 2.22.0 WPS error while clipping raster with GeoJSON input
- GEOS-11071 GeoJSON PPIO goes NPE while decoding a GeoJSON geometry
- GEOS-11107 Open search for EO community module: packaging missing gt-cql-json-xx.x.jar
- GEOS-11453 Failure to look-up default value of custom dimensions on vector layers
- GEOS-11484 DirectRasterRenderer is not respecting advancedProjectionHandling and continuosMapWrapping format_options
- GEOS-11493 Azure blob store may not get environment parameters from property file
- GEOS-11497 WPS execution fails with GeoJSON input
- GEOS-11504 ResourceAccessManagerWrapper misses some delegating methods
- GEOS-11505 OWS Monitor only handles WFS 1.0 requests
- GEOS-11513 WMTS/GetDomainValues - Returned values are not sorted
- GEOS-11514 Fix parsing WPS geometry geojson inputs
- GEOS-11524 csw: default queryables mapping not generated
- GEOS-11543 Unable to use propertyName to filter properties in a GetFeature request when service is not set
- GEOS-11553 SLD Style: Empty SE Rotationelement throws RuntimeException (QGIS generated SLD)
- GEOS-11556 NullPointerException when GWC disk quota monitoring is disabled
- GEOS-11559 The customized attributes editor is prone to setting the wrong attribute source
Task:
- GEOS-11470 Upgrade the version of Mongo driver for schemaless plugin from 4.0.6 to 4.11.2
- GEOS-11506 Upgrade Spring version from 5.3.37 to 5.3.39 and Spring security from 5.8.13 to 5.8.14
- GEOS-11508 Update OSHI from 6.4.10 to 6.6.3
- GEOS-11533 Update org.apache.commons.vfs2 to 2.9.0
- GEOS-11574 Bump org.eclipse.jetty:jetty-server from 9.4.52.v20230823 to 9.4.55.v20240627 in /src
- GEOS-11587 Update map fish-print-v2 2.3.2
For the complete list see 2.25.4 release notes.
Community Updates
Community module development:
- GEOS-11517 Using various OGC APIs results in service enabled check related WARN logs
- GEOS-11518 DGGS JDBC store SQL encoder should not force the timezone to CET
- GEOS-11519 Make DGGS rHealPix tests run again
- GEOS-11560 OGC API modules lack cql2-json in assembly
- GEOS-11563 Allow configuring a DGGS resolution offset on a layer basis
- GEOS-11565 Allow configuring the minimum and maximum DGGS resolution for a layer
- GEOS-11579 DGGS modules prevent GeoServer startup if JEP is not installed
Community modules are shared as source code to encourage collaboration. If a topic being explored is of interest to you, please contact the module developer to offer assistance.
About GeoServer 2.25 Series
Additional information on GeoServer 2.25 series:
- GeoServer 2.25 User Manual
- GeoServer 2024 Roadmap Plannings
- Raster Attribute Table extension
- Individual contributor clarification
Release notes: ( 2.25.4 | 2.25.3 | 2.25.2 | 2.25.1 | 2.25.0 | 2.25-RC )
Tutorials
- Using Logical Operators in GeoServer Filters
- Exploring CQL/ECQL Filtering in GeoServer
- Using Spatial Operators in GeoServer Filters
- Using Value Comparison Operators in GeoServer Filters
- Using Binary Comparison Operators in GeoServer Filters
- Utilizing the Demo Section in Geoserver
- How to Implement Basic Security in Geoserver
- How to create Tile Layers with GeoServer
- How to style layers using GeoServer and QGIS
- How to Publish a GeoTIFF file in GeoServer