[GIS] Quickly make server side spatial filters for WFS layers based on other layer’s selected geometries in QGIS

expressionpyqgisqgisspatial-querywfs

In QGIS 2.12.1, I have a WFS layer which is huge so I don't want my users to load it entirely but only parts of it based on intersections with geometries.

Today, I'm using the query builder and execute filters like :

intersects( $geometry , geom_from_wkt( 'Polygon ((29085.2677694238955155 5701652.79282033536583185, 23973.40924414427718148 5683250.10212932899594307, 39820.17067251115804538 5683250.10212932899594307, 37264.24140987131977454 5698074.49185263924300671, 37264.24140987131977454 5698074.49185263924300671, 29085.2677694238955155 5701652.79282033536583185))'))

I need to use the plugin getWKT and copy/paste the WKT which is not practical for beginners. Also this plugin doesn't handle multiple selected features.

So I'd like to know if it is possible to easily do the same job with geometries from selected features of another layer?

EDIT :
Inspired by this article I've created a user defined function :

from qgis.utils import qgsfunction, iface
from qgis.core import QGis
@qgsfunction(0, "Python")
def getSelectedFeaturesInWKT(values, feature, parent):
  """
    Returns the wkt of selected features
  """
  layer = iface.activeLayer()
  features = layer.selectedFeatures()
  geom = features[0].geometry()
  return geom.exportToWkt()

This function return the first (to test then I want to get all features) selected feature in wkt.

Then I load this script and modify my query :

intersects( $geometry , geom_from_wkt(getSelectedFeaturesInWKT()))

The expression is valid but the layer created has the URI of the WFS server without the GML filter.

I have tested getSelectedFeaturesInWKT() alone and it works.

Best Answer

The only way is to use a WFS filter (querying the server side with a filter in the URL) to lower the time to receive the filtered data.

Unfortunately, you will have to wait for this feature. It's currently in the master version but not in the 2.12.1 last release according to this issue.

Related Question