QGIS 3 Processing question

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

QGIS 3 Processing question

Frank Broniewski-6

Hi all,

 

I am getting my feet wet with the new QGIS 3 Processing script syntax. In my script I want to use some other processing scripts from the toolbox. I think I got most things right, but I do not get a result back. For testing I just try to use ‘qgis:pointsalonglines’ on a temporary polygon layer and return the points as a result. Here’s what I have so far:

 

def initAlgorithm(self, config=None):

        self.addParameter(

            QgsProcessingParameterFeatureSource(

                self.INPUT,

                self.tr("Vector Polygon Layer"),

                [QgsProcessing.TypeVectorPolygon]

            )

        )

        […]

        self.addParameter(

            QgsProcessingParameterFeatureSink(

                self.OUTPUT,

                self.tr('Center line'),

                QgsProcessing.TypeVectorAnyGeometry

            )

        )

 

 

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

        source = self.parameterAsSource(parameters, self.INPUT, context)

        # turn QgsProcessingParameterFeature - the source -  into a

        # QgsVectorLayer apparently self.parameterAsVectorLayer() does

        # not work on temporary layers so this step is required to use

        # the input layer in processing algorithms

        source_vl = source.materialize(QgsFeatureRequest())

 

        pt_value = […]

 

        # qgis:pointsalonglines

        params = {

            'INPUT': source_vl,

            'DISTANCE': pt_value,

            'START_OFFSET': 0,

            'END_OFFSET': 0,

            'OUTPUT': 'memory:'

        }

 

        points = processing.run(

            'qgis:pointsalonglines',

            params, context=context, feedback=feedback

        )['OUTPUT']

        return {self.OUTPUT: points}

 

 

running the script gives the following output in the processing window (including some debug info not included above):

 

Eingabeparameter:

{ 'DISTANCE' : 10, 'INPUT' : 'Polygon?crs=EPSG:31466&uid={e1a2d6cf-0cac-46f5-b1c8-ee208c497d22}', 'OUTPUT' : 'memory:' }

 

<class 'qgis._core.QgsProcessingFeatureSource'>

Ergebnisse: {'OUTPUT': 'output_953bb179_3f12_4312_89e4_5ffa13e8336b'}

<class 'qgis._core.QgsVectorLayer'>

Ausführung nach 0.03 Sekunden abgeschlossen

Ergebisse:

{'OUTPUT': <PyQt5.QtCore.QObject object at 0x0000023FC2CEBC18>}

 

Lade Ergebnis Layer

Algorithmus 'None' beendet

 

The last output line does not look right to me, any idea what I am doing wrong here?

 

Many thanks,

Frank

 

 

Dipl. Geogr. Frank Broniewski

Waldhölzbacher Str. 51

66679 Losheim am See

06872 509 068 4

0176 611 26 9 2 6

www.frankbroniewski.com

 


_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

