[Qgis-developer] addAttributes case sensitive?
wonder.sk at gmail.com
Sun Jun 1 12:03:21 EDT 2008
On Fri, May 30, 2008 at 8:24 AM, cfarmer <cfarmer at uvic.ca> wrote:
> Hello list,
> Does anyone know how addAttributes on a QgsFeature defines which field to
> populate? As far as I can tell, I should have to specify an index, and the
> attribute that I am trying to add, however, if I have two fields that have
> the same name with different cases (i.e. one field says "Ycoor" and
> another says 'YCOOR', even if I specify the index of 'YCOOR', values are
> written to 'Ycoor'.
addAtribute() function basically just adds new item to dictionary of
attributes connected with feature. To get its value actually saved,
you would need to save it in provider. But provider saves only
attributes which are present in the data source. So if you want to
create a new column for attributes first you would need to call
provider's addAttributes() function (if provider supports it) and
specify attribute name and type. Later when you add features with this
new attribute it should save it correctly.
> So in short, why does field name matter in this case? Or am I doing
> something completely wrong:
> fieldList #pre-existing field list obtained from QgsProvider.fields()
> field = QgsField("YCOOR", QVariant.Double, "double", 24, 16, "Polygon area")
> index1 = len(fieldList.keys())
> fieldList[index1] = field
Here you actually don't add the new field - you're just modifying the
field list. You would have to call addAttributes() of the provider.
The type which is passed is provider dependent.
> #then later on, when setting the attributes of an output feature...
> #Set new attributes equal to attributes of currently existing feature...
> #Now add on the new attribute...
> outFeat.addAttribute(index1, QVariant(attr1))
To really save this change, call changeAttributeValues() of provider.
> Note: The currently existing attributes have a field named 'Ycoor'. The
> new field being added is called 'YCOOR'. Using the above code, 'Ycoor' is
> (re)populated, and 'YCOOR' is left blank?
I using field names with difference just in case is generally not a
good idea, since some providers might do difference and some not.
More information about the Qgis-developer