I am writing a PyQGIS script (Python version 3.9) within FME Workbench 2024.1.2 Python caller transformer which has to run some processing tasks from the QGIS (LTR 3.34.4) processing framework:
import fme
import fmeobjects
import sys
from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback
class FeatureProcessor(object):
"""Template Class Interface:
When using this class, make sure its name is set as the value of the 'Class to Process Features'
transformer parameter.
"""
def __init__(self):
"""Base constructor for class members."""
# Initialize QGIS Application
QgsApplication.setPrefixPath("F:/QGIS 3.34.4/apps/qgis-ltr", True)
self.qgs = QgsApplication([], False)
self.qgs.initQgis()
# Append the path where processing plugin can be found
sys.path.append('F:/QGIS 3.34.4/apps/qgis-ltr/python/plugins/processing')
from processing.core.Processing import Processing
Processing.initialize()
import processing
def has_support_for(self, support_type: int):
"""This method is called by FME to determine if the PythonCaller supports Bulk mode,
which allows for significant performance gains when processing large numbers of features.
Bulk mode cannot always be supported.
More information available in transformer help.
"""
return support_type == fmeobjects.FME_SUPPORT_FEATURE_TABLE_SHIM
def input(self, feature: fmeobjects.FMEFeature):
"""This method is called for each feature which enters the PythonCaller.
Processed input features can be emitted from this method using self.pyoutput().
If knowledge of all input features is required for processing, then input features should be
cached to a list instance variable and processed using group processing or in the close() method.
"""
gpkg_path = feature.getAttribute("path_windows")
layer_name = feature.getAttribute("path_filename")
# Open the specified layer in the GeoPackage
layer = QgsVectorLayer(gpkg_path, layer_name + '|layername=' + layer_name, 'ogr')
crs = layer.crs().authid()
feature.setAttribute("CRS", crs)
params = {'INPUT': layer,
'ALL_PARTS': False,
'OUTPUT': 'memory:'}
result = processing.run("native:centroids", params)
centroid_layer = result['OUTPUT']
self.pyoutput(feature)
def close(self):
"""This method is called once all the FME Features have been processed from input()."""
self.qgs.exitQgis()
def process_group(self):
"""This method is called by FME for each group when group processing mode is enabled.
This implementation should reset any instance variables used for the next group.
Bulk mode should not be enabled when using group processing.
More information available in transformer help.
"""
pass
The QGIS core functionalities ran successfully (crs attribute with EPSG:4326 was added to the attribute table), so the python env settings should be correct. But processing framework seems not to be recognized.
Errors according to log file:
ERROR | Message Type: fme::internal::_v0::Exception |
ERROR | Python Exception <NameError>: name 'processing' is not defined |
ERROR | Error encountered while calling method `input' |
FATAL | PythonCaller (PythonFactory): PythonFactory failed to process feature |
Preferred Python interpreter (FME Options → Translation) is set to the Python39 dll file:
F:\QGIS 3.34.4\apps\Python39\python39.dll
Python Home (PYTHONHOME): F:\QGIS 3.34.4\apps\Python39
Environment variables for system user are:
PYTHON: F:\Apps\FMEForm\2024_1_2\python
PYTHONHOME: F:\QGIS 3.34.4\bin
PYTHONPATH: F:\QGIS 3.34.4\apps\qgis-ltr\python; F:\QGIS 3.34.4\apps\qgis-ltr\python\plugins
(Python Compatibility in Workspace set to Python 3.9+)
By only importing the processing module no error occurs. By running a processing algorithm, the above errors appear so no processing algorithm can be run.
Can you please help with this? Thank you!