[Qgis-developer] Accessing Features from API

motta.luiz at gmail.com motta.luiz at gmail.com
Tue Aug 25 07:12:06 EDT 2009


You need define which field you want get by call attributeMap.
...

    id = v_provider.fieldNameIndex( field_name )
    v_provider.select([id]) # Define field you get attribute
    rows = []
    feat = QgsFeature()
    while v_provider.nextFeature( feat ):
      data = feat.attributeMap()[ id ]
      row = [ data ]
      rows.append( row )

...


PS: I made some sources for study case, if you want i can send for you.

ahmed at elegantdesigns.ca escreveu:
> 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?
>
> cheers,
> dassouki
>
> # CODE:
>
>   # 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() ==
> myName:
>         if layer.isValid():
>           return layer
>         else:
>           return None
>
>
>
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>   



More information about the Qgis-developer mailing list