Commit de88ebf8 authored by Gaetano Raffaele's avatar Gaetano Raffaele
Browse files

WIP: complete refactoring of stratification application.

parent f819110b
......@@ -10,6 +10,7 @@
* *
***************************************************************************
"""
import warnings
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessingAlgorithm,
......@@ -271,8 +272,12 @@ class LandscapeStratification(QgsProcessingAlgorithm):
if datefile == '':
datefile = os.path.splitext(Smooth_TS)[0] + '_dates.txt'
to_del = []
if int(begin_date)!=20000101 or int(end_date)!=int(datetime.date.today().strftime("%Y%m%d")):
TS_file, datefile = NDVI_subdates_maker(Smooth_TS,datefile,tmp_folder,int(begin_date),int(end_date),context,feedback)
to_del.append(TS_file)
to_del.append(datefile)
else :
TS_file = Smooth_TS
......@@ -286,11 +291,13 @@ class LandscapeStratification(QgsProcessingAlgorithm):
os.remove(os.path.join(output_folder,"outcontext.txt"))
if ndv is not None:
setNoDataValue(Moy_TS, ndv)
to_del.append(Moy_TS)
mask = os.path.join(tmp_folder, prefix + "mask.tif")
ND_app_parameters = {"in": TS_file, "out": mask, "mode": "buildmask", 'mode.apply.mask': TS_file,
"outputpixeltype": 0}
processing.run('otb:ManageNoData', ND_app_parameters, context=context, feedback=feedback)
to_del.append(mask)
# Compute metric using no-data value
LS_Strat_pre = os.path.join(tmp_folder, prefix+"LandStrat_metric_pre.tif")
......@@ -302,16 +309,19 @@ class LandscapeStratification(QgsProcessingAlgorithm):
if cend != None :
LS_app_parameters["cend"]=cend
processing.run("otb:LandscapeStratificationMetric", LS_app_parameters, context=context, feedback=feedback)
to_del.append(LS_Strat_pre)
LS_Strat_raw = os.path.join(tmp_folder, prefix + "LandStrat_metric_raw.tif")
ND_app_parameters = {'in': LS_Strat_pre, 'out': LS_Strat_raw, 'mode': 'apply', 'mode.apply.mask': mask,
'outputpixeltype': 5}
processing.run('otb:ManageNoData', ND_app_parameters, context=context, feedback=feedback)
setNoDataValue(LS_Strat_raw, ndv)
to_del.append(LS_Strat_raw)
LS_Strat_norm = os.path.join(tmp_folder, prefix+"LandStrat_metric_norm.tif")
DC_app_parameters={"in":LS_Strat_raw, "out":LS_Strat_norm, "mask":mask, "outmin":0, "outmax":2048, "outputpixeltype":2}
processing.run("otb:DynamicConvert", DC_app_parameters, context=context, feedback=feedback)
to_del.append(LS_Strat_norm)
LS_Strat = output_raster
ND_app_parameters = {'in': LS_Strat_norm, 'out':LS_Strat, 'mode':'apply', 'mode.apply.mask':mask, 'outputpixeltype':2}
......@@ -319,6 +329,13 @@ class LandscapeStratification(QgsProcessingAlgorithm):
setNoDataValue(LS_Strat, ndv)
if not parameters['KEEP_TEMP']:
rmtree(tmp_folder)
for x in to_del:
try:
os.remove(x)
except:
warnings.warn('Could not remove ' + x)
if len(os.listdir(tmp_folder)) == 0:
os.rmdir(tmp_folder)
return {self.OUTPUT: out_layer}
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