[Qgis-user] Filtering MSSQL Layers with python

Stefan Löhr loehr at topographics.de
Fri Aug 15 07:20:12 PDT 2014


Hello QGis Users,

in my QGIS Plugin I am filtering layers with this Python code:


LegInt = iface.legendInterface()
layers = LegInt.layers()
TestLayer=None
for layer in layers:
    layerType = layer.type()
    if layer.name() == "my_layer_name":
        s_lay=layer
        break

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.

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 testdata was 
exactly the same in Postgis and MSSQL, so I know that the filterexpression 
is correct.

Could this be a bug?
Can somebody reproduce this?


Have a nice day,
Stefan




More information about the Qgis-user mailing list