[Qgis-user] Field comparision

Jorge Tornero - Listas jtorlistas at gmail.com
Tue Nov 26 23:34:12 PST 2013


Hi list,

I'm developing a Python plugin for QGIS and one of the features of this 
plugin is that the user can choose to create a new layer to store the 
points generated by the plugin or store them in a existing point layer.

Every point has an associated information (attributes or fields), and in 
the case of new layer creation, it's easy to create the new layer with 
those attributes (say, for instance, name, number and temperature).

But in the case of using a existing layer to store the points, I want 
first to check if the destination layer already has those fields; in 
that case i'll use them directly but if the fields doesn't exist in the 
layer, I'll give the user the chance to add it to the layer. This is 
pretty simple, too.

But my approach to check for existing fields, consists in creating some 
QgsFields with the desired specs and check for equality against the 
layer fields.

The problem is that the operator == of the class QgsField doesn't work 
properly, because it looks like when the fields are not of the string 
type, the comparision is bad. Seems that default construction of 
QgsFields of the type integer or double is different than the default 
construction of these fields when creating a layer with:

QgsVectorLayer(
"Point?field=id:integer&field=survey:string(20)&field=station:string(5)&field=stnnum:integer&field=stnlat:double&field=stnlon:double&field=stnobs:string(255)&index=yes",
               "newLayer",
               "memory")

In the latter case, the field stnnum has:

name=stnnum
type=2
typeName=integer
length=10
precision=0

while if you create the ssame field with:

stnnumField=QgsField(name='stnnum',type=2,typeName='integer')

name=stnnum
type=2
typeName=integer
length=0
precision=0

And that's why == doesn't work as expected (for me, of course)

So maybe this approach is not very good.

Any ideas to override this behaviour or just check for existing fields?


Best regards,

Jorge Tornero







More information about the Qgis-user mailing list