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.

  • why don't you use the extent? You can set it with BBOX and probably update string BBOX with qgis extent parameter Dec 14 '15 at 19:20
  • 1
    My main layer contains a lot of points with a lot of attributes. If I only want points intersecting two or more geometries far from each other I'd need a bbox big enough to have my main layer slow to load. Dec 15 '15 at 8:55

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.

  • I'm already using WFS filter. This feature is enabled since 2.8 at least. This is not the problem. Dec 14 '15 at 12:49

If you want it to be user friendly, why not stick to the build in 'Only request features overlapping the current view extent'. Screen shot from QGIS 2.12.1.

enter image description here

  • It is not enough. Current view extent can be very large but users only need to intersect 3 cities for example. Dec 14 '15 at 10:58

Here is an option:

  1. Have the user select the various "intersections with geometries" features that they want to see the WFS features within or intersected by
  2. Create temp layer or memory layer from intersecting WFS features with features selected in step 1 and write attributes and geometry to temp layer to show, see example code below:

    # reference to WFS layer
    uri = "http://localhost/geoserver/wfs? srsname=EPSG:4326&typename=workspaceName:layerName&version=1.0.0&request=GetFeature&service=WFS"
    selectedFeatures = []
    WFSlayer = QgsVectorLayer(uri, "layerName", "WFS")
    features1 = layer1.selectedFeatures() # this layer is the layer the user or code selects in the map
    for WFSfeature in WFSlayer.getFeatures():
      for f in features1:
        if WFSfeature.geometry().intersects(f.geometry()):
    # create temp layer, eg use LineString geometry
    tempLayer = QgsVectorLayer("LineString?crs=epsg:4326", "tempLayer", "memory")
    temp_data = tempLayer.dataProvider()
    attr = WFSlayer.dataProvider().fields().toList()
    # zoom to temp layer...etc
  • I don't have QGIS to test now but from what I understand the WFS layer is fully loaded and the intersection is made client side isn't it? Dec 20 '15 at 18:01
  • I confirm the query is made client side so the question is not answered but your script might help other people so I award you the bounty. Thanks. Dec 21 '15 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.