[Qgis-user] QGIS 3 Processing question

Frank Broniewski hallo at frankbroniewski.com
Fri Jun 29 00:35:09 PDT 2018


Hi Nyall,

thanks a ton for your response. I am well know for making things tricky for myself 😃
Anyway, my testing-algorithm-script is still not working. It runs through, but I do not get the expected result loaded into the layer tree. I suppose it runs through - the  output from the 'qgis:pointsalonglines' is a QgsVectorLayer - but after finishing I get nuthink back.

Here's my complete script: You'll need a temporary polygon layer in a projected CRS (I use EPSG:31466) for it to work nicely. Copy & Paste it into a new script window and run it. I simply don't find the reason why the point result layer isn't loaded into my layer tree ...


from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsApplication,
                       QgsProcessing,
                       QgsFeatureSink,
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterNumber,
                       QgsProcessingParameterFeatureSink)

import processing


class PolygonCenterline(QgsProcessingAlgorithm):

    INPUT = 'INPUT'
    DISTANCE = 'DISTANCE'
    OUTPUT = 'OUTPUT'
    
    def tr(self, text):
        return QCoreApplication.translate('Processing', text)
    
    def createInstance(self):
        return PolygonCenterline()

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

    def groupId(self):
        return 'cartography'

    def name(self):
        return 'polygoncenterline'

    def displayName(self):
        return self.tr('Calculate a polygon centerline')

    def initAlgorithm(self, config=None):
        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.INPUT,
                self.tr('Vector Polygon Layer'),
                [QgsProcessing.TypeVectorPolygon]
            )
        )

        self.addParameter(
            QgsProcessingParameterNumber(
                self.DISTANCE,
                self.tr('Point distance value'),
                type=QgsProcessingParameterNumber.Double,
                minValue=10.0
            )
        )

        self.addParameter(
            QgsProcessingParameterFeatureSink(
                self.OUTPUT,
                self.tr('Center line')
            )
        )

    def processAlgorithm(self, parameters, context, feedback):
        # qgis:pointsalonglines
        params = {
            'INPUT': parameters[self.INPUT], 
            'DISTANCE': parameters[self.DISTANCE],
            'START_OFFSET': 0,
            'END_OFFSET': 0,
            'OUTPUT': 'memory:'
        }
        points = processing.run(
            'qgis:pointsalonglines', 
            params, context=context, feedback=feedback
        )['OUTPUT']

        return {self.OUTPUT: points}




-----Ursprüngliche Nachricht-----
Von: Nyall Dawson <nyall.dawson at gmail.com> 
Gesendet: Freitag, 29. Juni 2018 01:04
An: Frank Broniewski <hallo at frankbroniewski.com>
Cc: qgis-user <qgis-user at lists.osgeo.org>
Betreff: Re: [Qgis-user] QGIS 3 Processing question

On Fri, 29 Jun 2018 at 07:14, Frank Broniewski <hallo at frankbroniewski.com> wrote:
>
>     def processAlgorithm(self, parameters, context, feedback):


You're making this tricky for yourself! Cut out everything in processAlgorithm related to self.INPUT, and just pass the parameter value direct to the child algorithm to handle:

>         params = {
>
>             'INPUT': parameters[self.INPUT],
>
>             'DISTANCE': pt_value,
>
>             'START_OFFSET': 0,
>
>             'END_OFFSET': 0,
>
>             'OUTPUT': 'memory:'
>
>         }


Nyall
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5488 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20180629/6803284e/attachment.bin>


More information about the Qgis-user mailing list