pktools  2.6.3
Processing Kernel for remote sensing data
ExampleAlgorithm.py
1 # -*- coding: utf-8 -*-
2 
3 """
4 ***************************************************************************
5  __init__.py
6  ---------------------
7  Date : July 2013
8  Copyright : (C) 2013 by Victor Olaya
9  Email : volayaf at gmail dot com
10 ***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************
18 """
19 
20 __author__ = 'Victor Olaya'
21 __date__ = 'July 2013'
22 __copyright__ = '(C) 2013, Victor Olaya'
23 # This will get replaced with a git SHA1 when you do a git archive
24 __revision__ = '$Format:%H$'
25 
26 
27 from PyQt4.QtCore import *
28 from PyQt4.QtGui import *
29 
30 from qgis.core import *
31 
32 from processing.core.Processing import Processing
33 from processing.core.GeoAlgorithm import GeoAlgorithm
34 from processing.core.QGisLayers import QGisLayers
35 
36 from processing.parameters.ParameterVector import ParameterVector
37 
38 from processing.outputs.OutputVector import OutputVector
39 
40 
41 class ExampleAlgorithm(GeoAlgorithm):
42  '''This is an example algorithm that takes a vector layer and creates
43  a new one just with just those features of the input layer that are
44  selected.
45 
46  It is meant to be used as an example of how to create your own
47  algorithms and explain methods and variables used to do it. An algorithm
48  like this will be available in all elements, and there is not need
49  for additional work.
50 
51  All processing algorithms should extend the GeoAlgorithm class.
52  '''
53 
54  # constants used to refer to parameters and outputs.
55  # They will be used when calling the algorithm from another algorithm,
56  # or when calling from the QGIS console.
57  OUTPUT_LAYER = "OUTPUT_LAYER"
58  INPUT_LAYER = "INPUT_LAYER"
59 
61  '''Here we define the inputs and output of the algorithm, along
62  with some other properties
63  '''
64 
65  # the name that the user will see in the toolbox
66  self.name = "Create copy of layer"
67 
68  # the branch of the toolbox under which the algorithm will appear
69  self.group = "Algorithms for vector layers"
70 
71  # we add the input vector layer. It can have any kind of geometry
72  # It is a mandatory (not optional) one, hence the False argument
73  self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", [ParameterVector.VECTOR_TYPE_ANY], False))
74 
75  # we add a vector layer as output
76  self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer with selected features"))
77 
78  def processAlgorithm(self, progress):
79  '''Here is where the processing itself takes place'''
80 
81  # the first thing to do is retrieve the values of the parameters
82  # entered by the user
83  inputFilename = self.getParameterValue(self.INPUT_LAYER)
84  output = self.getOutputValue(self.OUTPUT_LAYER)
85 
86  # input layers vales are always a string with its location.
87  # That string can be converted into a QGIS object (a QgsVectorLayer in
88  # this case) using the Processing.getObjectFromUri() method.
89  vectorLayer = QGisLayers.getObjectFromUri(inputFilename)
90 
91  # And now we can process
92 
93  # First we create the output layer. The output value entered by the user
94  # is a string containing a filename, so we can use it directly
95  settings = QSettings()
96  systemEncoding = settings.value( "/UI/encoding", "System" )
97  provider = vectorLayer.dataProvider()
98  writer = QgsVectorFileWriter(output,
99  systemEncoding,
100  provider.fields(),
101  provider.geometryType(),
102  provider.crs()
103  )
104 
105  # Now we take the features from input layer and add them to the output.
106  # Method features() returns an iterator, considering the selection that
107  # might exist in layer and the configuration that indicates
108  # should algorithm use only selected features or all of them
109  features = QGisLayers.features(vectorLayer)
110  for f in features:
111  writer.addFeature(f)
112 
113  # There is nothing more to do here. We do not have to open the layer
114  # that we have created. The framework will take care of that, or will handle
115  # it if this algorithm is executed within a complex model