<div dir="ltr"><div>Hi Anita,</div><div><br></div><div>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</div><div>
parameters['INPUT'] as an input.</div><div><br></div><div>Thanks for your help<br></div><div><br></div><div>Kind Regards</div><div><br></div><div>Andrew<br></div><div><br></div><div>import processing<br>from qgis.PyQt.QtCore import QCoreApplication<br>from qgis.core import QgsProcessingAlgorithm, QgsProcessing, QgsProcessingParameterFeatureSink,QgsProcessingParameterFeatureSource<br><br>class testAlg(QgsProcessingAlgorithm):<br>    OUTPUT = 'OUTPUT'<br>    INPUT = 'INPUT'<br><br>    def tr(self, text):<br>        return QCoreApplication.translate('testalg', text)<br><br>    def createInstance(self):<br>        return type(self)()<br><br>    def group(self):<br>        return <a href="http://self.tr">self.tr</a>('Test')<br><br>    def groupId(self):<br>        return 'test'<br><br>    def __init__(self):<br>        super().__init__()<br><br>    def initAlgorithm(self, config=None):<br>        <br>        self.addParameter(<br>            QgsProcessingParameterFeatureSource(<br>                self.INPUT,<br>                <a href="http://self.tr">self.tr</a>('Input layer'),<br>                [QgsProcessing.TypeVectorAnyGeometry]<br>            )<br>        )<br>        <br>        self.addParameter(<br>            QgsProcessingParameterFeatureSink(<br>                self.OUTPUT, <br>                <a href="http://self.tr">self.tr</a>('Output'), <br>                QgsProcessing.TypeVectorPolygon<br>            )<br>        )<br><br>    def name(self):<br>        return 'testalg'<br><br>    def displayName(self):<br>        return <a href="http://self.tr">self.tr</a>('Test Algorithm')<br><br>    def processAlgorithm(self, parameters, context, feedback):<br><br>        output = processing.run("native:buffer", {<br>            'INPUT': parameters['INPUT'],<br>            'DISTANCE': 10,<br>            'SEGMENTS': 5,<br>            'END_CAP_STYLE': 0,<br>            'JOIN_STYLE': 0,<br>            'MITER_LIMIT': 2,<br>            'DISSOLVE': False,<br>            'OUTPUT': parameters['OUTPUT']<br>        }, context=context, feedback=feedback)['OUTPUT']<br>        <br>        return {self.OUTPUT: output}<br></div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Aug 9, 2018 at 5:29 PM Andrew C <<a href="mailto:andrew.cutts@gmail.com">andrew.cutts@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi Anita,</div><div><br></div><div>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?</div><div><br></div><div>I found this <a href="https://gis.stackexchange.com/questions/282773/writing-a-python-processing-script-with-qgis-3-0" target="_blank">https://gis.stackexchange.com/questions/282773/writing-a-python-processing-script-with-qgis-3-0</a></div><div>which lead me to this</div><div><a href="https://github.com/qgis/QGIS/blob/master/doc/porting_processing.dox" target="_blank">https://github.com/qgis/QGIS/blob/master/doc/porting_processing.dox</a></div><div><br></div><div>Which is recommending "Best practice in 3.x Processing algorithms is to use "feature sinks" instead of vector layer outputs"</div><div><br></div><div><br></div><div>Kind Regards</div><div>Andrew<br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Aug 9, 2018 at 2:23 PM Anita Graser <<a href="mailto:anitagraser@gmx.at" target="_blank">anitagraser@gmx.at</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-size:small">Hi Andrew,</div><br><div class="gmail_quote"><div dir="ltr">On Thu, Aug 9, 2018 at 2:57 PM Andrew C <<a href="mailto:andrew.cutts@gmail.com" target="_blank">andrew.cutts@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>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.<br></div></div></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">I think you'd need an output layer instead of a feature sink. Have a look at <a href="https://github.com/qgis/QGIS/blob/master/python/plugins/processing/algs/qgis/PostGISExecuteAndLoadSQL.py" target="_blank">https://github.com/qgis/QGIS/blob/master/python/plugins/processing/algs/qgis/PostGISExecuteAndLoadSQL.py</a></div><br>self.addOutput(QgsProcessingOutputVectorLayer(<br><div class="gmail_default" style="font-size:small;display:inline">   </div>self.OUTPUT,<br><div class="gmail_default" style="font-size:small;display:inline">   </div><a href="http://self.tr" target="_blank">self.tr</a>("Output layer"),<br><div class="gmail_default" style="font-size:small;display:inline">   </div>QgsProcessing.TypeVectorAnyGeometry))<div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">Regards,</div></div><div class="gmail_default" style="font-size:small">Anita</div><div class="gmail_default" style="font-size:small"><br></div></div></div>
</blockquote></div>
</blockquote></div>