15

Is there a way to get the extent (bbox) of a vector layer in QGIS?

I see that I can update the extent, but I am looking for the actual coordinates of the extent.

0

7 Answers 7

22

The layer extents are available in the Layer Properties | Metadata section | Extent.

enter image description here

12

To get a bounding box in QGIS as a new vector layer use the "Extract layer extent":
Vector -> Research Tools -> Extract layer extent

enter image description here

3
  • I don't really want to create a new file. I am only interested in the four coordinates.
    – ustroetz
    Feb 3, 2014 at 17:08
  • You could just use extract the nodes of the newly created polgygon and use the Export/Add geometry columns tool to get the corner coordinates. Put everything into a Processing model and it runs in no time...?
    – Curlew
    Feb 3, 2014 at 17:15
  • Note: This works for raster layers too
    – prusswan
    Jun 25, 2019 at 6:34
2

It is also possible to get the extent within the Field Calculator by means of the layer_property() function and its extent parameter, and a bunch of: x_min, x_max, y_min, and y_max.

x_min(layer_property('test', 'extent')) || ' ' || x_max(layer_property('test', 'extent')) || ' ' || y_min(layer_property('test', 'extent')) || ' ' || y_max(layer_property('test', 'extent'))

This expression will lead to this result:

435177.1907588559 435464.7619550383 5645765.283683421 5645985.346796726
2

PostGIS has the ST_Extent() function which generates a bounding box for you. So my approach would be to export the layer to a database. if you want id, name, geom, bounding box just do

select id, name, geom, ST_Extent(geom)
from your_table group by id
1

From the (right-click) context menu of the desired layer, choose Export => Save Feature As...

Layer Context Menu: Export -> Save Feature As...

In the dialog that appears, you can ignore all inputs and just scroll down to the Extent section and enable the checkbox.

Save Feature As Dialog: Extent Section

So for a standard bbox array of [minx,miny,maxx,maxy] your values would be [west,south,east,north].

1

There are several possibilities by means of the PyQGIS

  1. Using the .boundingBoxOfSelected() method
layer = QgsProject.instance().mapLayersByName('test')[0]
layer.selectAll()
extent = layer.boundingBoxOfSelected()
print(extent)
layer.removeSelection()
  1. Using the .extent() method
layer = QgsProject.instance().mapLayersByName('test')[0]
extent = layer.extent()
print(extent)

In both cases, the result will end up in a form of:

<QgsRectangle: 435177.19075885589700192 5645765.28368342109024525, 435464.76195503829512745 5645985.34679672587662935>

To get a more visually pleasing result, please extend the code with an approach mentioned in this answer

xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()

print('XMin:{0}, XMax:{1}, YMin:{2}, YMax:{3}'.format(xmin, xmax, ymin, ymax))

this will lead in

XMin:435177.1907588559, XMax:435464.7619550383, YMin:5645765.283683421, YMax:5645985.346796726
0

Also similar to @underdark's answer, you may see under the Information | Information from provider

extent

The extent is provided in the following order: xmin,ymin : xmax,ymax

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.