I'm working on a custom QGIS Processing algorithm, and I'd like to include an option for the user to process only the selected features in the input layer by using the checkbox "Selected Features Only" for QgsProcessingParameterFeatureSource
, I spend hours trying to fix it but I don't see a way to get that. I'm using QGIS 3.22
Here's a simplified version of my script:
def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterFeatureSource(
self.INPUT,
self.tr('Layer contendo pontos finais e iniciais de cada parcela'),
[QgsProcessing.TypeVectorPoint]
)
)
def processAlgorithm(self, parameters, context, feedback):
source_layer = self.parameterAsSource(
parameters,
self.INPUT,
context,
QgsProcessingFeatureSource.Flags()
)
new_field_1 = processing.run("native:fieldcalculator", {
'INPUT': source_layer.materialize(QgsFeatureRequest()), # Use 'source_layer' directly
'FIELD_NAME': 'groupkey',
'FIELD_TYPE': 2,
'FIELD_LENGTH': 25,
'FIELD_PRECISION': 0,
'FORMULA': 'concat("patio", \'-\', "orderparc")',
'OUTPUT': 'memory:'
})
points_to_path_result = processing.run("native:pointstopath", {
'INPUT': new_field_1['OUTPUT'],
'CLOSE_PATH': True,
'ORDER_EXPRESSION': '"Name"',
'NATURAL_SORT': False,
'GROUP_EXPRESSION': '"groupkey"',
'OUTPUT': 'memory:'
}, context=context, feedback=feedback)
# rest of the scrit...