Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
add comment

3 Answers 3

up vote 5 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).

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);
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
show 1 more comment

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

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

share|improve this answer
add comment

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( [ f.id() for f in it ] )
share|improve this answer
add comment

Your Answer

 
discard

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.