"Select by Attribute" in QGIS using python?
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.
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);
sextante.runalg('qgis:selectbylocation', "bufferarea", "hospitals", 1)
http://qgissextante.blogspot.in/2013/01/using-selection-algorithms.html