Try...
# substitute 'self' with 'qgis.utils.iface' when run from Python console
# 'self.iface = iface' would usually precede this command in your class
layers = self.iface.legendInterface().layers()
for layer in layers:
layerType = layer.type()
if layerType == QgsMapLayer.VectorLayer:
# do some stuff here
Culled from consolidatethread.py from QConsolidate plugin.
Description of QgsLegendInterface object returned from legendInterface().
Edit: added info on 'self' above.
You can use the solutions given in Using processing algorithms from the console:
But if you look at what's in the module (version 2.2):
import processing
dir(processing)
no more getfeatures()
or getFeatures()
You can control this with a little function adapted from Script de Python para filtrar por patrón de texto los métodos de Clases en PyQGIS de José Guerrero
import re
def get_patt(keyword, L):
return [item for item in dir(L) if re.search(keyword,item)]
get_patt("getfeatures",processing)
[]
get_patt("getFeatures",processing)
[]
but there is:
get_patt("features",processing)
['features']
So the command is:
features = processing.features(layer)
But for me, it is easiest with:
without processing:
layer = qgis.utils.iface.activeLayer()
with processing:
layer = processing.getObject("name_of_the_layer")
All the features:
for feat in layer.getFeatures():
geom= feat.geometry()
attr =feat.attributes()
Selected features only:
for feat in layer.selectedFeatures():
geom= feat.geometry()
attr =feat.attributes()
and, for example, an "universal solution":
if layer.selectedFeatureCount():
geom = [feat.geometry() for feat in layer.selectedFeatures()]
else:
geom = [feat.geometry() for feat in layer.getFeatures()]
Best Answer
Check that the layer is valid. If the layer, or the path is not valid qgis will no raise an error, it will return a
QgsVectorLayer
object where you can call the methods but with mostly no-op.It will be probably a bad path. Also try to open (manually) the file in qgis to check if there is any error with the shp.