Hi Thijs,
OK, now I understand the problem. It looks like the ErosionTask
keeps only labels that are being eroded. The rest are set to no_data_value
. IMO, this is a bug and not a feature.
For the time being you can use this custom task (until the ErosionTask is fixed):
class ClassFilterTask(EOTask):
"""
Run class specific morphological operation.
"""
def __init__(self, lulc_feature, lulc_values, morph_operation, struct_elem=None):
self.lulc_feature_type, self.lulc_feature_name = next(iter(self._parse_features(lulc_feature)))
self.lulc_values=lulc_values
if isinstance(morph_operation, MorphologicalOperations):
self.morph_operation = MorphologicalOperations.get_operation(morph_operation)
else:
self.morph_operation = morph_operation
self.struct_elem = struct_elem
def execute(self, eopatch):
lulc = eopatch[self.lulc_feature_type][self.lulc_feature_name].copy()
for lulc_value in self.lulc_values:
lulc_mod = self.morph_operation((lulc==lulc_value).squeeze(), self.struct_elem) * lulc_value
lulc_mod = lulc_mod[...,np.newaxis]
lulc[lulc==lulc_value]=lulc_mod[lulc==lulc_value]
eopatch.add_feature(self.lulc_feature_type, self.lulc_feature_name, lulc)
return eopatch
which can be then used as
from eolearn.ml_tools import MorphologicalFilterTask, MorphologicalOperations, MorphologicalStructFactory
erode_1 = ClassFilterTask((FeatureType.MASK_TIMELESS, 'LULC'),[1,2,9],
MorphologicalOperations.EROSION,struct_elem=MorphologicalStructFactory.get_disk(3))
erode_2 = ClassFilterTask((FeatureType.MASK_TIMELESS, 'LULC'),[5,8],
MorphologicalOperations.EROSION,struct_elem=MorphologicalStructFactory.get_disk(1))
Please note that you shouldnāt include no_data_value
(0) to labels to be eroded.
I hope this will work for you.