[QGIS-Developer] Filtering QgsFeature

Francisco Camello franciscocamellon at gmail.com
Fri Aug 7 05:41:52 PDT 2020


Hi all,

     I'm developing a processing algorithm that filters a QgsVectorLayer by
expressions provided via QgsFeatureRequest, till this point it's ok. But, I
must give to the users an option to run the algorithm only on selected
features. In this case, the type of object received change from
QgsVectorLayer to a list of QgsFeature, therefore, and I'm stuck here
because I can't make any requests through QgsFeatureRequest() or selection
using the selectByExpression() method, to filtering QgsFeature. Follow the
code below.

def checkedFeatures(self, rules, layerList, onlySelected, returnIterator=True):
        """
        Select features by conditional rules stored in the rules JSON file.
        :param (dict) rules: dictionary from conditional rules;
        :param (QgsVectorLayer) layerList: list from all loaded layers;
        :param (bool) onlySelected: boolean.
        """
        failedDict = {}
        for ruleName in rules:
            failedList = []
            for lyr in layerList:
                loadedLyrName = lyr.name()
                if loadedLyrName in rules[ruleName]:
                    allRules = rules[ruleName][loadedLyrName]['allRules']
                    for rule in allRules:
                        request = QgsFeatureRequest().setFilterExpression(rule)
                        if onlySelected:
                            selected = lyr.selectedFeatures()
                            # this FOR block below receives a
QgsFeature list from lyr.selectedFeatures()
                            # and so I can't make any requests through
QgsFeatureRequest()
                            # or selection using the selectByExpression() method
                            for feat in selected: # I cant do the selection here
                                feat.selectByExpression(rule)
                                failedList+=[feat for feat in
lyr.selectedFeatures()]
                        else:
                            for feat in lyr.getFeatures(request):
                                failedList.append(feat)
            failedDict[ruleName] = failedList
        return failedDict

The expressions provided do verifications in one attribute or in a set of
attributes. It looks like to this:
{ "Incorrect attribute": { "hid_island_a": { "allRules":
[ "\"geometriaaproximada\" not in  (1,2)" ] } },
  "Ortographic error": { " hid_island_a ": { "allRules": [ "(regexp_match (
"\"name" , '^ ' ) or regexp_match ( ""name"" , '  ' ) or regexp_match (
""name"" , ' $' ) or regexp_match ( ""name"" , '^[a-z]' ))"]}}
}
May I do this by QgsFeatureFilterProvider.filterFeatures()? Or there is
another way to filter QgsFeatures?

Best regards,
__________________________________________________________________________________
Francisco Alves Camello Neto
GIS Consultant
+55 021 96987 9945  +55 021 98122 4645
franciscocamellon at gmail.com
* Even recycled paper takes energy to make. Do you really need to print
this e-mail?*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20200807/7b4a541a/attachment-0001.html>


More information about the QGIS-Developer mailing list