-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a4cc7f
commit 3861e1c
Showing
1 changed file
with
57 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 : [email protected] | ||
***************************************************************************/ | ||
/*************************************************************************** | ||
|
@@ -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() | ||
|