Skip to content
Snippets Groups Projects
Commit c92b5146 authored by paul.tresson_ird.fr's avatar paul.tresson_ird.fr
Browse files

load parameters from json file for encoder.

parent e5e80c36
No related branches found
No related tags found
No related merge requests found
...@@ -86,6 +86,7 @@ class EncoderAlgorithm(QgsProcessingAlgorithm): ...@@ -86,6 +86,7 @@ class EncoderAlgorithm(QgsProcessingAlgorithm):
PAUSES = 'PAUSES' PAUSES = 'PAUSES'
REMOVE_TEMP_FILES = 'REMOVE_TEMP_FILES' REMOVE_TEMP_FILES = 'REMOVE_TEMP_FILES'
TEMP_FILES_CLEANUP_FREQ = 'TEMP_FILES_CLEANUP_FREQ' TEMP_FILES_CLEANUP_FREQ = 'TEMP_FILES_CLEANUP_FREQ'
JSON_PARAM = 'JSON_PARAM'
def initAlgorithm(self, config=None): def initAlgorithm(self, config=None):
...@@ -311,6 +312,16 @@ class EncoderAlgorithm(QgsProcessingAlgorithm): ...@@ -311,6 +312,16 @@ class EncoderAlgorithm(QgsProcessingAlgorithm):
defaultValue=0, defaultValue=0,
) )
json_param = QgsProcessingParameterFile(
name=self.JSON_PARAM,
description=self.tr(
'Pass parameters as json file'),
# extension='pth',
fileFilter='JSON Files (*.json)',
optional=True,
defaultValue=None
)
for param in ( for param in (
crs_param, crs_param,
res_param, res_param,
...@@ -321,6 +332,7 @@ class EncoderAlgorithm(QgsProcessingAlgorithm): ...@@ -321,6 +332,7 @@ class EncoderAlgorithm(QgsProcessingAlgorithm):
pauses_param, pauses_param,
remove_tmp_files, remove_tmp_files,
tmp_files_cleanup_frq, tmp_files_cleanup_frq,
json_param,
): ):
param.setFlags( param.setFlags(
param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
...@@ -333,6 +345,8 @@ class EncoderAlgorithm(QgsProcessingAlgorithm): ...@@ -333,6 +345,8 @@ class EncoderAlgorithm(QgsProcessingAlgorithm):
""" """
Here is where the processing itself takes place. Here is where the processing itself takes place.
""" """
parameters = self.load_parameters_as_json(feedback, parameters)
self.process_options(parameters, context, feedback) self.process_options(parameters, context, feedback)
## compute parameters hash to have a unique identifier for the run ## compute parameters hash to have a unique identifier for the run
...@@ -638,6 +652,20 @@ class EncoderAlgorithm(QgsProcessingAlgorithm): ...@@ -638,6 +652,20 @@ class EncoderAlgorithm(QgsProcessingAlgorithm):
return {"Output feature path": self.output_subdir, 'Patch samples saved': self.iPatch, 'OUTPUT_RASTER':dst_path, 'OUTPUT_LAYER_NAME':layer_name} return {"Output feature path": self.output_subdir, 'Patch samples saved': self.iPatch, 'OUTPUT_RASTER':dst_path, 'OUTPUT_LAYER_NAME':layer_name}
def load_parameters_as_json(self, feedback, parameters):
parameters['JSON_PARAM'] = str(parameters['JSON_PARAM'])
json_param = parameters['JSON_PARAM']
print(json_param)
if json_param != 'NULL':
with open(json_param) as json_file:
parameters = json.load(json_file)
feedback.pushInfo(f'Loading previous parameters from {json_param}')
parameters.pop('JSON_PARAM',None)
else:
parameters.pop('JSON_PARAM',None)
return parameters
def remove_temp_files(self): def remove_temp_files(self):
""" """
......
...@@ -46,7 +46,8 @@ class TestEncoderAlgorithm(unittest.TestCase): ...@@ -46,7 +46,8 @@ class TestEncoderAlgorithm(unittest.TestCase):
'SIZE': 224, 'SIZE': 224,
'STRIDE': 224, 'STRIDE': 224,
'TEMP_FILES_CLEANUP_FREQ': 1000, 'TEMP_FILES_CLEANUP_FREQ': 1000,
'WORKERS': 0 'WORKERS': 0,
'JSON_PARAM': 'NULL',
} }
result = self.algorithm.processAlgorithm(parameters, self.context, self.feedback) result = self.algorithm.processAlgorithm(parameters, self.context, self.feedback)
expected_result_path = os.path.join(self.algorithm.output_subdir,'merged.tif') expected_result_path = os.path.join(self.algorithm.output_subdir,'merged.tif')
......
...@@ -103,7 +103,6 @@ def save_parameters_to_json(parameters, output_dir): ...@@ -103,7 +103,6 @@ def save_parameters_to_json(parameters, output_dir):
converted_parameters = convert_qvariant(parameters) converted_parameters = convert_qvariant(parameters)
converted_parameters['CKPT'] = str(converted_parameters['CKPT']) converted_parameters['CKPT'] = str(converted_parameters['CKPT'])
for key, item in converted_parameters.items():
with open(dst_path, "w") as json_file: with open(dst_path, "w") as json_file:
json.dump(converted_parameters, json_file, indent=4) json.dump(converted_parameters, json_file, indent=4)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment