[Qgis-developer] Accessing Features from API

ahmed at elegantdesigns.ca ahmed at elegantdesigns.ca
Mon Aug 24 13:26:47 EDT 2009

The output of the attached code is: 

Layer: "test_shp_layer"
field: "test_shp_field"
v_provider: "<qgis.core.QgsFeature object at 0xab50dac>"
rows: []

Even if I add a dummy string to the list, it will only return that value.
For some reason, I cannot seem to fetch the features from the layer and
load them into the list. I was wondering, what did my fragile brain do
wrong? and how can i fix it?



  # loads a field into a python list
  def field_to_list( self, layer_name, field_name ):
    v_layer = self.get_vector_layer_by_name( layer_name ) )
    v_provider = v_layer.dataProvider()
    id = v_provider.fieldNameIndex( field_name )
    rows = []
    feat = QgsFeature()
    while v_provider.nextFeature( feat ):
      data = feat.attributeMap()[ id ]
      row = [ data ]
      rows.append( row )
    QMessageBox.warning( self, "window title", str( ",".join( rows ) ) )
    return rows
  def get_vector_layer_by_name( self, myName ):
    layermap = QgsMapLayerRegistry.instance().mapLayers()
    for name, layer in layermap.iteritems():
      if layer.type() == QgsMapLayer.VectorLayer and layer.name() ==
        if layer.isValid():
          return layer
          return None

More information about the Qgis-developer mailing list