4

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).

...buffer(transform($geometry,'EPSG:4326',@project_crs),250,1)...

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.

8

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

layer_property(  @layer , 'crs' )

Sample output:

enter image description here

  • 2
    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. – Matthias Kuhn Feb 28 at 16:45
  • It works as well, thanks for the suggestion. – Germán Carrillo Feb 28 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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