Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[sextante] add example provider code
  • Loading branch information
alexbruy committed Jul 29, 2013
1 parent 9359753 commit 0b92877
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/plugins/sextante/CMakeLists.txt
Expand Up @@ -19,6 +19,7 @@ ADD_SUBDIRECTORY(script)
ADD_SUBDIRECTORY(taudem)
ADD_SUBDIRECTORY(tools)
ADD_SUBDIRECTORY(tests)
ADD_SUBDIRECTORY(exampleprovider)

FILE(GLOB UI_FILES ui/*.ui)
PYQT4_WRAP_UI(PYUI_FILES ${UI_FILES})
Expand Down
5 changes: 5 additions & 0 deletions python/plugins/sextante/exampleprovider/CMakeLists.txt
@@ -0,0 +1,5 @@
FILE(GLOB PY_FILES *.py)
FILE(GLOB OTHER_FILES *.txt)

PLUGIN_INSTALL(sextante ./exampleprovider ${PY_FILES})
PLUGIN_INSTALL(sextante ./exampleprovider ${OTHER_FILES})
115 changes: 115 additions & 0 deletions python/plugins/sextante/exampleprovider/ExampleAlgorithm.py
@@ -0,0 +1,115 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
__init__.py
---------------------
Date : July 2013
Copyright : (C) 2013 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Victor Olaya'
__date__ = 'July 2013'
__copyright__ = '(C) 2013, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'


from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *

from sextante.core.Sextante import Sextante
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers

from sextante.parameters.ParameterVector import ParameterVector

from sextante.outputs.OutputVector import OutputVector


class ExampleAlgorithm(GeoAlgorithm):
'''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 SEXTANTE
algorithms and explain methods and variables used to do it. An algorithm
like this will be available in all SEXTANTE elements, and there is not need
for additional work.
All SEXTANTE algorithms should extend the GeoAlgorithm class.
'''

# constants used to refer to parameters and outputs.
# They will be used when calling the algorithm from another algorithm,
# or when calling SEXTANTE from the QGIS console.
OUTPUT_LAYER = "OUTPUT_LAYER"
INPUT_LAYER = "INPUT_LAYER"

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

# the name that the user will see in the toolbox
self.name = "Create copy of layer"

# the branch of the toolbox under which the algorithm will appear
self.group = "Algorithms for vector layers"

# we add the input vector layer. It can have any kind of geometry
# It is a mandatory (not optional) one, hence the False argument
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))

# we add a vector layer as output
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer with selected features"))

def processAlgorithm(self, progress):
'''Here is where the processing itself takes place'''

# the first thing to do is retrieve the values of the parameters
# entered by the user
inputFilename = self.getParameterValue(self.INPUT_LAYER)
output = self.getOutputValue(self.OUTPUT_LAYER)

# input layers vales are always a string with its location.
# That string can be converted into a QGIS object (a QgsVectorLayer in
# this case) using the Sextante.getObjectFromUri() method.
vectorLayer = QGisLayers.getObjectFromUri(inputFilename)

# And now we can process

# First we create the output layer. The output value entered by the user
# is a string containing a filename, so we can use it directly
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" )
provider = vectorLayer.dataProvider()
writer = QgsVectorFileWriter(output,
systemEncoding,
provider.fields(),
provider.geometryType(),
provider.crs()
)

# Now we take the features from input layer and add them to the output.
# Method features() returns an iterator, considiring the selection that
# might exisist in layer and SEXTANTE configuration that indicates
# should algorithm use only selected features or all of them
features = QGisLayers.features(vectorLayer)
for f in features:
writer.addFeature(f)

# There is nothing more to do here. We do not have to open the layer
# that we have created. SEXTANTE will take care of that, or will handle
# it if this algorithm is executed within a complex model
@@ -0,0 +1,91 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
__init__.py
---------------------
Date : July 2013
Copyright : (C) 2013 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Victor Olaya'
__date__ = 'July 2013'
__copyright__ = '(C) 2013, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'


from exampleprovider.ExampleAlgorithm import ExampleAlgorithm

from sextante.core.AlgorithmProvider import AlgorithmProvider
from sextante.core.SextanteConfig import Setting, SextanteConfig


class ExampleAlgorithmProvider(AlgorithmProvider):

MY_DUMMY_SETTING = "MY_DUMMY_SETTING"

def __init__(self):
AlgorithmProvider.__init__(self)
# deactivate provider by default
self.activate = False
# load algorithms
self.alglist = [ExampleAlgorithm()]
for alg in self.alglist:
alg.provider = self

def initializeSettings(self):
'''In this method we add settings needed to configure our provider.
Do not forget to call the parent method, since it takes care or
automatically adding a setting for activating or deactivating the
algorithms in the provider
'''
AlgorithmProvider.initializeSettings(self)
SextanteConfig.addSetting(Setting("Example algorithms", ExampleAlgorithmProvider.MY_DUMMY_SETTING, "Example setting", "Default value"))
'''To get the parameter of a setting parameter, use
SextanteConfig.getSetting(name_of_parameter)
'''

def unload(self):
'''Setting should be removed here, so they do not appear anymore
when the plugin is unloaded'''
AlgorithmProvider.unload(self)
SextanteConfig.removeSetting(ExampleAlgorithmProvider.MY_DUMMY_SETTING)

def getName(self):
'''This is the name that will appear on the toolbox group.
It is also used to create the command line name of all the algorithms
from this provider
'''
return "Example provider"

def getDescription(self):
'''This is the provired full name.
'''
return "Example algorithms"

def getIcon(self):
'''We return the default icon'''
return AlgorithmProvider.getIcon(self)

def _loadAlgorithms(self):
'''Here we fill the list of algorithms in self.algs.
This method is called whenever the list of algorithms should be updated.
If the list of algorithms can change while executing SEXTANTE for QGIS
(for instance, if it contains algorithms from user-defined scripts and
a new script might have been added), you should create the list again
here.
In this case, since the list is always the same, we assign from the pre-made list.
This assignment has to be done in this method even if the list does not change,
since the self.algs list is cleared before calling this method
'''
self.algs = self.alglist
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
__init__.py
---------------------
Date : July 2013
Copyright : (C) 2013 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Victor Olaya'
__date__ = 'July 2013'
__copyright__ = '(C) 2013, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'


import os, sys
import inspect

from qgis.core import *

from sextante.core.Sextante import Sextante
from exampleprovider.ExampleAlgorithmProvider import ExampleAlgorithmProvider

cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]

if cmd_folder not in sys.path:
sys.path.insert(0, cmd_folder)


class SextanteExampleProviderPlugin:
def __init__(self):
self.provider = ExampleAlgorithmProvider()

def initGui(self):
Sextante.addProvider(self.provider)

def unload(self):
Sextante.removeProvider(self.provider)
29 changes: 29 additions & 0 deletions python/plugins/sextante/exampleprovider/__init__.py
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
__init__.py
---------------------
Date : July 2013
Copyright : (C) 2013 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Victor Olaya'
__date__ = 'July 2013'
__copyright__ = '(C) 2013, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'


def classFactory(iface):
from exampleprovider.SextanteExampleProviderPlugin import SextanteExampleProviderPlugin
return SextanteExampleProviderPlugin()
18 changes: 18 additions & 0 deletions python/plugins/sextante/exampleprovider/metadata.txt
@@ -0,0 +1,18 @@
[general]
name=SEXTANTE Example Provider
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
category=Analysis
version=2.0
qgisMinimumVersion=2.0

author=Victor Olaya
email=volayaf@gmail.com

tags=analysis,sextante

homepage=
tracker=
repository=

experimental=False
deprecated=False

0 comments on commit 0b92877

Please sign in to comment.