Using QGIS expressions, it's easy to create a polygon with the extent (bounding box) covering all geometries of a layer using layer_property(@layer,'extent').

However, how do I get a polygon covering the whole layer extent, not only the extent of the geometry?

See the screenshot: the expression generates the blue polygon. However, I look for an expression generating a polygon symbolized here with the pink frame:

enter image description here

  • I do not really understand the purpose of this action?
    – Taras
    Jun 24, 2021 at 11:05
  • See here why: gis.stackexchange.com/a/402125/88814 - to be able to get the output of layer_property('OpenStreetMap','extent') without need of loading another layer (OpenStreetMap).
    – Babel
    Jun 24, 2021 at 11:07
  • So, layer-extent would be equal to CRS-extent?
    – Erik
    Jun 24, 2021 at 11:09
  • Yes, I guess so
    – Babel
    Jun 24, 2021 at 11:20

1 Answer 1


You can define a custom function to get crs extent.

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

@qgsfunction(args='auto', group='Custom')
def crs_extent(layer_name, feature, parent):
    layers = QgsProject.instance().mapLayersByName(layer_name)
    if layers:
        bounds = layers[0].crs().bounds()
        raise ValueError(f"Layer {layer_name} could not found")
    return QgsGeometry.fromRect(bounds)

Result for crs_extent(@layer_name):

enter image description here

Result for crs_extent('OSM'): (No empty space at the top/bottom)

enter image description here

Note: Layer's CRS is WGS84 (EPSG:4326). If you use projected CRS for Layer, you may not get an expected result. For example, UTM's unit is meter, but crs().bounds() returns bounds in degree.

  • Avoiding a thank you message, still a warm "welcome back" message - we missed you here !
    – Babel
    Aug 29, 2021 at 15:26
  • 2
    Nice, indeed! And transform (crs_extent( 'layer_name'), 'EPSG:4326','EPSG:3857') does the job to get it to another projection.
    – Babel
    Aug 29, 2021 at 15:32
  • YOU are an expert in expression, not me. :) Aug 29, 2021 at 15:35

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.