Skip to content

Commit 4fe067a

Browse files
ghtmttnyalldawson
authored andcommitted
script template with basic function
1 parent 46d59b7 commit 4fe067a

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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(parameters, self.INPUT, context)
110+
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT,
111+
context, source.fields(), source.wkbType(), source.sourceCrs())
112+
113+
# Compute the number of steps to display within the progress bar and
114+
# get features from source
115+
total = 100.0 / source.featureCount() if source.featureCount() else 0
116+
features = source.getFeatures()
117+
118+
for current, feature in enumerate(features):
119+
# Stop the algorithm if cancel button has been clicked
120+
if feedback.isCanceled():
121+
break
122+
123+
# Add a feature in the sink
124+
sink.addFeature(feature, QgsFeatureSink.FastInsert)
125+
126+
# Update the progress bar
127+
feedback.setProgress(int(current * total))
128+
129+
# Return the results of the algorithm. In this case our only result is
130+
# the feature sink which contains the processed features, but some
131+
# algorithms may return multiple feature sinks, calculated numeric
132+
# statistics, etc. These should all be included in the returned
133+
# dictionary, with keys matching the feature corresponding parameter
134+
# or output names.
135+
return {self.OUTPUT: dest_id}

0 commit comments

Comments
 (0)