TransWikia.com

QGIS How to find QVariant of existing attribute

Geographic Information Systems Asked on August 31, 2021

I want to add attributes, without features, that exists in one layer but not in the other using the code below. I see that I am not obtaining the "QVariant" from anywhere and just assumes it will be Double. Is there a way to obtain the correct information from the source layer? Or is there a better way of doing this?

diff = list(set(ly1.fields().names()).difference(ly2.fields().names()))
for att in diff:
    res = ly2.dataProvider().addAttributes([QgsField(att, QVariant.Double)])
    ly2.updateFields()

I have looked into using:

type = ly1.fields().field(item).typeName()

This however returns, e.g., int8. But unsure how to use it.

One Answer

I'm not sure why you want to use QVariant. You can simply copy the field directly e.g

diff = list(set(ly1.fields().names()).difference(ly2.fields().names()))
for att in diff:
    res = ly2.dataProvider().addAttributes([
        QgsField(ly1.fields().field(att)) # Get the field from the name and make a copy you add
    ])
    ly2.updateFields()

Correct answer by ThomasG77 on August 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP