Skip to content

Commit 73dc9c4

Browse files
committedSep 14, 2013
`processing]improved doc of example algorithm
1 parent 99857bc commit 73dc9c4

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed
 

‎python/plugins/processing/algs/SaveSelectedFeatures.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class SaveSelectedFeatures(GeoAlgorithm):
3636
'''This is an example algorithm that takes a vector layer and creates
3737
a new one just with just those features of the input layer that are
3838
selected.
39-
It is meant to be used as an example of how to create your own SEXTANTE
39+
It is meant to be used as an example of how to create your own Processing
4040
algorithms and explain methods and variables used to do it.
41-
An algorithm like this will be available in all SEXTANTE elements, and
41+
An algorithm like this will be available in all Processing elements, and
4242
there is not need for additional work.
4343
4444
All geoprocessingalgorithms should extend the GeoAlgorithm class'''
@@ -51,7 +51,11 @@ class SaveSelectedFeatures(GeoAlgorithm):
5151

5252
def defineCharacteristics(self):
5353
'''Here we define the inputs and output of the algorithm, along
54-
with some other properties'''
54+
with some other properties.
55+
This will give the algorithm its semantics, and allow to use it in the modeler.
56+
As a rule of thumb, do not produce anything not declared here.
57+
It will work fine in the toolbox, but it will not work in the modeler.
58+
If that's what you intend, then set self.showInModeler = False'''
5559

5660
#the name that the user will see in the toolbox
5761
self.name = "Save selected features"
@@ -70,8 +74,13 @@ def processAlgorithm(self, progress):
7074
'''Here is where the processing itself takes place'''
7175

7276
#the first thing to do is retrieve the values of the parameters
73-
#entered by the user
77+
#entered by the user.
78+
#The getParameterValue will return the value with its corresponding type,
79+
#strings in the case of inputs and outputs
7480
inputFilename = self.getParameterValue(self.INPUT_LAYER)
81+
82+
#The output. It will get the value of the destinatation file entered by the user.
83+
#If the user select "Save to temporary file", when we arrive here it will already have an asigned value, which will be
7584
output = self.getOutputFromName(self.OUTPUT_LAYER)
7685

7786
#input layers values are always a string with its location.
@@ -83,16 +92,24 @@ def processAlgorithm(self, progress):
8392

8493
#First we create the output layer.
8594
#To do so, we call the getVectorWriter method in the Output object.
86-
#That will give as a ProcessingVectorWriter, that we can later use to add features.
95+
#That will give us a VectorWriter, that we can later use to add features.
8796
provider = vectorLayer.dataProvider()
88-
writer = output.getVectorWriter( provider.fields(), provider.geometryType(), vectorLayer.crs() )
97+
writer = output.getVectorWriter( provider.fields(),
98+
provider.geometryType(),
99+
#this is the layer crs. By default all resulting layers are
100+
#assumed to be in the same crs are the inputs, and will be loaded
101+
#with this assumptions when executed from the toolbox.
102+
#The self.crs variable has to be canged in case this is not true,
103+
#or in case there are no input layer from which the output crs can be infered
104+
vectorLayer.crs() )
89105

90106
#Now we take the selected features and add them to the output layer
91107
features = vector.features(vectorLayer)
92108
total = len(features)
93109
i = 0
94-
for feat in features:
110+
for i, feat in enumerate(features):
95111
writer.addFeature(feat)
112+
#we use the progress object to communicate with the user
96113
progress.setPercentage(100 * i / float(total))
97114
i += 1
98115
del writer

0 commit comments

Comments
 (0)