18

Is there a way to use the function "Select by Attribute" in QGIS using a python command? In my plugin the user should enter a value via a GUI and this value should be used in a function which selects all features which have this attribute. The column name is fixed in the code, the function should only search for the correct value.

In my current solution the function connects QGIS to a PostgreSQL database and runs an SQL statement. This creates a table from the result and the table is visualised as Shapefile in QGIS.

In principal it would be enough to highlight the features and not to create a new Shapefile of the selection. Using the "Select by Attribute" function would also skip the unnecessary database connection.

Is there a way to use the function "Select by Attribute" in python so that the features are highlighted? Using the function in QGIS all features that doesn't match the query are temporary blanked-out that would be ok too.

1
  • 2
    These answers need a qgis 3 update. Commented Apr 8, 2019 at 12:13

3 Answers 3

11

Starting from QGIS 2.2 this is supported in a very natural way. It can be done via the QGIS expression engine using the QgsFeatureRequest.setFilterExpression( unicode ) method.

# The important part: get the feature iterator with an expression
it = l.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"Counties" = \'Norwich\'' ) )
# Set the selection
l.setSelectedFeatures( [ f.id() for f in it ] )

The best thing about it: if you have a recent QGIS version (2.10 and later), this will be filtered directly in the database so much more performant than other solutions while still being very readable.

9

Yes. You can get all the attributes through the python bindings and implement extra filtering in your own plugin. See this PyQGIS Coobook excerpt for the rundown and some examples. You would then just exclude any nonmatching results from the returned dictionary.

As for the visualisation, you'll likely still have to create another layer, as select() does not have fitting arguments. You can use a memory layer to avoid having to create physical files (more on that in the cookbook).

edit:

Actually, you can use selectedFeaturesIds() with setSelectedFeatures(ids) to change the selection to the subset you created. Quoting the implementation directly:

/** Get a copy of the user-selected features */  
QList<QgsFeature> selectedFeatures();

/** Return reference to identifiers of selected features */
const QSet<qint64> &selectedFeaturesIds() const;

/** Change selection to the new set of features */
void setSelectedFeatures(const QSet<qint64> &ids);
6
  • What a pity! I thought it was possible in QGIS, because it is also possible in ArcGIS using arcpy. Nevertheless thanks for your answer!
    – userHH
    Commented Aug 30, 2012 at 12:46
  • Maybe there is a way; I just don't know it. Give other users of the site some time — maybe someone does. Commented Aug 30, 2012 at 13:07
  • I think it must be possible. In QGIS all features are visualized that matches with the selection, all other features are temporary blanked out.
    – userHH
    Commented Aug 30, 2012 at 13:36
  • It is, I found it. QGIS does this in the core, but it looks like it exposes enough of the interface to adjust the selection properly. Commented Aug 30, 2012 at 14:05
  • Can you send me a link?
    – userHH
    Commented Aug 30, 2012 at 14:42
2

sextante.runalg('qgis:selectbylocation', "bufferarea", "hospitals", 1)

http://qgissextante.blogspot.in/2013/01/using-selection-algorithms.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.