Dear Developers,

to implement a QgsExpressionFunction I like to access the "layer" from a QgsExpressionContext "context".
Using the C++ API this can be done with the QgsExpressionUtils:

QgsVectorLayer *vl = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent );

(several example exist qgsexpressionfunction.cpp)


In Python the QgsExpressionUtils are not available and accessing the "layer" attribute raises a TypeError "unable to convert a C++ 'QPointer<QgsMapLayer>' instance to a Python object"

Example:
from qgis.core import QgsExpressionContext, QgsVectorLayer, QgsExpressionContextUtils

lyr = QgsVectorLayer('point?crs=epsg:4326&field=id:integer', 'dummy', 'memory')

context = QgsExpressionContext()
context.appendScope(QgsExpressionContextUtils.layerScope(lyr))
print(f'name={context.variable("layer_name")}')

# this raises: TypeError: unable to convert a C++ 'QPointer<QgsMapLayer>' instance to a Python object
lyr2 = context.variable('layer')


Is there another way to get the QgsMapLayer instance in Python?

If not, I could open an issue to bring the QgsExpressionUtils into the QGIS Python API as well.








--
--
Benjamin Jakimow, Doctoral Researcher
Earth Observation Lab | Geography Department | Humboldt-Universität zu Berlin

e-mail: benjamin.jaki...@geo.hu-berlin.de

phone:  +49 (0) 30 2093 6894
mobile: +49 (0) 157 5656 8477
fax:    +49 (0) 30 2093 6848
mail:   Unter den Linden 6 | 10099 Berlin | Germany
room: 2'222
_______________________________________________
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to