Skip to content

AlgorithmFactory

Import
from pyqgis_wrapper.processing import AlgorithmFactory

AlgorithmFactory

Factory for QgsProcessingAlgorithm

fetch(name, provider=None, exact_match=False) staticmethod

Allows to search the processing registry for a specific algorithm. Caller can define a partial name (insensitive to case) and optionnaly an existing provider such as : - "3d" - "gdal" - "grass" - "native" - "pdal" - "qgis" - "saga" - "sagang" - "model" - "script"

The method will return two list : matching algorithm ids and a short display string of the algorithm to help selecting the right one.

Caller can then call the build method with the wanted alg id by either slicing the list or copy pasting the string id.

Parameters:

Name Type Description Default
name str

Partial or total name. Alg will return any algoirtmh that matches the name in his algorithm id or display name.

required
provider typing.Optional[typing.Union[str, pyqgis_wrapper.processing.algorithms.ProviderType]]

Provider to perform an advanced filter, defaults to None

None
exact_match bool

Perform an exact search, defaults to False

False

Returns:

Type Description
Tuple[List[str], List[str]]

List of matching algorithm ids and list of display name of the matching algorithm.

Source code in pyqgis_wrapper/processing/algorithms.py
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
@staticmethod
def fetch(
    name: str,
    provider: Optional[Union[str, ProviderType]] = None,
    exact_match: bool = False,
) -> Tuple[List[str], List[str]]:
    """
    Allows to search the processing registry for a specific algorithm.
    Caller can define a partial name (insensitive to case) and optionnaly an existing provider such as :
         - "3d"
         - "gdal"
         - "grass"
         - "native"
         - "pdal"
         - "qgis"
         - "saga"
         - "sagang"
         - "model"
         - "script"

    The method will return two list : matching algorithm ids and a
    short display string of the algorithm to help selecting the right one.

    Caller can then call the `build` method with the wanted alg id by either
    slicing the list or copy pasting the string id.

    :param name: Partial or total name. Alg will return any algoirtmh
    that matches the name in his algorithm id or display name.
    :type name: str
    :param provider: Provider to perform an advanced filter, defaults to None
    :type provider: Optional[Union[str, ProviderType]], optional
    :param exact_match: Perform an exact search, defaults to False
    :type exact_match: bool

    :return: List of matching algorithm ids and list of display name of the matching algorithm.
    :rtype: Tuple[List[str], List[str]]
    """
    provider_id = AlgorithmFactory._resolve_provider(provider)
    name_lower = name.lower()
    provider_id_lower = provider_id.lower() if provider_id else None
    name_lower = AlgorithmFactory._adjust_name_for_exact_match(
        name_lower, provider_id_lower, exact_match
    )

    return AlgorithmFactory._match_algorithms(
        name_lower, provider_id_lower, exact_match
    )

build(algorithm_id) classmethod

Return a dynamically created subclass of QgisAlgorithm with the given algorithm_id. Caches the result for reuse.

The algorithm can then be used normally by calling apply and optionnaly build_parameters

Parameters:

Name Type Description Default
algorithm_id str

Matching algoirthm id. To be sure of the match you can use the fetch method

required

Returns:

Type Description
Type[QgisAlgorithm]

An instance of QgisAlgorithm or any of its subclasses dynamically created.

Source code in pyqgis_wrapper/processing/algorithms.py
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
@classmethod
def build(cls, algorithm_id: str) -> Type[QgisAlgorithm]:
    """
    Return a dynamically created subclass of QgisAlgorithm with the given algorithm_id.
    Caches the result for reuse.

    The algorithm can then be used normally by calling `apply` and optionnaly `build_parameters`

    :param algorithm_id: Matching algoirthm id. To be sure of the match you can use the `fetch` method
    :type algorithm_id: str

    :return: An instance of QgisAlgorithm or any of its subclasses dynamically created.
    :rtype: Type[QgisAlgorithm]
    """
    if algorithm_id in cls._algorithms_cache:
        return cls._algorithms_cache[algorithm_id]

    algorithm = QgsApplication.processingRegistry().algorithmById(algorithm_id)
    if not algorithm:
        raise AlgorithmNotFound(f"Invalid algorithm_id: {algorithm_id}")

    alias = algorithm_id.split(":")[1].replace(".", "")
    class_name = f"{alias.title()}Algorithm"
    base_class = cls._detect_algorithm_type(algorithm).base_class()

    dynamic_class = type(
        class_name,
        (base_class,),
        {
            "algorithm_id": algorithm_id,
            "name": algorithm.displayName(),
        },
    )

    # Apparently dynamic class creation can be heavy so we cache it
    cls._algorithms_cache[algorithm_id] = dynamic_class
    return dynamic_class

AlgorithmType

Bases: str, enum.Enum

Class to store algorithm type and base class to build dynamically.

base_class()

Link the algorithm type to the base class. You can ADD or UPDATE the dict when needed

Source code in pyqgis_wrapper/processing/algorithms.py
43
44
45
46
47
48
49
50
51
52
53
def base_class(self):
    """
    Link the algorithm type to the base class.
    You can ADD or UPDATE the dict when needed
    """
    return {
        AlgorithmType.VECTOR: VectorAlgorithm,
        AlgorithmType.RASTER: RasterAlgorithm,
        AlgorithmType.CLOUD: PointCloudAlgorithm,
        AlgorithmType.UNKNOWN: QgisAlgorithm,
    }.get(self, QgisAlgorithm)

ProviderType

Bases: str, enum.Enum

Store the available QgsProcessingProvider to avoid typo

TODO: Dynamically build available providers