pktools  2.6.3
Processing Kernel for remote sensing data
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
pktools.ExampleAlgorithm.ExampleAlgorithm Class Reference
Inheritance diagram for pktools.ExampleAlgorithm.ExampleAlgorithm:
Inheritance graph
[legend]
Collaboration diagram for pktools.ExampleAlgorithm.ExampleAlgorithm:
Collaboration graph
[legend]

Public Member Functions

def defineCharacteristics
 
def processAlgorithm
 

Public Attributes

 name
 
 group
 

Static Public Attributes

string OUTPUT_LAYER = "OUTPUT_LAYER"
 
string INPUT_LAYER = "INPUT_LAYER"
 

Detailed Description

This is an example algorithm that takes a vector layer and creates
a new one just with just those features of the input layer that are
selected.

It is meant to be used as an example of how to create your own
algorithms and explain methods and variables used to do it. An algorithm
like this will be available in all elements, and there is not need
for additional work.

All processing algorithms should extend the GeoAlgorithm class.

Definition at line 41 of file ExampleAlgorithm.py.

Member Function Documentation

def pktools.ExampleAlgorithm.ExampleAlgorithm.defineCharacteristics (   self)
Here we define the inputs and output of the algorithm, along
with some other properties

Definition at line 60 of file ExampleAlgorithm.py.

60 
61  def defineCharacteristics(self):
62  '''Here we define the inputs and output of the algorithm, along
63  with some other properties
64  '''
65 
66  # the name that the user will see in the toolbox
67  self.name = "Create copy of layer"
68 
69  # the branch of the toolbox under which the algorithm will appear
70  self.group = "Algorithms for vector layers"
71 
72  # we add the input vector layer. It can have any kind of geometry
73  # It is a mandatory (not optional) one, hence the False argument
74  self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", [ParameterVector.VECTOR_TYPE_ANY], False))
75 
76  # we add a vector layer as output
77  self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer with selected features"))
def pktools.ExampleAlgorithm.ExampleAlgorithm.processAlgorithm (   self,
  progress 
)
Here is where the processing itself takes place

Definition at line 78 of file ExampleAlgorithm.py.

78 
79  def processAlgorithm(self, progress):
80  '''Here is where the processing itself takes place'''
81 
82  # the first thing to do is retrieve the values of the parameters
83  # entered by the user
84  inputFilename = self.getParameterValue(self.INPUT_LAYER)
85  output = self.getOutputValue(self.OUTPUT_LAYER)
86 
87  # input layers vales are always a string with its location.
88  # That string can be converted into a QGIS object (a QgsVectorLayer in
89  # this case) using the Processing.getObjectFromUri() method.
90  vectorLayer = QGisLayers.getObjectFromUri(inputFilename)
91 
92  # And now we can process
93 
94  # First we create the output layer. The output value entered by the user
95  # is a string containing a filename, so we can use it directly
96  settings = QSettings()
97  systemEncoding = settings.value( "/UI/encoding", "System" )
98  provider = vectorLayer.dataProvider()
99  writer = QgsVectorFileWriter(output,
100  systemEncoding,
101  provider.fields(),
102  provider.geometryType(),
103  provider.crs()
104  )
105 
106  # Now we take the features from input layer and add them to the output.
107  # Method features() returns an iterator, considering the selection that
108  # might exist in layer and the configuration that indicates
109  # should algorithm use only selected features or all of them
110  features = QGisLayers.features(vectorLayer)
111  for f in features:
112  writer.addFeature(f)
113 
114  # There is nothing more to do here. We do not have to open the layer
115  # that we have created. The framework will take care of that, or will handle
116  # it if this algorithm is executed within a complex model

The documentation for this class was generated from the following file: