|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from PyQt5.QtCore import QCoreApplication |
| 4 | +from qgis.core import (QgsProcessing, |
| 5 | + QgsFeatureSink, |
| 6 | + QgsProcessingAlgorithm, |
| 7 | + QgsProcessingParameterFeatureSource, |
| 8 | + QgsProcessingParameterFeatureSink) |
| 9 | + |
| 10 | + |
| 11 | +class ExampleProcessingAlgorithm(QgsProcessingAlgorithm): |
| 12 | + """ |
| 13 | + This is an example algorithm that takes a vector layer and |
| 14 | + creates a new identical one. |
| 15 | +
|
| 16 | + It is meant to be used as an example of how to create your own |
| 17 | + algorithms and explain methods and variables used to do it. An |
| 18 | + algorithm like this will be available in all elements, and there |
| 19 | + is not need for additional work. |
| 20 | +
|
| 21 | + All Processing algorithms should extend the QgsProcessingAlgorithm |
| 22 | + class. |
| 23 | + """ |
| 24 | + |
| 25 | + # Constants used to refer to parameters and outputs. They will be |
| 26 | + # used when calling the algorithm from another algorithm, or when |
| 27 | + # calling from the QGIS console. |
| 28 | + |
| 29 | + INPUT = 'INPUT' |
| 30 | + OUTPUT = 'OUTPUT' |
| 31 | + |
| 32 | + def tr(self, string): |
| 33 | + """ |
| 34 | + Returns a translatable string with the self.tr() function. |
| 35 | + """ |
| 36 | + return QCoreApplication.translate('Processing', string) |
| 37 | + |
| 38 | + def createInstance(self): |
| 39 | + return ExampleProcessingAlgorithm() |
| 40 | + |
| 41 | + def name(self): |
| 42 | + """ |
| 43 | + Returns the algorithm name, used for identifying the algorithm. This |
| 44 | + string should be fixed for the algorithm, and must not be localised. |
| 45 | + The name should be unique within each provider. Names should contain |
| 46 | + lowercase alphanumeric characters only and no spaces or other |
| 47 | + formatting characters. |
| 48 | + """ |
| 49 | + return 'duplicatevectorlayer' |
| 50 | + |
| 51 | + def displayName(self): |
| 52 | + """ |
| 53 | + Returns the translated algorithm name, which should be used for any |
| 54 | + user-visible display of the algorithm name. |
| 55 | + """ |
| 56 | + return self.tr('Duplicate a vector layer') |
| 57 | + |
| 58 | + def group(self): |
| 59 | + """ |
| 60 | + Returns the name of the group this algorithm belongs to. This string |
| 61 | + should be localised. |
| 62 | + """ |
| 63 | + return self.tr('Example scripts') |
| 64 | + |
| 65 | + def groupId(self): |
| 66 | + """ |
| 67 | + Returns the unique ID of the group this algorithm belongs to. This |
| 68 | + string should be fixed for the algorithm, and must not be localised. |
| 69 | + The group id should be unique within each provider. Group id should |
| 70 | + contain lowercase alphanumeric characters only and no spaces or other |
| 71 | + formatting characters. |
| 72 | + """ |
| 73 | + return 'examplescripts' |
| 74 | + |
| 75 | + def initAlgorithm(self, config=None): |
| 76 | + """ |
| 77 | + Here we define the inputs and output of the algorithm, along |
| 78 | + with some other properties. |
| 79 | + """ |
| 80 | + |
| 81 | + # We add the input vector features source. It can have any kind of |
| 82 | + # geometry. |
| 83 | + self.addParameter( |
| 84 | + QgsProcessingParameterFeatureSource( |
| 85 | + self.INPUT, |
| 86 | + self.tr('Input layer'), |
| 87 | + [QgsProcessing.TypeVectorAnyGeometry] |
| 88 | + ) |
| 89 | + ) |
| 90 | + |
| 91 | + # We add a feature sink in which to store our processed features (this |
| 92 | + # usually takes the form of a newly created vector layer when the |
| 93 | + # algorithm is run in QGIS). |
| 94 | + self.addParameter( |
| 95 | + QgsProcessingParameterFeatureSink( |
| 96 | + self.OUTPUT, |
| 97 | + self.tr('Output layer') |
| 98 | + ) |
| 99 | + ) |
| 100 | + |
| 101 | + def processAlgorithm(self, parameters, context, feedback): |
| 102 | + """ |
| 103 | + Here is where the processing itself takes place. |
| 104 | + """ |
| 105 | + |
| 106 | + # Retrieve the feature source and sink. The 'dest_id' variable is used |
| 107 | + # to uniquely identify the feature sink, and must be included in the |
| 108 | + # dictionary returned by the processAlgorithm function. |
| 109 | + source = self.parameterAsSource( |
| 110 | + parameters, |
| 111 | + self.INPUT, |
| 112 | + context |
| 113 | + ) |
| 114 | + (sink, dest_id) = self.parameterAsSink( |
| 115 | + parameters, |
| 116 | + self.OUTPUT, |
| 117 | + context, |
| 118 | + source.fields(), |
| 119 | + source.wkbType(), |
| 120 | + source.sourceCrs() |
| 121 | + ) |
| 122 | + |
| 123 | + # Compute the number of steps to display within the progress bar and |
| 124 | + # get features from source |
| 125 | + total = 100.0 / source.featureCount() if source.featureCount() else 0 |
| 126 | + features = source.getFeatures() |
| 127 | + |
| 128 | + for current, feature in enumerate(features): |
| 129 | + # Stop the algorithm if cancel button has been clicked |
| 130 | + if feedback.isCanceled(): |
| 131 | + break |
| 132 | + |
| 133 | + # Add a feature in the sink |
| 134 | + sink.addFeature(feature, QgsFeatureSink.FastInsert) |
| 135 | + |
| 136 | + # Update the progress bar |
| 137 | + feedback.setProgress(int(current * total)) |
| 138 | + |
| 139 | + # Return the results of the algorithm. In this case our only result is |
| 140 | + # the feature sink which contains the processed features, but some |
| 141 | + # algorithms may return multiple feature sinks, calculated numeric |
| 142 | + # statistics, etc. These should all be included in the returned |
| 143 | + # dictionary, with keys matching the feature corresponding parameter |
| 144 | + # or output names. |
| 145 | + return {self.OUTPUT: dest_id} |
0 commit comments