From 79295675a05a80df5e05b6a1cbec243f8121e211 Mon Sep 17 00:00:00 2001 From: Nicolas Karasiak Date: Thu, 11 Jun 2020 20:26:02 +0200 Subject: [PATCH] v3.64 with median and closing filter --- dzetsaka_provider.py | 10 +- metadata.txt | 4 +- processing/closingFilter.py | 222 ++++++++++++++++++++++++++++++++++++ 3 files changed, 232 insertions(+), 4 deletions(-) create mode 100644 processing/closingFilter.py diff --git a/dzetsaka_provider.py b/dzetsaka_provider.py index 598d781..16f7987 100644 --- a/dzetsaka_provider.py +++ b/dzetsaka_provider.py @@ -39,7 +39,6 @@ from .processing.train import trainAlgorithm from .processing.classify import classifyAlgorithm from .processing.splitTrainValidation import splitTrain -# from .processing.learnWithStandCV import trainSTANDalgorithm pluginPath = os.path.dirname(__file__) @@ -84,13 +83,18 @@ def loadAlgorithms(self): # self.addAlgorithm(trainSTANDalgorithm()) if self.providerType == 'Experimental': + from .processing.medianFilter import medianFilterAlgorithm + from .processing.closingFilter import closingFilterAlgorithm + self.addAlgorithm(closingFilterAlgorithm()) + self.addAlgorithm(medianFilterAlgorithm()) + from .processing.domainAdaptation import domainAdaptation from .processing.shannonEntropy import shannonAlgorithm - from .processing.medianFilter import medianFilterAlgorithm + self.addAlgorithm(domainAdaptation()) self.addAlgorithm(shannonAlgorithm()) - self.addAlgorithm(medianFilterAlgorithm()) + def id(self): """ diff --git a/metadata.txt b/metadata.txt index 5a48bb5..84aa930 100644 --- a/metadata.txt +++ b/metadata.txt @@ -10,7 +10,7 @@ name=dzetsaka : Classification tool qgisMinimumVersion=3.0 description=Fast and Easy Classification plugin for Qgis -version=3.63 +version=3.64 author=Nicolas Karasiak email=karasiak.nicolas@gmail.com @@ -29,6 +29,8 @@ repository=http://www.github.com/nkarasiak/dzetsaka # Uncomment the following line and add your changelog: changelog= + 3.64 + * add closing filter in the processing toolbox 3.63 * fix bug in train algorithm (split was % of train not of validation) 3.62 diff --git a/processing/closingFilter.py b/processing/closingFilter.py new file mode 100644 index 0000000..da338fa --- /dev/null +++ b/processing/closingFilter.py @@ -0,0 +1,222 @@ +# -*- coding: utf-8 -*- + +""" +/*************************************************************************** + className + A QGIS plugin + description + ------------------- + begin : 2016-12-03 + copyright : (C) 2016 by Nico + email : nico@nico + ***************************************************************************/ + +/*************************************************************************** + * * + * 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__ = 'Nicolas Karasiak' +__date__ = '2018-02-24' +__copyright__ = '(C) 2018 by Nicolas Karasiak' + +# This will get replaced with a git SHA1 when you do a git archive + +__revision__ = '$Format:%H$' + + +# from ... import dzetsaka.scripts.function_dataraster as dataraster + +#from PyQt4.QtGui import QIcon +#from PyQt4.QtCore import QSettings + + +from qgis.PyQt.QtGui import QIcon +from PyQt5.QtCore import QCoreApplication + +from qgis.core import (QgsMessageLog, + QgsProcessingAlgorithm, + QgsProcessingParameterRasterLayer, + QgsProcessingParameterNumber, + QgsProcessingParameterRasterDestination, + QgsRasterLayer) +import os +from ..scripts import function_dataraster as dataraster + +pluginPath = os.path.abspath( + os.path.join( + os.path.dirname(__file__), + os.pardir)) +# EX +""" +from processing.core.GeoAlgorithm import GeoAlgorithm +from processing.core.parameters import ParameterRaster +from processing.core.parameters import ParameterNumber +from processing.core.outputs import OutputRaster +""" + + +class closingFilterAlgorithm(QgsProcessingAlgorithm): + """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. + """ + + # Constants used to refer to parameters and outputs. They will be + # used when calling the algorithm from another algorithm, or when + # calling from the QGIS console. + + INPUT_RASTER = 'INPUT_RASTER' + OUTPUT_RASTER = 'OUTPUT_RASTER' + CLOSING_SIZE = 'CLOSING_SIZE' + + def icon(self): + + return QIcon(os.path.join(pluginPath, 'icon.png')) + + def initAlgorithm(self, config=None): + """Here we define the inputs and output of the algorithm, along + with some other properties. + """ + + # 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( + QgsProcessingParameterRasterLayer( + self.INPUT_RASTER, + self.tr('Input raster') + ) + ) + + # We add a raster as output + self.addParameter( + QgsProcessingParameterRasterDestination( + self.OUTPUT_RASTER, + self.tr('Output raster') + ) + ) + # add num + + self.addParameter( + QgsProcessingParameterNumber( + self.CLOSING_SIZE, + self.tr('Size of the closing filter'), + type=QgsProcessingParameterNumber.Integer, + defaultValue=5, + minValue=3)) + + def name(self): + """ + Returns the algorithm name, used for identifying the algorithm. This + string should be fixed for the algorithm, and must not be localised. + The name should be unique within each provider. Names should contain + lowercase alphanumeric characters only and no spaces or other + formatting characters. + """ + return 'Closing filter' + + def processAlgorithm(self, parameters, context, feedback): + """Here is where the processing itself takes place.""" + + INPUT_RASTER = self.parameterAsRasterLayer( + parameters, self.INPUT_RASTER, context) + #INPUT_RASTER = self.getParameterValue(self.INPUT_RASTER) + OUTPUT_RASTER = self.parameterAsOutputLayer( + parameters, self.OUTPUT_RASTER, context) + CLOSING_SIZE = self.parameterAsInt( + parameters, self.CLOSING_SIZE, context) + + """ + MEDIAN_ITER = self.parameterAsInt(parameters, self.MEDIAN_ITER, context) + MEDIAN_SIZE = self.parameterAsInt(parameters, self.MEDIAN_SIZE, context) + # 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 + + #from scipy import ndimage + #import gdal + """ + INPUT_RASTER_src = INPUT_RASTER.source() + + # feedback.pushInfo(str(OUTPUT_RASTER)) + #QgsMessageLog.logMessage('output is: '+str(OUTPUT_RASTER)) + + from scipy.ndimage.morphology import grey_closing + + data, im = dataraster.open_data_band(INPUT_RASTER_src) + + proj = data.GetProjection() + geo = data.GetGeoTransform() + d = data.RasterCount + + total = 100 / (d * 1) + + outFile = dataraster.create_empty_tiff(OUTPUT_RASTER, im, d, geo, proj) + + iterPos = 0 + + for i in range(d): + # Read data from the right band + # pbNow+=1 + # pb.setValue(pbNow) + + tempBand = data.GetRasterBand(i + 1).ReadAsArray() + + + tempBand = grey_closing( + tempBand, size=(CLOSING_SIZE, CLOSING_SIZE)) + #tempBand = tempBand + feedback.setProgress(int(i * total)) + + # Save bandand outFile + out = outFile.GetRasterBand(i + 1) + out.WriteArray(tempBand) + out.FlushCache() + tempBand = None + + return {self.OUTPUT_RASTER: OUTPUT_RASTER} + + # return OUTPUT_RASTER + + def tr(self, string): + return QCoreApplication.translate('Processing', string) + + def createInstance(self): + return closingFilterAlgorithm() + + def displayName(self): + """ + Returns the translated algorithm name, which should be used for any + user-visible display of the algorithm name. + """ + return self.tr(self.name()) + + def group(self): + """ + Returns the name of the group this algorithm belongs to. This string + should be localised. + """ + return self.tr(self.groupId()) + + def groupId(self): + """ + Returns the unique ID of the group this algorithm belongs to. This + string should be fixed for the algorithm, and must not be localised. + The group id should be unique within each provider. Group id should + contain lowercase alphanumeric characters only and no spaces or other + formatting characters. + """ + return 'Raster tool'