From 3861e1c7f188af06413714f375bb58ba377d112b Mon Sep 17 00:00:00 2001 From: JeanAlvarez Date: Thu, 7 Nov 2024 16:14:54 -0300 Subject: [PATCH] =?UTF-8?q?Corre=C3=A7=C3=B5es=20e=20padroniza=C3=A7=C3=A3?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exportFeaturesByAttributeAlgorithm.py | 113 +++++++++--------- 1 file changed, 57 insertions(+), 56 deletions(-) diff --git a/DsgTools/core/DSGToolsProcessingAlgs/Algs/OtherAlgs/exportFeaturesByAttributeAlgorithm.py b/DsgTools/core/DSGToolsProcessingAlgs/Algs/OtherAlgs/exportFeaturesByAttributeAlgorithm.py index c565da526..0956c7db8 100644 --- a/DsgTools/core/DSGToolsProcessingAlgs/Algs/OtherAlgs/exportFeaturesByAttributeAlgorithm.py +++ b/DsgTools/core/DSGToolsProcessingAlgs/Algs/OtherAlgs/exportFeaturesByAttributeAlgorithm.py @@ -7,7 +7,7 @@ ------------------- begin : 2024-11-06 git sha : $Format:%H$ - copyright : (C) 2024 by Jean Michael Estevez Alvarez- Brazilian Army + copyright : (C) 2024 by Jean Michael Estevez Alvarez - Brazilian Army email : jeanalvarez@id.uff.br ***************************************************************************/ /*************************************************************************** @@ -38,8 +38,9 @@ from ..Help.algorithmHelpCreator import HTMLHelpCreator as help - class ExportFeaturesByAttributeAlgorithm(QgsProcessingAlgorithm): + """Algorithm to export features based on attribute filtering.""" + INPUT = 'INPUT' FIELD = 'FIELD' OPERATOR = 'OPERATOR' @@ -56,44 +57,12 @@ class ExportFeaturesByAttributeAlgorithm(QgsProcessingAlgorithm): 'contains', 'is null', 'is not null', - 'does not contain' - ] + 'does not contain'] STRING_OPERATORS = ['begins with', 'contains', 'does not contain'] - def createInstance(self): - return ExportFeaturesByAttributeAlgorithm() - - def name(self): - return 'exportfeaturesbyattribute' - - def displayName(self): - return self.tr('Export Features by Attribute') - - - def group(self): - """ - Returns the name of the group this algorithm belongs to. This string - should be localised. - """ - return self.tr("Utils") - - 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 "DSGTools - Utils" - - def tr(self, string): - return QCoreApplication.translate("ExportFeaturesByAttribute", string - ) - def shortHelpString(self): - return help().shortHelpString(self.name()) def initAlgorithm(self, config=None): + """Initializes algorithm parameters.""" self.addParameter( QgsProcessingParameterVectorLayer( self.INPUT, @@ -135,20 +104,35 @@ def initAlgorithm(self, config=None): ) def processAlgorithm(self, parameters, context, feedback): + """Processes the algorithm.""" layer = self.parameterAsVectorLayer(parameters, self.INPUT, context) fieldName = self.parameterAsString(parameters, self.FIELD, context) operator = self.OPERATORS[self.parameterAsEnum(parameters, self.OPERATOR, context)] value = self.parameterAsString(parameters, self.VALUE, context) + fields = layer.fields() if layer else QgsFields() + (sink, dest_id) = self.parameterAsSink( + parameters, + self.OUTPUT, + context, + fields, + layer.wkbType() if layer else QgsWkbTypes.NoGeometry, + layer.sourceCrs() if layer else None + ) + + if sink is None: + raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT)) + + if layer is None: - feedback.pushInfo("Camada inválida ou não encontrada.") - return {self.OUTPUT: None} + raise QgsProcessingException("Camada inválida ou não encontrada.") + if fieldName not in layer.fields().names(): feedback.pushInfo(f"O atributo '{fieldName}' não foi encontrado na camada. Nenhuma exportação realizada.") - return {self.OUTPUT: None} + return {self.OUTPUT: dest_id} - # Construindo a expressão de seleção + field_ref = QgsExpression.quotedColumnRef(fieldName) if operator == 'is null': expression_string = f"{field_ref} IS NULL" @@ -166,31 +150,48 @@ def processAlgorithm(self, parameters, context, feedback): expression = QgsExpression(expression_string) if expression.hasParserError(): - feedback.pushInfo(f"Erro na expressão: {expression.parserErrorString()}") - return {self.OUTPUT: None} + raise QgsProcessingException(f"Erro na expressão: {expression.parserErrorString()}") - # Configurando o sink para a camada de saída - (sink, dest_id) = self.parameterAsSink( - parameters, - self.OUTPUT, - context, - layer.fields(), - layer.wkbType(), - layer.sourceCrs() - ) - - # Verificando se há features que correspondem ao filtro + features = layer.getFeatures(QgsFeatureRequest(expression)) matched_features = [f for f in features] - if not matched_features: + + + nFeatures = len(matched_features) + if nFeatures == 0: feedback.pushInfo("Nenhum recurso encontrado com o valor especificado.") return {self.OUTPUT: dest_id} - # Adiciona as features que correspondem ao filtro no sink - for feature in matched_features: + stepSize = 100 / nFeatures + + + for current, feature in enumerate(matched_features): if feedback.isCanceled(): break sink.addFeature(feature, QgsFeatureSink.FastInsert) + feedback.setProgress(int((current / nFeatures) * 100)) feedback.pushInfo("Exportação concluída com sucesso.") return {self.OUTPUT: dest_id} + + def name(self): + return 'exportfeaturesbyattribute' + + def displayName(self): + return self.tr('Export Features by Attribute') + + def group(self): + return self.tr("Utils") + + def groupId(self): + return "DSGTools - Utils" + + def tr(self, string): + return QCoreApplication.translate("ExportFeaturesByAttribute", string) + + def shortHelpString(self): + return help().shortHelpString(self.name()) + + def createInstance(self): + return ExportFeaturesByAttributeAlgorithm() +