Aller au contenu | Aller au menu | Aller à la recherche

Spatial operators in QGIS expression

To improve the implementation of the OGC WFS standard in QGIS-Server, I needed to evaluate filters including spatial operators. The Open Geospatial Consortium has defined in the Filter Encoding and Simple Feature Specification for SQL standard a list of spatial operators, who are :

  • Equals,
  • Disjoint,
  • Touches,
  • Within,
  • Overlaps,
  • Crosses,
  • Intersects,
  • et Contains.

To which must be added BBOX who corresponds to an intersection (Intersects) with the emprise (BoundingBox) of the geometry passed as a parameter.

All these operators require 2 arguments which are 2 geometries that can be:

  • that of the geographic object (Feature) for which the expression is evaluated
  • a GML2 geometry in the case of Filter Encoding standard, a WKT geometry in the expression case

After coding a QGIS-Server specific implemetation et getting feedback from QGIS contributors, I undertook the implementation of these operators in the QGIS expression système. These developments are currently on my QGIS github repository but already fonctional. And here are some examples.

QGIS Expression Window QGIS Window Expression constructor. In the list of available operators, you will find in the geometry category the new spatial operators $geometry, geomFromWKT, geomFromGML2, bbox, disjoint, etc.

To achieve these examples, I chose a dataset form Montpellier OpenData :

  • Sous-quartiers (Sub-districts), this data represents the administrative sub-division of the city of Montpellier.

In the layer metadata, we find the following extent :

  • xMin,yMin 765145.88,6274561.22
  • xMax,yMax 776031.67,6284189.52

We will first use it to select sub-districts on the diagonal xMin,yMin xMax,yMax, which gives the expression :

  • intersects( $geometry, geomFromWKT( 'LINESTRING(765145.88 6274561.22, 776031.67 6284189.52)' ) )

QGIS Expression test intersects linestring

QGIS Expression select intersects linestring

If you want to select all elements, you can write:

  • bbox( $geometry, geomFromWKT( 'LINESTRING(765145.88 6274561.22, 776031.67 6284189.52)' ) )

But it is also possible to use these expressions in set of style rules, with such expressions:

  • intersects( $geometry, geomFromWKT( 'POLYGON' ) )
  • NOT intersects( $geometry, geomFromWKT( 'POLYGON' ) )

QGIS Expression spatial style rules

Color is thus associated with each of these expressions which can easily styled Northwest and Southeast sub-districts of Montpellier.

QGIS Expression spatial style rules rendering

You can also add the selection on the diagonal with a priority higher than the other:

  • intersects( $geometry, geomFromWKT( 'LINESTRING(765145.88 6274561.22, 776031.67 6284189.52)' ) )

And get this: QGIS Expression spatial style rules rendering

This kind of thing was necessary to the WFS in QGIS-Server but now we can imagine extending these spatial operators by adding transformer like:

  • exportToWKT
  • buffer
  • centroid
  • convexhull
  • distance
  • simplify

To create content or complexify spatial queries.