1

I'm trying to select features using a string comparison within my script using a QgsFeatureRequest() and getFeatures()

roadsRequest = QgsFeatureRequest()
roadsExpression = str(roadsIDField[0]) + ' LIKE ' +  str(featIDUnique)
feedback.pushInfo(roadsExpression)

For reference then, the roadsExpression looks like the below or I have tried using an " = " operator too:

identifier LIKE 534553B5-9A71-4DE2-B8C2-56B0B31D5EB2

I've then set the filter expression of the QgsFeatureRequest() object and passed attempted to get features as below and getFeatures() should return a QgsFeatureIterator:

roadsRequest.setFilterExpression(roadsExpression)
roadsFeatures = roadsSource.getFeatures(roadsRequest)

Now, I've brought roadsSource in as a parameterAsSource() to provide a QgsProcessingFeatureSource() object and I therefore get a valid QgsFeatureIterator object in roadsFeatures but this is empty.

However, I am unable to edit a QgsProcessingFeatureSource object therefore I tried bringing roadsSource in using parameterAsVectorLayer() to provide a QgsVectorLayer() object that I can edit. However when I use getFeatures(roadsRequest) on this vector layer object, I get an invalid feature iterator.

I think I am doing something wrong with how I've built my request using the strings but I don't know what as everything is compiling and running without any errors, I'm just not getting the results out that I expect.

closed as unclear what you're asking by Matthias Kuhn, Jochen Schwarze, Erik, whyzar, xunilk Feb 19 at 17:25

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You need to wrap the string in your expression in single quotes: "{0} LIKE '{1}'".format(str(roadsIDField[0]), str(featIDUnique)) – Nathan W Feb 17 at 22:17