Hi Anita,

I worked it out. Here is a very simple replication of a buffer for
scripting, in case anyone else needs it. I don't know if this is best
practice but it works for a simple test. I didn't realise I could parse
parameters['INPUT'] as an input.

Thanks for your help

Kind Regards

Andrew

import processing
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import QgsProcessingAlgorithm, QgsProcessing,
QgsProcessingParameterFeatureSink,QgsProcessingParameterFeatureSource

class testAlg(QgsProcessingAlgorithm):
    OUTPUT = 'OUTPUT'
    INPUT = 'INPUT'

    def tr(self, text):
        return QCoreApplication.translate('testalg', text)

    def createInstance(self):
        return type(self)()

    def group(self):
        return self.tr('Test')

    def groupId(self):
        return 'test'

    def __init__(self):
        super().__init__()

    def initAlgorithm(self, config=None):

        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.INPUT,
                self.tr('Input layer'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )

        self.addParameter(
            QgsProcessingParameterFeatureSink(
                self.OUTPUT,
                self.tr('Output'),
                QgsProcessing.TypeVectorPolygon
            )
        )

    def name(self):
        return 'testalg'

    def displayName(self):
        return self.tr('Test Algorithm')

    def processAlgorithm(self, parameters, context, feedback):

        output = processing.run("native:buffer", {
            'INPUT': parameters['INPUT'],
            'DISTANCE': 10,
            'SEGMENTS': 5,
            'END_CAP_STYLE': 0,
            'JOIN_STYLE': 0,
            'MITER_LIMIT': 2,
            'DISSOLVE': False,
            'OUTPUT': parameters['OUTPUT']
        }, context=context, feedback=feedback)['OUTPUT']

        return {self.OUTPUT: output}

On Thu, Aug 9, 2018 at 5:29 PM Andrew C <andrew.cu...@gmail.com> wrote:

> Hi Anita,
>
> Thanks very much for this. I am still lost though. If I replace the sink
> and add your code I don't get an output in the dialog box - it needs to be
> a parameter for that to happen right?
>
> I found this
> https://gis.stackexchange.com/questions/282773/writing-a-python-processing-script-with-qgis-3-0
> which lead me to this
> https://github.com/qgis/QGIS/blob/master/doc/porting_processing.dox
>
> Which is recommending "Best practice in 3.x Processing algorithms is to
> use "feature sinks" instead of vector layer outputs"
>
>
> Kind Regards
> Andrew
>
>
>
> On Thu, Aug 9, 2018 at 2:23 PM Anita Graser <anitagra...@gmx.at> wrote:
>
>> Hi Andrew,
>>
>> On Thu, Aug 9, 2018 at 2:57 PM Andrew C <andrew.cu...@gmail.com> wrote:
>>
>>> I am not clear on how QgsFeatureSinkis working, perhaps that is why I am
>>> not getting the buffered layer? Normally in a script I could try printing
>>> variables I cannot see where these are printed either when I tested.
>>>
>>
>> I think you'd need an output layer instead of a feature sink. Have a look
>> at
>> https://github.com/qgis/QGIS/blob/master/python/plugins/processing/algs/qgis/PostGISExecuteAndLoadSQL.py
>>
>> self.addOutput(QgsProcessingOutputVectorLayer(
>>
>> self.OUTPUT,
>>
>> self.tr("Output layer"),
>>
>> QgsProcessing.TypeVectorAnyGeometry))
>>
>> Regards,
>> Anita
>>
>>
_______________________________________________
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to