How can I access the current feature's/layer's CRS from an QGIS expression, or from a custom expression in PyQGIS?

In my expression, I use the following to buffer a (currently lat-long-specified) feature in map units (m).


I'd like to generalize this to work with any layer, whatever its layer CRS is, i.e. without hardcoding the 'EPSG:4326'. But there doesn't seem to be a @layer_crs variable. Is there some other way to access this using core expression functions and variables?

As an alternative, I'm open to (and might even prefer) a PyQGIS solution that can be put into a custom function. But (I may be wrong), the QgsFeature passed as the feature parameter in an expression custom function does not know its (source) CRS or even its layer.

I've also looked at the optional context parameter to a custom function, but have gotten lost whether I can shovel through the QgsExpressionContext object this provides to find the parent layer and its source CRS.

1 Answer 1


For QGIS >= v3.18.0

You can use the @layer_crs variable in your expression.

For QGIS < v3.18.0

There is not a layer_crs, but we can use the 'crs' layer property:

layer_property(  @layer , 'crs' )

Sample output:

enter image description here

  • 3
    Does @layer instead of @layer_name work also? I think that should be best practice as it avoids a roundtrip over the layer store and possibly ambiguous name resolution. Feb 28, 2020 at 16:45
  • It works as well, thanks for the suggestion. Feb 28, 2020 at 16:46
  • 1
    There is a @layer_crs nowadays :) Nov 23, 2021 at 10:48
  • 1
    I'll update the answer, thanks @bugmenot123. Nov 23, 2021 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.