In my QGIS Plugin I am filtering layers with this Python code:
FeatureIterator = layer.getFeatures( QgsFeatureRequest().setFilterExpression ("Fieldname='SomeValue'") )
for feature in FeatureIterator :
attrs = feature.attributes()
print "Feature ID: " + str(feature.id()) + " " + str(attrs)
This works for most layer-types (shapefile, PostGIS, personal GDB,etc). But when I tried it with MSSQL Spatial layers, no results are returned. The iterator is empty.
Edit: It works if I use the same filter clause (Fieldname='SomeValue') as a Feature subset at Layer-Properties. Then the layer shows only the intended subset.
I work with QGIS 2.2 and 2.4. MSSQL Server is 2008R2. My test data was exactly the same in PostGIS and MSSQL, so I know that the filter expression is correct.
Could this be a bug?
Edit: I was able to track the SQL queries that are executed on the Database. Instead of the filter expression that I intended, QGIS adds an Intersect clause and an ID filter to the query:
select .... from ... where
[shape].STIntersects([geometry]::STGeomFromText('POLYGON((0.00000000 0.00000000, 0.00000000 0.00000000, 0.00000000 0.00000000, 0.00000000 0.00000000, 0.00000000 0.00000000))',25832)) = 1 and [objectid] = 30681189080760422
The Coordinates of the intersect-rectangle are always 0 and the objectid-value doesn't exist.
[ ]
.