Skip to content

Commit 0b92877

Browse files
committed
[sextante] add example provider code
1 parent 9359753 commit 0b92877

File tree

7 files changed

+308
-0
lines changed

7 files changed

+308
-0
lines changed

python/plugins/sextante/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ADD_SUBDIRECTORY(script)
1919
ADD_SUBDIRECTORY(taudem)
2020
ADD_SUBDIRECTORY(tools)
2121
ADD_SUBDIRECTORY(tests)
22+
ADD_SUBDIRECTORY(exampleprovider)
2223

2324
FILE(GLOB UI_FILES ui/*.ui)
2425
PYQT4_WRAP_UI(PYUI_FILES ${UI_FILES})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FILE(GLOB PY_FILES *.py)
2+
FILE(GLOB OTHER_FILES *.txt)
3+
4+
PLUGIN_INSTALL(sextante ./exampleprovider ${PY_FILES})
5+
PLUGIN_INSTALL(sextante ./exampleprovider ${OTHER_FILES})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 sextante.core.Sextante import Sextante
33+
from sextante.core.GeoAlgorithm import GeoAlgorithm
34+
from sextante.core.QGisLayers import QGisLayers
35+
36+
from sextante.parameters.ParameterVector import ParameterVector
37+
38+
from sextante.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 SEXTANTE
47+
algorithms and explain methods and variables used to do it. An algorithm
48+
like this will be available in all SEXTANTE elements, and there is not need
49+
for additional work.
50+
51+
All SEXTANTE 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 SEXTANTE from the QGIS console.
57+
OUTPUT_LAYER = "OUTPUT_LAYER"
58+
INPUT_LAYER = "INPUT_LAYER"
59+
60+
def defineCharacteristics(self):
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 Sextante.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, considiring the selection that
107+
# might exisist in layer and SEXTANTE 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. SEXTANTE will take care of that, or will handle
115+
# it if this algorithm is executed within a complex model
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 exampleprovider.ExampleAlgorithm import ExampleAlgorithm
28+
29+
from sextante.core.AlgorithmProvider import AlgorithmProvider
30+
from sextante.core.SextanteConfig import Setting, SextanteConfig
31+
32+
33+
class ExampleAlgorithmProvider(AlgorithmProvider):
34+
35+
MY_DUMMY_SETTING = "MY_DUMMY_SETTING"
36+
37+
def __init__(self):
38+
AlgorithmProvider.__init__(self)
39+
# deactivate provider by default
40+
self.activate = False
41+
# load algorithms
42+
self.alglist = [ExampleAlgorithm()]
43+
for alg in self.alglist:
44+
alg.provider = self
45+
46+
def initializeSettings(self):
47+
'''In this method we add settings needed to configure our provider.
48+
Do not forget to call the parent method, since it takes care or
49+
automatically adding a setting for activating or deactivating the
50+
algorithms in the provider
51+
'''
52+
AlgorithmProvider.initializeSettings(self)
53+
SextanteConfig.addSetting(Setting("Example algorithms", ExampleAlgorithmProvider.MY_DUMMY_SETTING, "Example setting", "Default value"))
54+
'''To get the parameter of a setting parameter, use
55+
SextanteConfig.getSetting(name_of_parameter)
56+
'''
57+
58+
def unload(self):
59+
'''Setting should be removed here, so they do not appear anymore
60+
when the plugin is unloaded'''
61+
AlgorithmProvider.unload(self)
62+
SextanteConfig.removeSetting(ExampleAlgorithmProvider.MY_DUMMY_SETTING)
63+
64+
def getName(self):
65+
'''This is the name that will appear on the toolbox group.
66+
It is also used to create the command line name of all the algorithms
67+
from this provider
68+
'''
69+
return "Example provider"
70+
71+
def getDescription(self):
72+
'''This is the provired full name.
73+
'''
74+
return "Example algorithms"
75+
76+
def getIcon(self):
77+
'''We return the default icon'''
78+
return AlgorithmProvider.getIcon(self)
79+
80+
def _loadAlgorithms(self):
81+
'''Here we fill the list of algorithms in self.algs.
82+
This method is called whenever the list of algorithms should be updated.
83+
If the list of algorithms can change while executing SEXTANTE for QGIS
84+
(for instance, if it contains algorithms from user-defined scripts and
85+
a new script might have been added), you should create the list again
86+
here.
87+
In this case, since the list is always the same, we assign from the pre-made list.
88+
This assignment has to be done in this method even if the list does not change,
89+
since the self.algs list is cleared before calling this method
90+
'''
91+
self.algs = self.alglist
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
import os, sys
28+
import inspect
29+
30+
from qgis.core import *
31+
32+
from sextante.core.Sextante import Sextante
33+
from exampleprovider.ExampleAlgorithmProvider import ExampleAlgorithmProvider
34+
35+
cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
36+
37+
if cmd_folder not in sys.path:
38+
sys.path.insert(0, cmd_folder)
39+
40+
41+
class SextanteExampleProviderPlugin:
42+
def __init__(self):
43+
self.provider = ExampleAlgorithmProvider()
44+
45+
def initGui(self):
46+
Sextante.addProvider(self.provider)
47+
48+
def unload(self):
49+
Sextante.removeProvider(self.provider)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
def classFactory(iface):
28+
from exampleprovider.SextanteExampleProviderPlugin import SextanteExampleProviderPlugin
29+
return SextanteExampleProviderPlugin()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[general]
2+
name=SEXTANTE Example Provider
3+
description=An example plugin that adds algorithms to SEXTANTE. Mainly created to guide developers in the process of creating plugins that add new capabilities to SEXTANTE
4+
category=Analysis
5+
version=2.0
6+
qgisMinimumVersion=2.0
7+
8+
author=Victor Olaya
9+
email=volayaf@gmail.com
10+
11+
tags=analysis,sextante
12+
13+
homepage=
14+
tracker=
15+
repository=
16+
17+
experimental=False
18+
deprecated=False

0 commit comments

Comments
 (0)