PavelVeselsky / fft-convolution-filter

Raster smoothing and edge detection filters based on fftconvolve SciPy function
GNU General Public License v3.0
1 stars 1 forks source link

Please consider adding it to Processing #3

Open pcav opened 9 years ago

pcav commented 9 years ago

so it can be used in complex modelling, batch, etc.

PavelVeselsky commented 9 years ago

I considered this, but I didn't find how to do it. I asked a question for it on gis.stackexchange.com and I'll implement this after getting an answer there.

pcav commented 9 years ago

Please see some other plugin, e.g. Lecos, or ask also on QGIS-dev ML. It should be reasonably easy. Thanks.

Martin-Jung commented 9 years ago

:+1: Easiest way howto to do this (the way I did it in LecoS) is to restructure your code into proper classes instead of functions.

So you create a provider class

# Processing bindings
from processing.core.AlgorithmProvider import AlgorithmProvider
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.ProcessingConfig import Setting
from processing.core.ProcessingLog import ProcessingLog

# Your algorithm classes
from fftconv import *

class LecoSAlgorithmsProv(AlgorithmProvider):

    def __init__(self):
        AlgorithmProvider.__init__(self)
        # Create algorithms list
        self.createAlgsList()

    def getDescription(self):
        return "Plugin name"

    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)
        '''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)

    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 "yourName"

    def getIcon(self):
        '''Return the Icon if you have one (you should)'''
        return QIcon("link to file")

    def createAlgsList(self):
        '''Create list of Arguments'''

        self.preloadedAlgs = []        
        # Load in Algorithms from fftconv

        # Landscape preperation
        self.preloadedAlgs.append( doFFT() )        

        for alg in self.preloadedAlgs:
            alg.provider = self # reset provider

    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.preloadedAlgs

Then create or load a new file (fftconv.py expected above), which actually contains your algorithm. This class has to inherit from GeoAlgorithm

class doFFT(GeoAlgorithm):
    # Define constants
    INPUT_RASTER = "INPUT_RASTER"
    OUTPUT_RASTER = "OUTPUT_RASTER"

    def getIcon(self):
        '''Return the Icon if you have one (you should)'''
        return QIcon("link to file")

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

        self.name = "Do some FFT smoothing"
        self.cmdName = "fftsmooth"
        self.group = "Raster generalization"

        self.addParameter(ParameterRaster(self.INPUT_RASTER, "InputRaster", False))
        # The output
        self.addOutput(OutputRaster(self.OUTPUT_RASTER, "Result output"))

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

        # Retrieve the values of the parameters entered by the user
        inputSource = self.getParameterValue(self.INPUT_RASTER) # returns a QgsRaster object
        output = self.getOutputValue(self.OUTPUT_RASTER) # Path string and any output will be written to this file

        # Then run the algorithm...        

    def help(self):
        # Consider writting a proper help file (in html)
        helppath = os.path.join("help.html")
        if os.path.isfile(helppath):
            return False, helppath
        else:
            return False, None