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

WIP: complete refactoring of stratification application.

parent 7a07b57f
......@@ -20,7 +20,6 @@ from qgis.core import (QgsProcessingAlgorithm,
QgsProcessingParameterVectorLayer,
QgsProcessingContext)
from qgis import processing
from qgis.utils import iface
import ogr
import os
import sys
......@@ -41,65 +40,39 @@ def find_first_month_day(dates, month=1, day=1):
if dates[0] > test_date:
start_year += 1
test_date = datetime.datetime(start_year, month, day)
return find_next_date(dates, test_date)
def get_period_intervals(date_file, md=[1,1], duration=365):
def get_period_intervals(date_file, md=[1, 1], duration=365):
dates = []
#orig_dates = []
with open(date_file) as f:
for l in f.read().splitlines():
try:
dates.append(datetime.datetime.strptime(l, '%Y%m%d'))
#orig_dates.append(l)
except:
continue
delta = dates[-1] - dates[-2]
periods = []
s,e = 0,0
s, e = 0, 0
S = 0
while s is not None and e is not None:
s = find_first_month_day(dates, md[0], md[1])
if s is not None:
e = find_next_date(dates, dates[s] + datetime.timedelta(days=duration))
e = find_next_date(dates, dates[s] + datetime.timedelta(days=duration) - delta)
if e is not None:
periods.append((S+s,S+e))
S += s+1
dates = dates[s+1:]
#for p in periods:
# print(orig_dates[p[0]],orig_dates[p[1]],p[1]-p[0]+1)
periods.append((S + s, S + e + 1))
S += s + 1
dates = dates[s + 1:]
return periods
def get_mean_expression(periods):
l = periods[0][1]-periods[0][0]+1
l = periods[0][1] - periods[0][0] + 1
expr = []
for i in range(1,l+1):
s = [p[0]+i for p in periods]
for i in range(1, l + 1):
s = [p[0] + i for p in periods]
expr.append('(' + '+'.join(['im1b%d' % x for x in s]) + ')/' + str(len(s)))
return '{' + ';'.join(expr) + '}'
def ModisToYYYYMMDD(raster_list, output):
dates=[]
for path in raster_list :
modisDate = path.split('_')[-2][3:]
year = int(modisDate[:-3])
day = int(modisDate[-3:])
month = 1
while day - calendar.monthrange(year,month)[1] > 0 and month <= 12:
day = day - calendar.monthrange(year,month)[1]
month = month + 1
month= '0'+str(month) if month < 10 else str(month)
day= '0'+str(day) if day < 10 else str(day)
dates.append(str(year)+str(month)+str(day))
dates.sort()
with open(output,'w') as f:
for date in dates:
f.write(date+'\n')
def NDVI_subdates_maker(TS_file,datefile,output_folder,begin_date,end_date,context,feedback):
raster_band_list = []
dates_list= []
......@@ -123,36 +96,16 @@ def NDVI_subdates_maker(TS_file,datefile,output_folder,begin_date,end_date,conte
return out, os.path.join(output_folder,os.path.basename(datefile))
def setNoDataValue(fn, val=None):
""" setNoDataValue(fn,val = 0)
Sets a value for nodata pixels in a raster GDAL dataset.
INPUT : fn - GDAL dataset file
val - nodata value (def. 0)
OUTPUT : None
"""
ds = gdal.Open(fn, gdal.GA_Update)
for i in range(0, ds.RasterCount):
band = ds.GetRasterBand(i + 1)
if val is None:
band.DeleteNoDataValue()
else:
band.SetNoDataValue(val)
band = None
ds.FlushCache()
if val is None:
ds.GetRasterBand(1).DeleteNoDataValue()
else:
ds.GetRasterBand(1).SetNoDataValue(val)
ds = None
def getNoDataValue(fn):
""" getNoDataValue(fn,val = 0)
Gets the value for nodata pixels in a raster GDAL dataset.
INPUT : fn - GDAL dataset file
OUTPUT : no-data value
"""
ds = gdal.Open(fn)
band = ds.GetRasterBand(1)
ndv = band.GetNoDataValue()
ndv = ds.GetRasterBand(1).GetNoDataValue()
ds = None
return ndv
......@@ -161,22 +114,6 @@ def moy_NDVI_TS(TS,dates, month, day, duration):
return get_mean_expression(periods)
class LandscapeStratification(QgsProcessingAlgorithm):
"""
This is an example algorithm that takes a vector layer and
creates a new identical one.
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 QgsProcessingAlgorithm
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 = 'INPUT'
INPUTDATE = 'INPUTDATE'
......@@ -185,74 +122,33 @@ class LandscapeStratification(QgsProcessingAlgorithm):
STARTMONTH = 'STARTMONTH'
STARTDAY = 'STARTDAY'
DURATION = 'DURATION'
CLIP = 'CLIP'
OUTPUT = 'OUTPUT'
PREFIX = 'PREFIX'
CBEGIN = 'CBEGIN'
CEND = 'CEND'
SW = 'SW'
CW = 'CW'
THRESHOLD = 'THRESHOLD'
def tr(self, string):
"""
Returns a translatable string with the self.tr() function.
"""
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return LandscapeStratification()
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 'LandscapeStratification'
def displayName(self):
"""
Returns the translated algorithm name, which should be used for any
user-visible display of the algorithm name.
"""
return self.tr('LandscapeStratification')
def group(self):
"""
Returns the name of the group this algorithm belongs to. This string
should be localised.
"""
return self.tr('TSNDVI')
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 'TSNDVI'
def shortHelpString(self):
"""
Returns a localised short helper string for the algorithm. This string
should provide a basic description about what the algorithm does and the
parameters and outputs associated with it..
"""
return self.tr("Compute a landscape stratification metric")
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 features source. It can have any kind of
# geometry.
self.addParameter(
QgsProcessingParameterFile(
self.INPUT,
......@@ -334,34 +230,13 @@ class LandscapeStratification(QgsProcessingAlgorithm):
self.tr('Starting PCA component'),
type=0,
defaultValue=2))
self.addParameter(
QgsProcessingParameterNumber(
self.CEND,
self.tr('Final PCA component (0 for last)'),
type=0,
defaultValue=5))
self.addParameter(
QgsProcessingParameterNumber(
self.SW,
self.tr('Weight for the spatial homogeneity'),
type=1,
defaultValue=0.5,
minValue=0,
maxValue=1))
self.addParameter(
QgsProcessingParameterNumber(
self.CW,
self.tr('Weight for the spectral homogeneity'),
type=1,
defaultValue=0.5,
minValue=0,
maxValue=1))
self.addParameter(
QgsProcessingParameterNumber(
self.THRESHOLD,
self.tr('Threshold for Generic region merging operation'),
defaultValue=850
))
self.addParameter(
QgsProcessingParameterFolderDestination(
......@@ -371,18 +246,7 @@ class LandscapeStratification(QgsProcessingAlgorithm):
)
def processAlgorithm(self, parameters, context, feedback):
"""
Here is where the processing itself takes place.
"""
if platform.system() == 'Linux':
sh = False
elif platform.system() == 'Windows':
sh = True
else:
sys.exit("Platform not supported!")
# Retrieve the feature source and sink. The 'dest_id' variable is used
# to uniquely identify the feature sink, and must be included in the
# dictionary returned by the processAlgorithm function.
Smooth_TS = self.parameterAsString(
parameters,
self.INPUT,
......@@ -395,12 +259,6 @@ class LandscapeStratification(QgsProcessingAlgorithm):
context
)
clip = self.parameterAsVectorLayer(
parameters,
self.CLIP,
context
)
output_folder = self.parameterAsString(
parameters,
self.OUTPUT,
......@@ -412,24 +270,17 @@ class LandscapeStratification(QgsProcessingAlgorithm):
prefix+='_'
begin_date = parameters['BEGDATE'] if parameters['BEGDATE'] != None else 20000101
end_date = parameters['ENDDATE'] if parameters['ENDDATE'] != None else int(datetime.date.today().strftime("%Y%m%d"))
#write_temp_files = parameters['KEEPFILES']
threshold=parameters['THRESHOLD']
cw=parameters['CW']
sw=parameters['SW']
cbegin=parameters['CBEGIN']
cend=parameters['CEND']
tmp_name = prefix + 'metrics'
cbegin = parameters['CBEGIN']
cend = parameters['CEND']
tmp_name = prefix + 'temp'
tmp_folder = os.path.join(output_folder,tmp_name)
if not os.path.exists(tmp_folder):
os.mkdir(tmp_folder)
print('Check Datefile')
print(datefile)
if datefile == '':
datefile = os.path.splitext(Smooth_TS)[0] + '_dates.txt'
print(datefile)
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)
......@@ -447,24 +298,10 @@ class LandscapeStratification(QgsProcessingAlgorithm):
if ndv is not None:
setNoDataValue(Moy_TS, ndv)
# manage mask (nodata from input U clip mask)
if clip is not None:
maskc = os.path.join(tmp_folder, prefix+"mask_clip.tif")
processing.run("otb:Rasterization", {"in":clip,"im":Smooth_TS,"mode.binary.foreground":1,"out":maskc, "outputpixeltype":0}, context=context, feedback=feedback)
vald = os.path.join(tmp_folder, prefix+"valid.tif")
ND_app_parameters = {"in": TS_file, "out":vald, "mode":"buildmask", 'mode.apply.mask':maskc, "outputpixeltype":0}
processing.run('otb:ManageNoData', ND_app_parameters, context=context, feedback=feedback)
mask = os.path.join(tmp_folder, prefix+"mask.tif")
BMX_parameters = {'il': [maskc, vald], 'exp':'im1b1*im2b1', 'out':mask, 'outputpixeltype':0, 'outcontext':os.path.join(output_folder,"outcontext.txt")}
processing.run("otb:BandMathX", BMX_parameters, context=context, feedback=feedback)
os.remove(os.path.join(output_folder,"outcontext.txt"))
#os.remove(maskc)
#os.remove(vald)
else:
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)
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)
# Compute metric using no-data value
LS_Strat_pre = os.path.join(tmp_folder, prefix+"LandStrat_metric_pre.tif")
......@@ -477,48 +314,23 @@ class LandscapeStratification(QgsProcessingAlgorithm):
LS_app_parameters["cend"]=cend
processing.run("otb:LandscapeStratificationMetric", LS_app_parameters, context=context, feedback=feedback)
LS_Strat = os.path.join(tmp_folder, prefix + "LandStrat_metric.tif")
ND_app_parameters = {'in': LS_Strat_pre, 'out': LS_Strat, 'mode': 'apply', 'mode.apply.mask': mask,
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, ndv)
setNoDataValue(LS_Strat_raw, ndv)
LS_Strat_norm = os.path.join(tmp_folder, prefix+"LandStrat_metric_norm.tif")
DC_app_parameters={"in":LS_Strat, "out":LS_Strat_norm, "mask":mask, "outmin":0, "outmax":2048, "outputpixeltype":2}
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)
LS_Strat = os.path.join(tmp_folder,prefix+"LandStrat_metric_norm_masked.tif")
LS_Strat = os.path.join(output_folder,prefix+"LandStrat_metric.tif")
ND_app_parameters = {'in': LS_Strat_norm, 'out':LS_Strat, 'mode':'apply', 'mode.apply.mask':mask, 'outputpixeltype':2}
processing.run('otb:ManageNoData', ND_app_parameters, context=context, feedback=feedback)
setNoDataValue(LS_Strat, ndv)
#os.remove(LS_Strat_pre)
#os.remove(LS_Strat_norm)
GRM_output_pre = os.path.join(tmp_folder,prefix+"LandStrat_map_pre.tif")
GRM_app_parameters = {'in':LS_Strat, 'threshold':float(threshold), 'criterion':'bs', 'out':GRM_output_pre}
if cw != 0.5:
GRM_app_parameters['criterion.bs.cw']= cw
if sw != 0.5:
GRM_app_parameters['criterion.bs.sw']= sw
processing.run("otb:LSGRM", GRM_app_parameters, context=context, feedback=feedback)
GRM_output = os.path.join(tmp_folder, prefix + "LandStrat_map.tif")
BMX_parameters = {'il': [GRM_output_pre, mask], 'exp': 'im1b1*im2b1', 'out': GRM_output, 'outputpixeltype': 3,
'outcontext': os.path.join(output_folder, "outcontext.txt")}
processing.run("otb:BandMathX", BMX_parameters, context=context, feedback=feedback)
os.remove(os.path.join(output_folder, "outcontext.txt"))
#os.remove(GRM_output_pre)
setNoDataValue(GRM_output, 0)
GRM_vec_output = os.path.join(output_folder,prefix+"LandscapeStratification.shp")
if os.path.exists(GRM_vec_output):
drv = ogr.GetDriverByName('ESRI Shapefile')
drv.DeleteDataSource(GRM_vec_output)
processing.run("gdal:polygonize", {'INPUT':GRM_output, 'BAND':1, 'OUTPUT':GRM_vec_output}, context=context, feedback=feedback)
nm = os.path.basename(os.path.splitext(GRM_vec_output)[0])
context.addLayerToLoadOnCompletion(GRM_vec_output,
nm = os.path.basename(os.path.splitext(LS_Strat)[0])
context.addLayerToLoadOnCompletion(LS_Strat,
QgsProcessingContext.LayerDetails(name=nm, project=context.project()))
return {'OUT':GRM_vec_output}
return {'OUT':LS_Strat}
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