Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 enought 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.

Regards from Hamburg,

share|improve this question
up vote 7 down vote accepted

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).


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);
share|improve this answer
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 Aug 30 '12 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. – lynxlynxlynx Aug 30 '12 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 Aug 30 '12 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. – lynxlynxlynx Aug 30 '12 at 14:05
Can you send me a link? – userHH Aug 30 '12 at 14:42

Current master / upcoming QGIS 2.2 supports this 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( [ for f in it ] )
share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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