smime.p7s (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: QGIS 3 Processing question

Nyall Dawson
On Fri, 29 Jun 2018 at 07:14, Frank Broniewski
<[hidden email]> 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
_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: QGIS 3 Processing question

Frank Broniewski-6
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 <[hidden email]>
Gesendet: Freitag, 29. Juni 2018 01:04
An: Frank Broniewski <[hidden email]>
Cc: qgis-user <[hidden email]>
Betreff: Re: [Qgis-user] QGIS 3 Processing question

On Fri, 29 Jun 2018 at 07:14, Frank Broniewski <[hidden email]> 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

_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

smime.p7s (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: QGIS 3 Processing question

Jean-Baptiste Desbas
Hi,

I had the same issue. I had to loop over the output vector layer (child alg) to feed the sink (main alg). Maybe is not the simpliest/shortest/fastest, but it works.
Something like that :

o = processing.run(
            'qgis:pointsalonglines',
            params, context=context, feedback=feedback
        )['OUTPUT']


 (sink, dest_id) = self.parameterAsSink(
                    parameters,
                    self.OUTPUT,
                    context,
                    o.fields,
                    o.wkbType(),
                    o.sourceCrs()
)

for f in o.getFeatures()
    sink.addFeature(f)


return {'OUTPUT':dest_id}


2018-06-29 9:35 GMT+02:00 Frank Broniewski <[hidden email]>:
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 <[hidden email]>
Gesendet: Freitag, 29. Juni 2018 01:04
An: Frank Broniewski <[hidden email]>
Cc: qgis-user <[hidden email]>
Betreff: Re: [Qgis-user] QGIS 3 Processing question

On Fri, 29 Jun 2018 at 07:14, Frank Broniewski <[hidden email]> 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

_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user


_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: QGIS 3 Processing question

Rudi von Staden
In reply to this post by Frank Broniewski-6
Hi Frank,

On Fri, 29 Jun 2018 at 09:35, Frank Broniewski <[hidden email]> wrote:
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.


I had a similar problem. I think it's because the template defines the output as a feature sink, so you assume you have to use the same. I think if you change your definition from:

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

to: 
self.addParameter(
            QgsProcessingParameterVectorDestination(
                self.OUTPUT,
                self.tr('Center line')
            )
        )

and then in processAlgorithm you need to add output as a layer you can work with:

output = self.parameterAsOutputLayer(parameters,self.OUTPUT,context)

Finally, in your `params` dict, change the value for OUTPUT from `memory:` to `output`.

The new syntax is more complicated than it used to be, and it takes some time to wrap your head around it. I do find that it gets easier once you've got a couple of scripts under your belt.

Rudi
 

_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: QGIS 3 Processing question

Frank Broniewski-6

Hi Rudi,

 

thanks a lot for your answer, I will definitely try that out. And once I’ve done it I will post my insights for future reference. I tried the QgsProcessingParameterVectorDestination already, but

 

> Finally, in your `params` dict, change the value for OUTPUT from `memory:` to `output`

 

This is really new to me. Where did you find that? I really searched the docs up and down and gis.stackexchange as well, but never came across an output output for the output 😃

 

Many thanks,

Frank

 

Von: Rudi von Staden <[hidden email]>
Gesendet: Sonntag, 1. Juli 2018 15:56
An: Frank Broniewski <[hidden email]>
Cc: qgis-user <[hidden email]>
Betreff: Re: [Qgis-user] QGIS 3 Processing question

 

Hi Frank,

 

On Fri, 29 Jun 2018 at 09:35, Frank Broniewski <[hidden email]> wrote:

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.

 

I had a similar problem. I think it's because the template defines the output as a feature sink, so you assume you have to use the same. I think if you change your definition from:

 

self.addParameter(

            QgsProcessingParameterFeatureSink(

                self.OUTPUT,

                self.tr('Center line')

            )

        )

 

to: 

self.addParameter(

            QgsProcessingParameterVectorDestination(

                self.OUTPUT,

                self.tr('Center line')

            )

        )

 

and then in processAlgorithm you need to add output as a layer you can work with:

 

output = self.parameterAsOutputLayer(parameters,self.OUTPUT,context)

 

Finally, in your `params` dict, change the value for OUTPUT from `memory:` to `output`.

 

The new syntax is more complicated than it used to be, and it takes some time to wrap your head around it. I do find that it gets easier once you've got a couple of scripts under your belt.

 

Rudi

 


_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

smime.p7s (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: QGIS 3 Processing question

Frank Broniewski-6
In reply to this post by Jean-Baptiste Desbas

Thanks for your answer, I feared that this might be an option after all. I will try Rudi’s answer first and then yours. I will definitely post my insights here so everyone can profit

 

Frank

 

Von: Jean-Baptiste Desbas <[hidden email]>
Gesendet: Freitag, 29. Juni 2018 16:03
An: Frank Broniewski <[hidden email]>
Cc: Nyall Dawson <[hidden email]>; qgis-user <[hidden email]>
Betreff: Re: [Qgis-user] QGIS 3 Processing question

 

Hi,

 

I had the same issue. I had to loop over the output vector layer (child alg) to feed the sink (main alg). Maybe is not the simpliest/shortest/fastest, but it works.

Something like that :

 

o = processing.run(
            'qgis:pointsalonglines',
            params, context=context, feedback=feedback
        )['OUTPUT']

 

 (sink, dest_id) = self.parameterAsSink(

                    parameters,
                    self.OUTPUT,
                    context,
                    o.fields,
                    o.wkbType(),
                    o.sourceCrs()
)

 

for f in o.getFeatures()

    sink.addFeature(f)

 

 

return {'OUTPUT':dest_id}

 

 

2018-06-29 9:35 GMT+02:00 Frank Broniewski <[hidden email]>:

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 <[hidden email]>
Gesendet: Freitag, 29. Juni 2018 01:04
An: Frank Broniewski <[hidden email]>
Cc: qgis-user <[hidden email]>
Betreff: Re: [Qgis-user] QGIS 3 Processing question


On Fri, 29 Jun 2018 at 07:14, Frank Broniewski <[hidden email]> 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


_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

 


_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

smime.p7s (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: QGIS 3 Processing question

Nyall Dawson
In reply to this post by Frank Broniewski-6
On Fri, 29 Jun 2018 at 17:35, Frank Broniewski
<[hidden email]> wrote:

>     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}
>

Try:

     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']

        # store result layer in context - this prevents it being
deleted and cleaned up
        # when processAlgorithm finishes
        context.temporaryLayerStore().addMapLayer(points)

         # return the layer ID in the results dictionary - processing
will automatically retrieve the corresponding
         # layer from the context when required
         return {self.OUTPUT: points.id()}


Nyall
_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: QGIS 3 Processing question

Frank Broniewski-6
Rudi, Nyall,

I tried both your tips on how to solve this puzzle, but both didn't work unfortunately. Nyall's tip with the temporary layer store just runs through nicely, but nothing gets added to the layer pane after the script finished. Rudi's tip changes mostly the vector layer type from memory to a physical layer, but does not work as well. I even tried to return the result from the processing alg directly with no success:

return processing.run(
            'qgis:pointsalonglines',
            params, context=context, feedback=feedback
        )

=> {'OUTPUT': <PyQt5.QtCore.QObject object at 0x000002024290CC18>}

Jean-Baptiste's tip on copying the features over to a new feature sink does work though.
I can however use the output from qgis:pointsalonglines without any problem in a second processing algorithm without needing to copy the features over to another feature sink.

It seems that there's some logic going on that prevents returning other processing results. Maybe it has to do with the ownership that's mentioned a few times in the docs [1]?



[1] https://qgis.org/pyqgis/master/core/Processing/QgsProcessingAlgorithm.html?highlight=parameterassink#qgis.core.QgsProcessingAlgorithm.addParameter



-----Ursprüngliche Nachricht-----
Von: Nyall Dawson <[hidden email]>
Gesendet: Dienstag, 3. Juli 2018 01:37
An: Frank Broniewski <[hidden email]>
Cc: qgis-user <[hidden email]>
Betreff: Re: [Qgis-user] QGIS 3 Processing question

On Fri, 29 Jun 2018 at 17:35, Frank Broniewski <[hidden email]> wrote:

>     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}
>
Try:

     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']

        # store result layer in context - this prevents it being deleted and cleaned up
        # when processAlgorithm finishes
        context.temporaryLayerStore().addMapLayer(points)

         # return the layer ID in the results dictionary - processing will automatically retrieve the corresponding
         # layer from the context when required
         return {self.OUTPUT: points.id()}


Nyall

_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

smime.p7s (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: QGIS 3 Processing question

Nyall Dawson
In reply to this post by Nyall Dawson
> Nyall's tip with the temporary layer store just runs through nicely, but nothing gets added to the layer pane after the script finished.

Sorry - missed something here. Because you're directly using the
output from the child algorithm as your algorithm's final output, you
should also pass the OUTPUT parameter value direct to the child
algorithm. So in

>
>      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:'
>          }

 'OUTPUT': 'memory:'

should be

 'OUTPUT': parameters[self.OUTPUT]

Nyall
_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: QGIS 3 Processing question

Frank Broniewski-6
Hi Nyall,

this works like a charm! Here's what I needed it for: Create polygon center lines in QGIS [1]. My script for processing is still quite rough around the edges and everywhere else and needs some testing, but I am getting results, thanks to you!

Many thanks,
Frank


[1] GIT https://github.com/frankbroniewski/polygoncenterline/blob/master/PolygonCenterline.py

-----Ursprüngliche Nachricht-----
Von: Nyall Dawson <[hidden email]>
Gesendet: Dienstag, 3. Juli 2018 11:55
An: Frank Broniewski <[hidden email]>
Cc: qgis-user <[hidden email]>
Betreff: Re: [Qgis-user] QGIS 3 Processing question

> Nyall's tip with the temporary layer store just runs through nicely, but nothing gets added to the layer pane after the script finished.

Sorry - missed something here. Because you're directly using the output from the child algorithm as your algorithm's final output, you should also pass the OUTPUT parameter value direct to the child algorithm. So in

>
>      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:'
>          }
 'OUTPUT': 'memory:'

should be

 'OUTPUT': parameters[self.OUTPUT]

Nyall

_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

smime.p7s (7K) Download Attachment