Commit 8694d5e4 authored by Gaetano Raffaele's avatar Gaetano Raffaele
Browse files

WIP: complete refactoring of stratification application.

parent e4976954
import warnings
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessingAlgorithm,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterVectorDestination)
from qgis import processing
import os,sys
sys.path.append(os.path.dirname(__file__))
from utils import *
class GetStratificationMap(QgsProcessingAlgorithm):
INPUT = 'INPUT'
THRESHOLD = 'THRESHOLD'
CW = 'CW'
SW = 'SW'
OUTPUT = 'OUTPUT'
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return GetStratificationMap()
def name(self):
return 'GetStratificationMap'
def displayName(self):
return self.tr('Get Stratification Map')
def group(self):
return self.tr('TSNDVI')
def groupId(self):
return 'TSNDVI'
def shortHelpString(self):
return self.tr("Segment a stratification metric to produce a map.")
def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterRasterLayer(
self.INPUT,
self.tr('Input landscape stratification metric')
)
)
self.addParameter(
QgsProcessingParameterNumber(
self.THRESHOLD,
self.tr('Threshold for Baatz and Schaepe criterion'),
type=0,
defaultValue=1000
)
)
self.addParameter(
QgsProcessingParameterNumber(
self.CW,
self.tr('Color (spectral) weight'),
type=2,
defaultValue=0.5
)
)
self.addParameter(
QgsProcessingParameterNumber(
self.DURATION,
self.tr('Spatial (compactness) weight'),
type=2,
defaultValue=0.5
)
)
self.addParameter(
QgsProcessingParameterVectorDestination(
self.OUTPUT,
self.tr('Output landscape stratification metric')
)
)
def processAlgorithm(self, parameters, context, feedback):
metric = self.parameterAsString(
parameters,
self.INPUT,
context
)
output_vector = self.parameterAsOutputLayer(
parameters,
self.OUTPUT,
context
)
ndv = getNoDataValue(metric)
output_folder = os.path.dirname(output_vector)
threshold = parameters['THRESHOLD']
cw = parameters['CW']
sw = parameters['SW']
prefix = os.path.splitext(os.path.basename(output_vector))[0] + '_'
tmp_name = prefix + 'temp'
tmp_folder = os.path.join(output_folder, tmp_name)
if not os.path.exists(tmp_folder):
os.mkdir(tmp_folder)
to_del = []
grm_pre = os.path.join(tmp_folder, prefix + '_segraw.tif')
grm_params = {'in': metric, 'out': grm_pre, 'criterion': 'bs', 'criterion.bs.cw': cw, 'criterion.bs.sw': sw, 'threshold': threshold}
processing.run('otb:LSGRM', grm_params, context=context, feedback=feedback)
ndt = os.path.join(tmp_folder, prefix + '_segndt.tif')
ndt_params = {'il': [metric, grm_pre], 'exp': 'im1b1 != ' + str(ndv) + ' ? im2b1 : 0'}
processing.run('otb:BandMath', ndt_params, context=context, feedback=feedback)
vec = output_vector
vec_params = {'in': ndt, 'out': vec}
out = processing.run('otb:SimpleVectorization', vec_params, context=context, feedback=feedback)
return {self.OUTPUT: out}
......@@ -52,7 +52,7 @@ def moy_NDVI_TS(TS,dates, month, day, duration):
periods = get_period_intervals(dates, [month,day], duration)
return get_mean_expression(periods)
class LandscapeStratification(QgsProcessingAlgorithm):
class LandscapeStratificationMetric(QgsProcessingAlgorithm):
INPUT = 'INPUT'
INPUTDATE = 'INPUTDATE'
......@@ -71,13 +71,13 @@ class LandscapeStratification(QgsProcessingAlgorithm):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return LandscapeStratification()
return LandscapeStratificationMetric()
def name(self):
return 'LandscapeStratification'
return 'LandscapeStratificationMetric'
def displayName(self):
return self.tr('LandscapeStratification')
return self.tr('Landscape Stratification')
def group(self):
return self.tr('TSNDVI')
......@@ -86,7 +86,7 @@ class LandscapeStratification(QgsProcessingAlgorithm):
return 'TSNDVI'
def shortHelpString(self):
return self.tr("Compute a landscape stratification metric")
return self.tr("Compute a PCA-based landscape stratification metric")
def initAlgorithm(self, config=None):
self.addParameter(
......
......@@ -45,7 +45,7 @@ class TemporalSmoothing(QgsProcessingAlgorithm):
return 'TemporalSmoothing'
def displayName(self):
return self.tr('TemporalSmoothing')
return self.tr('Temporal Smoothing')
def group(self):
return self.tr('TSNDVI')
......@@ -54,7 +54,7 @@ class TemporalSmoothing(QgsProcessingAlgorithm):
return 'TSNDVI'
def shortHelpString(self):
return self.tr("Compute the trend of the vegetation index time series")
return self.tr("Apply Savitzky-Golay filtering to a single channel time series (e.g. NDVI).")
def initAlgorithm(self, config=None):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment