2

I am writing a Processing python script based on the template provided here.

I am creating lines (destined to the 'OUTPUT') and I would like to clip them according to a polygon entered as the 'INPUT' parameter). I can't figure out how to call the Algorithm. For what it's worth, this doesn't work:
QgsProcessingAlgorithm('qgis:clip').run({'INPUT': self.OUTPUT, 'OVERLAY': self.INPUT})

I have tried several ways but couldn't get far as I get the error: qgis._core.QgsProcessingAlgorithm represents a C++ abstract class and cannot be instantiated

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
YeO
  • 609
  • 3
  • 13
  • 1
    'Abstract class' means you cannot instantiate it like QgsProcessingAlgorithm(..) or variable = QgsProcessingAlgorithm(). Other classes inherit from it just like class NewProcessingAlg(QgsProcessingAlgorithm) – Kadir Şahbaz Apr 10 '18 at 10:28
  • processing.run() See https://gis.stackexchange.com/a/262059/2856 and to get the input parameter syntax easily, run the alg manually and have a look at the processing history. – user2856 Apr 10 '18 at 10:55
  • @Luke ok, processing.run() seems the way to go (it felt counter-intuitive as we have QgsProcessingAlgorithm, QgsProcessingAlgRunner and many other classes). Now, any idea as to how I pipe this to my algorithm ? – YeO Apr 10 '18 at 12:58

2 Answers2

3

You can execute 'qgis:clip' in python as follows.

alg='qgis:clip'
params = {"INPUT": inputLayer,"OVERLAY": clipLayer,"OUTPUT":clippedLayer}
processing.run(alg, params)
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Asad Abbas
  • 1,634
  • 9
  • 26
  • I had figured that part from a previous suggestion, now what I am trying to do is have qgis:clip take the current output of my algorithm as input and then let the final output be my algorithm output. Not sure this is clear but basically I want to call this inside my algorithm and let it return the final output... still stuck here. – YeO Apr 19 '18 at 12:16
  • please share the code snippet of your algorithm I may be able to help you in matter. – Asad Abbas Apr 20 '18 at 05:29
  • It's based on this template, I have just changed the logic to actually do something but I'm using the QgsProcessingFeatureSink just as in the template. – YeO Apr 20 '18 at 11:15
  • You can try something like this
    clip_area = processing.runalg("qgis:clip", input_layer, clip_layer,clipped_layer) fin_area = processing.runalg("qgis:dissolve", clip_area['OUTPUT'], "true", None, processed_layer)
    – Asad Abbas Apr 25 '18 at 09:50
0

I have found the following to work:

temp = QgsVectorLayer('Line', 'temp', 'memory')
# do some stuff with temp
clipped_lines = processing.run('native:clip', {
        'INPUT': temp,
        'OVERLAY': parameters['INPUT'],
        'OUTPUT': 'memory:ClippedLines',
    }, context=context, feedback=feedback)['OUTPUT']
YeO
  • 609
  • 3
  • 13