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.

  • 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 '19 at 22:17