3

I want to calculate the area of the polygon and add it to the attribute table as another column. I used this script:

from qgis import processing
    
inlayDir = iface.activeLayer()
P3_boundary = r'C:\Users\CMCA\OneDrive - Boskalis\Documents\Trial PyQGIS\outTemp.gpkg'
    
dissolved = processing.runAndLoadResults("native:dissolve",
                                 {'INPUT': inlayDir, 
                                  'FIELD':[],
                                  'OUTPUT': 'TEMPORARY_OUTPUT'})
                                  
union = processing.runAndLoadResults( "qgis:union",{
                            'INPUT' : P3_boundary, 
                            'OUTPUT' : 'TEMPORARY_OUTPUT', 
                            'OVERLAY' : dissolved['OUTPUT'], 
                            'OVERLAY_FIELDS_PREFIX' : '' })
                            
clip = processing.runAndLoadResults("native:clip",
                                { 'INPUT':union['OUTPUT'],
                                'OVERLAY' : dissolved['OUTPUT'],
                                'OUTPUT' : 'TEMPORARY_OUTPUT',
                                })
                             
provider = clip.dataProvider()

field = QgsField("area3", QVariant.Double)

provider.addAttributes([field])

clip.updateFields()
    
index = provider.fieldNameIndex('area3')
    
for feat in clip.getFeatures():
    provider.changeAttributeValues({feat.id(): {index: feat.geometry().area()}})

result :

enter image description here

The error I get:

Traceback (most recent call last):
File "C:\PROGRA~1\QGIS32~1.0\apps\Python39\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 27, in <module>
AttributeError: 'dict' object has no attribute 'dataProvider'
1

2 Answers 2

7

Since processing.runAndLoadResults gives you a dictionary, you get the error. You have to use processing.run to get a layer and add it to QGIS manually using QgsProject.instance().addMapLayer(clip).

....

clip = processing.run("native:clip",
                      {'INPUT'  : union['OUTPUT'],
                       'OVERLAY': dissolved['OUTPUT'],
                       'OUTPUT' : 'TEMPORARY_OUTPUT',
                      })
                             
provider = clip.dataProvider()

....

QgsProject.instance().addMapLayer(clip)
5

You get that error because layer has no method fieldNameIndex, but provider has. Use the following script:

layer = iface.activeLayer()
provider = layer.dataProvider()

field = QgsField("area3", QVariant.Double)
provider.addAttributes([field])
layer.updateFields()

index = provider.fieldNameIndex('area3')

for feat in layer.getFeatures():
    provider.changeAttributeValues({feat.id(): {index: feat.geometry().area()}})

Note: This is the answer to the question before it was changed.

2
  • Hi , I cannot paste my whole script here in the comments section so i edited my post , can you please take a look , cause i still have errors. Thank you very much
    – PyCher
    Jan 29 at 6:13
  • it works if I use the script as individual script but if i use my output like i described in my statement above , It gives me error
    – PyCher
    Jan 29 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.