diff --git a/PreCourlis/processing/repair_profiles_algorithm.py b/PreCourlis/processing/repair_profiles_algorithm.py new file mode 100644 index 0000000000000000000000000000000000000000..24defbd97c47850346b3d26fccb54341fc85de62 --- /dev/null +++ b/PreCourlis/processing/repair_profiles_algorithm.py @@ -0,0 +1,144 @@ +from qgis.core import ( + QgsProcessing, + QgsProcessingParameterVectorLayer, + QgsProcessingParameterFeatureSink, + QgsProcessingOutputLayerDefinition +) + +from PreCourlis import processing +from PreCourlis.processing.precourlis_algorithm import PreCourlisAlgorithm + + +class RepairProfilesAlgorithm(PreCourlisAlgorithm): + INPUT = "INPUT" + OUTPUT = "OUTPUT" + + def initAlgorithm(self, config=None): + self.addParameter( + QgsProcessingParameterVectorLayer( + self.INPUT, + self.tr("Input"), + types=[QgsProcessing.TypeVectorLine], + defaultValue=None, + ) + ) + + self.addParameter( + QgsProcessingParameterFeatureSink( + self.OUTPUT, + self.tr("Output"), + type=QgsProcessing.TypeVectorLine, + createByDefault=True, + defaultValue=None, + ) + ) + + def processAlgorithm(self, parameters, context, model_feedback): + layer = self.parameterAsVectorLayer(parameters, self.INPUT, context) + + output = QgsProcessingOutputLayerDefinition(parameters[self.OUTPUT]) + output.destinationName = self.tr("Interpolated") + + TOPO_BAT_EXPR = "'B' || replace(rpad('', num_points($geometry) - 1, ','), ',', ',B')" + + processing.run( + "native:refactorfields", + { + "INPUT": layer, + "FIELDS_MAPPING": [ + { + "expression": '"sec_id"', + "length": 0, + "name": "sec_id", + "precision": 0, + "type": 2, + }, + { + "expression": '"sec_name"', + "length": 0, + "name": "sec_name", + "precision": 0, + "type": 10, + }, + { + "expression": '"abs_long"', + "length": 0, + "name": "abs_long", + "precision": 0, + "type": 6, + }, + { + "expression": '"axis_x"', + "length": 0, + "name": "axis_x", + "precision": 0, + "type": 6, + }, + { + "expression": '"axis_y"', + "length": 0, + "name": "axis_y", + "precision": 0, + "type": 6, + }, + { + "expression": "''", + "length": 0, + "name": "layers", + "precision": 0, + "type": 10, + }, + { + "expression": '"p_id"', + "length": 0, + "name": "p_id", + "precision": 0, + "type": 10, + }, + { + "expression": TOPO_BAT_EXPR, + "length": 0, + "name": "topo_bat", + "precision": 0, + "type": 10, + }, + { + "expression": '"abs_lat"', + "length": 0, + "name": "abs_lat", + "precision": 0, + "type": 10, + }, + { + "expression": '"zfond"', + "length": 0, + "name": "zfond", + "precision": 0, + "type": 10, + }, + ], + "OUTPUT": output + }, + ) + + return {self.OUTPUT: output} + + def name(self): + return "repair_profiles" + + def displayName(self): + return self.tr("Repair profiles layer") + + def shortHelpString(self): + return self.tr( + "This algorithm repair a profiles layer by adding missing topo_bath field." + ) + + def group(self): + return self.tr("Profiles") + + def groupId(self): + return "Profiles" + + def createInstance(self): + return RepairProfilesAlgorithm()