I am making a plugin that loads a WFS layer and lets the user filter out the information they need and create a layer based on that filtering. The new layer is created from the original layer based on expressions that are created from the users filtering. This prosess can be very slow as the WFS layer is rather large. I would therfore like to know if there is any faster or betther ways of doing this.
Here is an example of my code:
expr_string = "\"kommune\"=301 AND \"funksjon\"='kultur'" #example of an expr_string expr = QgsExpression(expr_string) it = vlayer.getFeatures( QgsFeatureRequest( expr ) ) ids = [i.id() for i in it] vlayer.setSelectedFeatures( ids ) selectedFeatures = vlayer.selectedFeatures() tempLayer = QgsVectorLayer("Point?crs=epsg:4326", "newLayer", "memory") QgsMapLayerRegistry.instance().addMapLayer(tempLayer) temp_data = tempLayer.dataProvider() attr = vlayer.dataProvider().fields().toList() temp_data.addAttributes(attr) tempLayer.updateFields() temp_data.addFeatures(selectedFeatures)
This code is based on an anwer from this post: Quickly make server side spatial filters for WFS layers based on other layer's selected geometries in QGIS