4

I have two processing algorithms in my plugin code and I want to use the outputs of them into a new algorithm, like this:

buff = processing.runandload('qgis:fixeddistancebuffer', layers[self.pic], 10 + (10 * (int(self.dlg.lcdNumber_1.value())) / 100), 5, True, None) 
# buff and buff2 are fine, they will add to canvas, no errors
buff2 = processing.runandload('qgis:fixeddistancebuffer', layers[self.pic], 30 + (30 * (int(self.dlg.lcdNumber_2.value())) / 100), 5, True, None)

and I want to use the two to a new algorithm like:

processing.runandload('qgis:difference', buff2['OUTPUT'], buff['OUTPUT'], None)

but it will give AttributeError: FixedDistanceBuffer instance has no attribute '__getitem__'
what can i do?

1 Answer 1

4

You have two issues in your code.

1) It cannot be used buff2['OUTPUT'] or buff['OUTPUT'] as parameters with 'runandload' processing method. These parameters should be used with 'runalg' method.

2) In 'qgis:difference' is necessary one additional parameter: IGNORE_INVALID [ParameterBoolean].

Similar to your example, I tried out following code:

import processing

point1 = processing.getObjectFromName('point1')
point2 = processing.getObjectFromName('point2')

buff = processing.runalg('qgis:fixeddistancebuffer', 
                         point1, 
                         1000, 
                         5, 
                         True, 
                         None) 

buff2 = processing.runalg('qgis:fixeddistancebuffer', 
                          point2, 
                          2000, 
                          5, 
                          True, 
                          None)

processing.runandload('qgis:difference', 
                      buff2['OUTPUT'], 
                      buff['OUTPUT'], 
                      False, 
                      None)

After running it at Python Console of QGIS; with these point layers:

enter image description here

it was produced its respective buffers (not loaded at Map Canvas) and its difference; as it can be observed at following image:

enter image description here

However, if you also need to load buffers following code works adequately:

import processing

point1 = processing.getObjectFromName('point1')
point2 = processing.getObjectFromName('point2')

buff = processing.runalg('qgis:fixeddistancebuffer', 
                          point1, 
                          1000, 
                          5, 
                          True, 
                          None) 

copy_buff = QgsVectorLayer(buff['OUTPUT'],
                           'buff',
                           'ogr')

QgsMapLayerRegistry.instance().addMapLayer(copy_buff)

buff2 = processing.runalg('qgis:fixeddistancebuffer', 
                           point2, 
                           2000, 
                           5, 
                           True, 
                           None)

copy_buff2 = QgsVectorLayer(buff2['OUTPUT'],
                            'buff2',
                            'ogr')

QgsMapLayerRegistry.instance().addMapLayer(copy_buff2)


processing.runandload('qgis:difference', 
                      buff2['OUTPUT'], 
                      buff['OUTPUT'], 
                      False, 
                      None)

as it can be observed at following image:

enter image description here

Editing Note:

Following code works well too by using 'runandload' processing method for buffers.

import processing

point1 = processing.getObjectFromName('point1')
point2 = processing.getObjectFromName('point2')

buff = processing.runandload('qgis:fixeddistancebuffer', 
                              point1, 
                              1000, 
                              10, 
                              True, 
                              None) 

buff = processing.getObjectFromName('Buffer')

buff2 = processing.runandload('qgis:fixeddistancebuffer', 
                              point2, 
                              2000, 
                              10, 
                              True, 
                              None)

buff2 = processing.getObjectFromName('Buffer')

processing.runandload('qgis:difference', 
                      buff2, 
                      buff, 
                      False, 
                      None)
0

Your Answer

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

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