@@ -36,9 +36,9 @@ class SaveSelectedFeatures(GeoAlgorithm):
36
36
'''This is an example algorithm that takes a vector layer and creates
37
37
a new one just with just those features of the input layer that are
38
38
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
40
40
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
42
42
there is not need for additional work.
43
43
44
44
All geoprocessingalgorithms should extend the GeoAlgorithm class'''
@@ -51,7 +51,11 @@ class SaveSelectedFeatures(GeoAlgorithm):
51
51
52
52
def defineCharacteristics (self ):
53
53
'''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'''
55
59
56
60
#the name that the user will see in the toolbox
57
61
self .name = "Save selected features"
@@ -70,8 +74,13 @@ def processAlgorithm(self, progress):
70
74
'''Here is where the processing itself takes place'''
71
75
72
76
#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
74
80
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
75
84
output = self .getOutputFromName (self .OUTPUT_LAYER )
76
85
77
86
#input layers values are always a string with its location.
@@ -83,16 +92,24 @@ def processAlgorithm(self, progress):
83
92
84
93
#First we create the output layer.
85
94
#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.
87
96
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 () )
89
105
90
106
#Now we take the selected features and add them to the output layer
91
107
features = vector .features (vectorLayer )
92
108
total = len (features )
93
109
i = 0
94
- for feat in features :
110
+ for i , feat in enumerate ( features ) :
95
111
writer .addFeature (feat )
112
+ #we use the progress object to communicate with the user
96
113
progress .setPercentage (100 * i / float (total ))
97
114
i += 1
98
115
del writer
0 commit comments