10

I would like to know if there is a way to add a dynamic coordinate system field into a QGIS composer layout.

I can use an expression to add many items such as document name, file path, etc but I can;t seem to figure out how to add the coordinate system?

1
  • 1
    If you are using QGIS 3.x now, you may wish to move your green accept checkmark to the QGIS 3.x answer.
    – PolyGeo
    Nov 6, 2018 at 22:06

2 Answers 2

10

Answer for QGIS 3.x:

A) For the CRS name of a specific layer use this:

Add this script to your custom functioneditor:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def get_crs(layer_name, feature, parent):
    return QgsProject.instance().mapLayersByName(layer_name)[0].crs().description()

enter image description here

and then get the crs name in expression editor with

get_crs(@layer_name)

enter image description here

Or use the expression layer_property( @layer_name, 'crs_description') as suggested by @etrimaille

B) For the CRS name of your project (for example projection in your print layout) use this:

Add this script to your custom functioneditor:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def get_projectcrs(project_crs, feature, parent):
    return QgsCoordinateReferenceSystem(project_crs).description()

enter image description here

and then get the crs name in expression editor with:

 get_projectcrs(@project_crs) 

enter image description here

Of course you can also type in e.g. 'EPSG:4326' manually or use another function than @project_crs which returns an EPSG-code. E.g. get_projectcrs('EPSG:3006')

Notes:

Also take a look at https://qgis.org/api/api_break.html for more information about changes in QGIS 3.x

3
  • Thanks @MrXsquared ! I previously tried exactly that code but didn't use the @ when passing the name of the layer. However, I still can't get this to work. When I add a map in the layout view and insert a label with the aforementioned function, the 'Output Preview' just shows NULL and no information is added in the label. I checked everything several times, the map has a name (and id) and a CRS but nothing shows up. It shouldn't depend on the specific CRS used, right?
    – TinkerPhil
    Nov 5, 2018 at 9:16
  • Of course this will not work in layout view since this refers to a specific layer. I edited my answer and added a function to return the used crs of your project. You can use this in your print layout to display the name of the used projection.
    – MrXsquared
    Nov 5, 2018 at 20:12
  • 2
    For your answer A, no need to have a python expression. You can do ` layer_property( @layer_name, 'crs_description')`.
    – etrimaille
    Nov 5, 2018 at 21:23
8

This answer was provided and accepted prior to the release of QGIS 3.0.

To get the Authority ID (i.e. "EPSG:4326"):

  • You can simply use the expression [% layer_property( 'your_layer_name_or_id', 'crs' ) %]

If you want to get the textual description of the CRS (i.e. "WGS84"):

  • AFAIK, you must create a custom function. To do this, go to "Insert an expression", and type the following function in the Function Editor tab.

Code:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def get_crs(layer_name, feature, parent):
    return QgsMapLayerRegistry.instance().mapLayersByName(layer_name)[0].crs().description()
  • Click on "Load"
  • In the Expression tab, type get_crs( 'your_layer_name' )
0

Your Answer

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

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