Using FME Form 2024.1.2 and QGIS 3.34.4 with Python 3.9 the following script:
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
shows the following 4 erorrs (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
Without running a processing algorithm:
#result = processing.run("native:centroids", params)
#centroid_layer = result['OUTPUT']
the code runs without errors, a new attribute CRS with EPSG:4326 is created in the attribute table, so both the qgis core module as the processing framework import process should be correct and recognized. But the processing algorithm (“native:centroids”) cannot be run and gets the above mentioned errors.
“Preferred Python Interpreter” in FME Options → Translation is set to: F:\QGIS 3.34.4\apps\Python39\python39.dll,
“Python Home (PYTHONHOME): F:\QGIS 3.34.4\apps\Python39, Python Compatibility in the Workspace is set to Python 3.9+
Python7QGIS related User enviroment Variables are set to:
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
Can you please help with this? Thank you!