8

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?

  • 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 '18 at 22:06
7

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' )
6

Answer for QGIS 3.x:

For easier solution without python see comment by gustry below

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

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

and then get the crs name in expression editor with:

 get_projectcrs(@project_crs) 

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

Notes:

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

  • 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 '18 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 '18 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 '18 at 21:23

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.