[Qgis-developer] Load attributes to a numpy array

Carson Farmer carson.farmer at gmail.com
Thu Aug 13 11:16:04 EDT 2009

Hi dassouki,
> I was wondering if anyone can help me load an attribute column into a numpy
> array http://docs.scipy.org/doc/numpy/reference/arrays.html . I’m not
> necessary looking for the full code, rather than a pseudo code.  For
> example, a layer that has columns A, B, C, I'd like to load all the values
> in column C into the array.
The only way to do this is to parse the layer's attribute table.
Something like the following:

id = provider.fieldNameIndex( 'field_name' )
rows = []
while provider.nextFeature( feat ):
    data = feat.attributeMap()[ id ]
    do something with row data *
    row = [ data ]
    rows.append( row )
array = numpy.array( rows )

    * here data will be a QVariant,
    so you'll have to do the appropriate
    manipulation to get the data type
    you want, if you want all the columns
    you could probably unzip the dict of
    attributes returned by attributeMap()
    and simply set row equal to that,
    just be careful, as dicts aren't stored
    in any particular order. In this case
    the keys are the columns ids



More information about the Qgis-developer mailing list